Skip to content

Commit ba7815c

Browse files
test: add unit tests for OptionListContextProvider to validate locale handling and options initialization
1 parent 34a7886 commit ba7815c

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import {act, renderHook} from '@testing-library/react-native';
2+
import React from 'react';
3+
import {usePersonalDetails} from '@components/OnyxListItemProvider';
4+
import OptionListContextProvider, {useOptionsList} from '@components/OptionListContextProvider';
5+
import useOnyx from '@hooks/useOnyx';
6+
import {createOptionList} from '@libs/OptionsListUtils';
7+
import type * as OptionsListUtilsModule from '@libs/OptionsListUtils';
8+
import ONYXKEYS from '@src/ONYXKEYS';
9+
10+
jest.mock('@libs/OptionsListUtils', () => {
11+
const actual = jest.requireActual<typeof OptionsListUtilsModule>('@libs/OptionsListUtils');
12+
return {
13+
...actual,
14+
createOptionList: jest.fn(),
15+
processReport: jest.fn(() => ({reportOption: null})),
16+
createOptionFromReport: jest.fn(),
17+
};
18+
});
19+
20+
jest.mock('@hooks/useOnyx', () => jest.fn());
21+
jest.mock('@components/OnyxListItemProvider', () => ({
22+
usePersonalDetails: jest.fn(),
23+
}));
24+
25+
const mockCreateOptionList = createOptionList as jest.MockedFunction<typeof createOptionList>;
26+
const mockUseOnyx = useOnyx as jest.MockedFunction<typeof useOnyx>;
27+
const mockUsePersonalDetails = usePersonalDetails as jest.MockedFunction<typeof usePersonalDetails>;
28+
29+
const wrapper: React.FC<{children: React.ReactNode}> = ({children}) => <OptionListContextProvider>{children}</OptionListContextProvider>;
30+
31+
describe('OptionListContextProvider locale guard', () => {
32+
let onyxState: Record<string, unknown>;
33+
let onyxSourceValues: Record<string, unknown>;
34+
35+
beforeEach(() => {
36+
jest.clearAllMocks();
37+
38+
onyxState = {
39+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: {locale: 'en'},
40+
[ONYXKEYS.COLLECTION.REPORT]: {},
41+
};
42+
43+
onyxSourceValues = {
44+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: onyxState[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES],
45+
[ONYXKEYS.COLLECTION.REPORT]: {},
46+
[ONYXKEYS.COLLECTION.REPORT_ACTIONS]: {},
47+
};
48+
49+
mockCreateOptionList.mockReturnValue({reports: [], personalDetails: []});
50+
mockUsePersonalDetails.mockReturnValue({});
51+
52+
mockUseOnyx.mockImplementation(((key: string) => {
53+
if (key === ONYXKEYS.DERIVED.REPORT_ATTRIBUTES) {
54+
return [onyxState[key], {sourceValue: onyxSourceValues[key]}];
55+
}
56+
57+
if (key === ONYXKEYS.COLLECTION.REPORT) {
58+
return [onyxState[key], {sourceValue: onyxSourceValues[key]}];
59+
}
60+
61+
if (key === ONYXKEYS.COLLECTION.REPORT_ACTIONS) {
62+
return [undefined, {sourceValue: onyxSourceValues[key]}];
63+
}
64+
65+
return [undefined];
66+
}) as typeof useOnyx);
67+
});
68+
69+
it('ignores locale changes before options are initialized', () => {
70+
const {rerender} = renderHook(({shouldInitialize}) => useOptionsList({shouldInitialize}), {
71+
initialProps: {shouldInitialize: false},
72+
wrapper,
73+
});
74+
75+
expect(mockCreateOptionList).not.toHaveBeenCalled();
76+
77+
onyxState = {
78+
...onyxState,
79+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: {locale: 'es'},
80+
};
81+
onyxSourceValues = {
82+
...onyxSourceValues,
83+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: onyxState[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES],
84+
};
85+
rerender({shouldInitialize: false});
86+
87+
expect(mockCreateOptionList).not.toHaveBeenCalled();
88+
});
89+
90+
it('refreshes options once after initialization when locale changes', () => {
91+
const {result, rerender} = renderHook(({shouldInitialize}) => useOptionsList({shouldInitialize}), {
92+
initialProps: {shouldInitialize: false},
93+
wrapper,
94+
});
95+
96+
act(() => {
97+
result.current.initializeOptions();
98+
});
99+
100+
expect(mockCreateOptionList).toHaveBeenCalledTimes(1);
101+
102+
mockCreateOptionList.mockClear();
103+
104+
onyxState = {
105+
...onyxState,
106+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: {locale: 'fr'},
107+
};
108+
onyxSourceValues = {
109+
...onyxSourceValues,
110+
[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES]: onyxState[ONYXKEYS.DERIVED.REPORT_ATTRIBUTES],
111+
};
112+
rerender({shouldInitialize: false});
113+
114+
expect(mockCreateOptionList).toHaveBeenCalledTimes(1);
115+
});
116+
});

0 commit comments

Comments
 (0)