Skip to content

Commit 3e83f2f

Browse files
committed
update test
1 parent ef5cf66 commit 3e83f2f

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

tests/unit/OptionListContextProviderTest.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import React from 'react';
33
import {usePersonalDetails} from '@components/OnyxListItemProvider';
44
import OptionListContextProvider, {useOptionsList} from '@components/OptionListContextProvider';
55
import useOnyx from '@hooks/useOnyx';
6-
import {createOptionList} from '@libs/OptionsListUtils';
6+
import usePrivateIsArchivedMap from '@hooks/usePrivateIsArchivedMap';
7+
import type {OptionList, SearchOption} from '@libs/OptionsListUtils';
8+
import {createOptionFromReport, createOptionList} from '@libs/OptionsListUtils';
79
import ONYXKEYS from '@src/ONYXKEYS';
10+
import type {Report} from '@src/types/onyx';
811

912
jest.mock('@libs/OptionsListUtils', () => {
1013
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -19,13 +22,16 @@ jest.mock('@libs/OptionsListUtils', () => {
1922
});
2023

2124
jest.mock('@hooks/useOnyx', () => jest.fn());
25+
jest.mock('@hooks/usePrivateIsArchivedMap', () => jest.fn());
2226
jest.mock('@components/OnyxListItemProvider', () => ({
2327
usePersonalDetails: jest.fn(),
2428
}));
2529

2630
const mockCreateOptionList = createOptionList as jest.MockedFunction<typeof createOptionList>;
31+
const mockCreateOptionFromReport = createOptionFromReport as jest.MockedFunction<typeof createOptionFromReport>;
2732
const mockUseOnyx = useOnyx as jest.MockedFunction<typeof useOnyx>;
2833
const mockUsePersonalDetails = usePersonalDetails as jest.MockedFunction<typeof usePersonalDetails>;
34+
const mockUsePrivateIsArchivedMap = usePrivateIsArchivedMap as jest.MockedFunction<typeof usePrivateIsArchivedMap>;
2935

3036
const wrapper: React.FC<{children: React.ReactNode}> = ({children}) => <OptionListContextProvider>{children}</OptionListContextProvider>;
3137

@@ -49,6 +55,7 @@ describe('OptionListContextProvider', () => {
4955

5056
mockCreateOptionList.mockReturnValue({reports: [], personalDetails: []});
5157
mockUsePersonalDetails.mockReturnValue({});
58+
mockUsePrivateIsArchivedMap.mockReturnValue({});
5259

5360
mockUseOnyx.mockImplementation(((key: string) => {
5461
if (key === ONYXKEYS.DERIVED.REPORT_ATTRIBUTES) {
@@ -114,4 +121,54 @@ describe('OptionListContextProvider', () => {
114121

115122
expect(mockCreateOptionList).toHaveBeenCalledTimes(1);
116123
});
124+
125+
it('passes privateIsArchived to createOptionFromReport when personal details change', () => {
126+
const reportID = '1';
127+
const accountID = '12345';
128+
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
129+
const report = {
130+
reportID,
131+
participants: {[accountID]: {notificationPreference: 'always'}},
132+
};
133+
134+
const archivedKey = `${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${reportID}`;
135+
mockUsePrivateIsArchivedMap.mockReturnValue({[archivedKey]: 'true'});
136+
137+
const initialPersonalDetails = {[accountID]: {accountID: Number(accountID), firstName: 'John', lastName: 'Doe', login: '[email protected]', displayName: 'John Doe'}};
138+
mockUsePersonalDetails.mockReturnValue(initialPersonalDetails);
139+
140+
onyxState = {
141+
...onyxState,
142+
[ONYXKEYS.COLLECTION.REPORT]: {[reportKey]: report},
143+
};
144+
onyxSourceValues = {
145+
...onyxSourceValues,
146+
[ONYXKEYS.COLLECTION.REPORT]: {[reportKey]: report},
147+
};
148+
149+
const mockReportOption = {reportID, item: report, text: 'John Doe', keyForList: reportID} as unknown as SearchOption<Report>;
150+
mockCreateOptionList.mockReturnValue({
151+
reports: [mockReportOption],
152+
personalDetails: [],
153+
} as OptionList);
154+
mockCreateOptionFromReport.mockReturnValue(mockReportOption);
155+
156+
const {result, rerender} = renderHook(({shouldInitialize}) => useOptionsList({shouldInitialize}), {
157+
initialProps: {shouldInitialize: false},
158+
wrapper,
159+
});
160+
161+
act(() => {
162+
result.current.initializeOptions();
163+
});
164+
165+
mockCreateOptionFromReport.mockClear();
166+
167+
// Change personal details to trigger the update effect
168+
const updatedPersonalDetails = {[accountID]: {accountID: Number(accountID), firstName: 'Jane', lastName: 'Doe', login: '[email protected]', displayName: 'Jane Doe'}};
169+
mockUsePersonalDetails.mockReturnValue(updatedPersonalDetails);
170+
rerender({shouldInitialize: false});
171+
172+
expect(mockCreateOptionFromReport).toHaveBeenCalledWith(report, updatedPersonalDetails, expect.any(Number), 'true', undefined, {showPersonalDetails: true});
173+
});
117174
});

0 commit comments

Comments
 (0)