Repository navigation
fix: Incorrect currency displayed when creating Distance expense in selfDM #81930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
893a837
fix: Incorrect currency displayed when creating Distance expense in s…
nkdengineer 3160f3a
resolve conflict
nkdengineer bbda3e3
fix lint
nkdengineer 2fe40c7
remove duplicate import
nkdengineer 2b1872a
fix lint
nkdengineer 97fb0e5
Merge branch 'main' into fix/80806
nkdengineer 9d5a2c5
write unit test for usePolicyForMovingExpenses
nkdengineer 36d9bc7
add explain comment
nkdengineer 7eba64c
fix prettier
nkdengineer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| import {renderHook} from '@testing-library/react-native'; | ||
| import Onyx from 'react-native-onyx'; | ||
| import OnyxListItemProvider from '@components/OnyxListItemProvider'; | ||
| import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
| import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
|
||
| const USER_EMAIL = 'user@example.com'; | ||
|
|
||
| function createValidPolicyForMovingExpenses(id: string, overrides: Partial<Policy> = {}): Policy { | ||
| return { | ||
| id, | ||
| name: `Workspace ${id}`, | ||
| type: CONST.POLICY.TYPE.TEAM, | ||
| role: CONST.POLICY.ROLE.ADMIN, | ||
| pendingAction: undefined, | ||
| employeeList: {}, | ||
| isPolicyExpenseChatEnabled: true, | ||
| customUnits: {}, | ||
| outputCurrency: 'USD', | ||
| ...overrides, | ||
| } as Policy; | ||
| } | ||
|
|
||
| describe('usePolicyForMovingExpenses', () => { | ||
| beforeAll(() => { | ||
| Onyx.init({ | ||
| keys: ONYXKEYS, | ||
| }); | ||
| return waitForBatchedUpdates(); | ||
| }); | ||
|
|
||
| beforeEach(async () => { | ||
| await Onyx.clear(); | ||
| return waitForBatchedUpdates(); | ||
| }); | ||
|
|
||
| it('should return default values when no policies and no session', () => { | ||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return undefined policy when isTrackDistanceRequest is true and isMovingFromTrackExpense is false', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('1'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(false, undefined, true, false), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return single valid policy when user is member of one policy', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('policy1'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBe('policy1'); | ||
| expect(result.current.policyForMovingExpenses?.id).toBe('policy1'); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return shouldSelectPolicy true when user is member of more than one valid policy', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy1 = createValidPolicyForMovingExpenses('policy1'); | ||
| const policy2 = createValidPolicyForMovingExpenses('policy2'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy1.id}`, policy1); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy2.id}`, policy2); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| expect(result.current.shouldSelectPolicy).toBe(true); | ||
| }); | ||
|
|
||
| it('should prefer expensePolicyID when provided and valid', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| await Onyx.set(ONYXKEYS.NVP_ACTIVE_POLICY_ID, 'activePolicy'); | ||
| const activePolicy = createValidPolicyForMovingExpenses('activePolicy'); | ||
| const expensePolicy = createValidPolicyForMovingExpenses('expensePolicy'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${expensePolicy.id}`, expensePolicy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(false, 'expensePolicy'), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBe('expensePolicy'); | ||
| expect(result.current.policyForMovingExpenses?.id).toBe('expensePolicy'); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return active policy when expensePolicyID not provided and active policy is valid', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| await Onyx.set(ONYXKEYS.NVP_ACTIVE_POLICY_ID, 'activePolicy'); | ||
| const activePolicy = createValidPolicyForMovingExpenses('activePolicy'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBe('activePolicy'); | ||
| expect(result.current.policyForMovingExpenses?.id).toBe('activePolicy'); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should not return policy with pendingAction DELETE', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('policy1', { | ||
| pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, | ||
| }); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should not return personal policy as valid for moving expenses', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('policy1', { | ||
| type: CONST.POLICY.TYPE.PERSONAL, | ||
| }); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return single policy when only one of two policies is valid', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const validPolicy = createValidPolicyForMovingExpenses('valid'); | ||
| const invalidPolicy = createValidPolicyForMovingExpenses('invalid', { | ||
| type: CONST.POLICY.TYPE.PERSONAL, | ||
| }); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${validPolicy.id}`, validPolicy); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${invalidPolicy.id}`, invalidPolicy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBe('valid'); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
|
|
||
| it('should return undefined when expensePolicyID is provided but policy is invalid', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('policy1', { | ||
| type: CONST.POLICY.TYPE.PERSONAL, | ||
| }); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(false, 'policy1'), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBeUndefined(); | ||
| expect(result.current.policyForMovingExpenses).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return policy when isTrackDistanceRequest is true but isMovingFromTrackExpense is true', async () => { | ||
| await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); | ||
| const policy = createValidPolicyForMovingExpenses('policy1'); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); | ||
| await waitForBatchedUpdates(); | ||
|
|
||
| const {result} = renderHook(() => usePolicyForMovingExpenses(false, undefined, true, true), { | ||
| wrapper: OnyxListItemProvider, | ||
| }); | ||
|
|
||
| expect(result.current.policyForMovingExpensesID).toBe('policy1'); | ||
| expect(result.current.shouldSelectPolicy).toBe(false); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.