11import Onyx from 'react-native-onyx' ;
2- import { createPolicyTax , deletePolicyTaxes , renamePolicyTax , setPolicyTaxesEnabled , updatePolicyTaxValue } from '@libs/actions/TaxRate' ;
2+ import { createPolicyTax , deletePolicyTaxes , renamePolicyTax , setPolicyTaxCode , setPolicyTaxesEnabled , updatePolicyTaxValue } from '@libs/actions/TaxRate' ;
33import CONST from '@src/CONST' ;
44import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager' ;
55import * as Policy from '@src/libs/actions/Policy/Policy' ;
@@ -12,7 +12,27 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
1212
1313OnyxUpdateManager ( ) ;
1414describe ( 'actions/PolicyTax' , ( ) => {
15- const fakePolicy : PolicyType = { ...createRandomPolicy ( 0 ) , taxRates : CONST . DEFAULT_TAX } ;
15+ const fakePolicy : PolicyType = {
16+ ...createRandomPolicy ( 0 ) ,
17+ taxRates : CONST . DEFAULT_TAX ,
18+ customUnits : {
19+ [ CONST . CUSTOM_UNITS . NAME_DISTANCE ] : {
20+ name : CONST . CUSTOM_UNITS . NAME_DISTANCE ,
21+ customUnitID : 'id_CUSTOM_UNIT_1' ,
22+ enabled : true ,
23+ rates : {
24+ // eslint-disable-next-line @typescript-eslint/naming-convention
25+ id_CUSTOM_UNIT_1 : {
26+ name : 'Distance' ,
27+ customUnitRateID : 'id_CUSTOM_UNIT_1' ,
28+ enabled : true ,
29+ currency : 'USD' ,
30+ rate : 67 ,
31+ } ,
32+ } ,
33+ } ,
34+ } ,
35+ } ;
1636 beforeAll ( ( ) => {
1737 Onyx . init ( {
1838 keys : ONYXKEYS ,
@@ -489,7 +509,8 @@ describe('actions/PolicyTax', () => {
489509 const taxID = 'id_TAX_RATE_1' ;
490510 const newTaxName = 'Tax rate 1 updated' ;
491511 mockFetch ?. pause ?.( ) ;
492- renamePolicyTax ( fakePolicy . id , taxID , newTaxName ) ;
512+ // @ts -expect-error - we can send undefined tax rate here for testing
513+ renamePolicyTax ( fakePolicy . id , taxID , newTaxName , fakePolicy ?. taxRates ?. taxes [ taxID ] ) ;
493514 return waitForBatchedUpdates ( )
494515 . then (
495516 ( ) =>
@@ -534,7 +555,8 @@ describe('actions/PolicyTax', () => {
534555 const newTaxName = 'Tax rate 1 updated' ;
535556 const originalTaxRate = { ...fakePolicy ?. taxRates ?. taxes [ taxID ] } ;
536557 mockFetch ?. pause ?.( ) ;
537- renamePolicyTax ( fakePolicy . id , taxID , newTaxName ) ;
558+ // @ts -expect-error - we can send undefined tax rate here for testing
559+ renamePolicyTax ( fakePolicy . id , taxID , newTaxName , fakePolicy ?. taxRates ?. taxes [ taxID ] ) ;
538560 return waitForBatchedUpdates ( )
539561 . then (
540562 ( ) =>
@@ -584,7 +606,8 @@ describe('actions/PolicyTax', () => {
584606 const newTaxValue = 10 ;
585607 const stringTaxValue = `${ newTaxValue } %` ;
586608 mockFetch ?. pause ?.( ) ;
587- updatePolicyTaxValue ( fakePolicy . id , taxID , newTaxValue ) ;
609+ // @ts -expect-error - we can send undefined tax rate here for testing
610+ updatePolicyTaxValue ( fakePolicy . id , taxID , newTaxValue , fakePolicy ?. taxRates ?. taxes [ taxID ] ) ;
588611 return waitForBatchedUpdates ( )
589612 . then (
590613 ( ) =>
@@ -630,7 +653,8 @@ describe('actions/PolicyTax', () => {
630653 const originalTaxRate = { ...fakePolicy ?. taxRates ?. taxes [ taxID ] } ;
631654 const stringTaxValue = `${ newTaxValue } %` ;
632655 mockFetch ?. pause ?.( ) ;
633- updatePolicyTaxValue ( fakePolicy . id , taxID , newTaxValue ) ;
656+ // @ts -expect-error - we can send undefined tax rate here for testing
657+ updatePolicyTaxValue ( fakePolicy . id , taxID , newTaxValue , fakePolicy ?. taxRates ?. taxes [ taxID ] ) ;
634658 return waitForBatchedUpdates ( )
635659 . then (
636660 ( ) =>
@@ -827,4 +851,46 @@ describe('actions/PolicyTax', () => {
827851 ) ;
828852 } ) ;
829853 } ) ;
854+ describe ( 'SetPolicyTaxCode' , ( ) => {
855+ const oldTaxCode = 'id_TAX_RATE_1' ;
856+ const newTaxCode = 'id_TAX_RATE_2' ;
857+ const oldTaxRateName = fakePolicy ?. taxRates ?. taxes [ oldTaxCode ] ?. name ;
858+
859+ it ( 'Set policy`s tax code' , ( ) => {
860+ mockFetch ?. pause ?.( ) ;
861+ const distanceRateCustomUnit = fakePolicy ?. customUnits ?. [ CONST . CUSTOM_UNITS . NAME_DISTANCE ] ;
862+
863+ setPolicyTaxCode (
864+ fakePolicy . id ,
865+ oldTaxCode ,
866+ newTaxCode ,
867+ // @ts -expect-error - we can send undefined tax rate here for testing
868+ fakePolicy ?. taxRates ?. taxes [ oldTaxCode ] ,
869+ fakePolicy ?. taxRates ?. foreignTaxDefault ,
870+ fakePolicy ?. taxRates ?. defaultExternalID ,
871+ distanceRateCustomUnit ,
872+ ) ;
873+
874+ return waitForBatchedUpdates ( ) . then (
875+ ( ) =>
876+ new Promise < void > ( ( resolve ) => {
877+ const connection = Onyx . connect ( {
878+ key : `${ ONYXKEYS . COLLECTION . POLICY } ${ fakePolicy . id } ` ,
879+ waitForCollectionCallback : false ,
880+ callback : ( policy ) => {
881+ Onyx . disconnect ( connection ) ;
882+ const taxRates = policy ?. taxRates ;
883+ const updatedTaxRate = taxRates ?. taxes ?. [ newTaxCode ] ;
884+
885+ // We expected to have a new tax rate with the new tax code
886+ expect ( updatedTaxRate ) . toBeDefined ( ) ;
887+ expect ( updatedTaxRate ?. previousTaxCode ) . toBe ( oldTaxCode ) ;
888+ expect ( updatedTaxRate ?. name ) . toBe ( oldTaxRateName ) ;
889+ resolve ( ) ;
890+ } ,
891+ } ) ;
892+ } ) ,
893+ ) ;
894+ } ) ;
895+ } ) ;
830896} ) ;
0 commit comments