|
| 1 | +import {PortalProvider} from '@gorhom/portal'; |
| 2 | +import {NavigationContainer} from '@react-navigation/native'; |
| 3 | +import {act, fireEvent, render, screen, waitFor} from '@testing-library/react-native'; |
| 4 | +import React from 'react'; |
| 5 | +import Onyx from 'react-native-onyx'; |
| 6 | +import ComposeProviders from '@components/ComposeProviders'; |
| 7 | +import {LocaleContextProvider} from '@components/LocaleContextProvider'; |
| 8 | +import OnyxProvider from '@components/OnyxProvider'; |
| 9 | +import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID'; |
| 10 | +import * as useResponsiveLayoutModule from '@hooks/useResponsiveLayout'; |
| 11 | +import type ResponsiveLayoutResult from '@hooks/useResponsiveLayout/types'; |
| 12 | +import {translateLocal} from '@libs/Localize'; |
| 13 | +import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator'; |
| 14 | +import type {WorkspaceSplitNavigatorParamList} from '@navigation/types'; |
| 15 | +import WorkspaceTagsPage from '@pages/workspace/tags/WorkspaceTagsPage'; |
| 16 | +import CONST from '@src/CONST'; |
| 17 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 18 | +import SCREENS from '@src/SCREENS'; |
| 19 | +import * as LHNTestUtils from '../utils/LHNTestUtils'; |
| 20 | +import * as TestHelper from '../utils/TestHelper'; |
| 21 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 22 | + |
| 23 | +TestHelper.setupGlobalFetchMock(); |
| 24 | + |
| 25 | +const Stack = createPlatformStackNavigator<WorkspaceSplitNavigatorParamList>(); |
| 26 | + |
| 27 | +const renderPage = (initialRouteName: typeof SCREENS.WORKSPACE.TAGS, initialParams: WorkspaceSplitNavigatorParamList[typeof SCREENS.WORKSPACE.TAGS]) => { |
| 28 | + return render( |
| 29 | + <ComposeProviders components={[OnyxProvider, LocaleContextProvider, CurrentReportIDContextProvider]}> |
| 30 | + <PortalProvider> |
| 31 | + <NavigationContainer> |
| 32 | + <Stack.Navigator initialRouteName={initialRouteName}> |
| 33 | + <Stack.Screen |
| 34 | + name={SCREENS.WORKSPACE.TAGS} |
| 35 | + component={WorkspaceTagsPage} |
| 36 | + initialParams={initialParams} |
| 37 | + /> |
| 38 | + </Stack.Navigator> |
| 39 | + </NavigationContainer> |
| 40 | + </PortalProvider> |
| 41 | + </ComposeProviders>, |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +describe('WorkspaceTags', () => { |
| 46 | + const FIRST_TAG = 'Tag One'; |
| 47 | + const SECOND_TAG = 'Tag Two'; |
| 48 | + |
| 49 | + beforeAll(() => { |
| 50 | + Onyx.init({ |
| 51 | + keys: ONYXKEYS, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + beforeEach(() => { |
| 56 | + jest.spyOn(useResponsiveLayoutModule, 'default').mockReturnValue({ |
| 57 | + isSmallScreenWidth: true, |
| 58 | + shouldUseNarrowLayout: true, |
| 59 | + } as ResponsiveLayoutResult); |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(async () => { |
| 63 | + await act(async () => { |
| 64 | + await Onyx.clear(); |
| 65 | + }); |
| 66 | + jest.clearAllMocks(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should show select option when the item is not selected and deselect option when the item is selected', async () => { |
| 70 | + await TestHelper.signInWithTestUser(); |
| 71 | + |
| 72 | + const policy = { |
| 73 | + ...LHNTestUtils.getFakePolicy(), |
| 74 | + role: CONST.POLICY.ROLE.ADMIN, |
| 75 | + areTagsEnabled: true, |
| 76 | + requiresTag: true, |
| 77 | + }; |
| 78 | + |
| 79 | + const tags = { |
| 80 | + TagListOne: { |
| 81 | + name: 'TagListOne', |
| 82 | + required: true, |
| 83 | + orderWeight: 1, |
| 84 | + tags: { |
| 85 | + [FIRST_TAG]: { |
| 86 | + name: FIRST_TAG, |
| 87 | + enabled: true, |
| 88 | + }, |
| 89 | + [SECOND_TAG]: { |
| 90 | + name: SECOND_TAG, |
| 91 | + enabled: true, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }; |
| 96 | + |
| 97 | + await act(async () => { |
| 98 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); |
| 99 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policy.id}`, tags); |
| 100 | + }); |
| 101 | + |
| 102 | + const {unmount} = renderPage(SCREENS.WORKSPACE.TAGS, {policyID: policy.id}); |
| 103 | + |
| 104 | + await waitForBatchedUpdatesWithAct(); |
| 105 | + |
| 106 | + await waitFor(() => { |
| 107 | + expect(screen.getByText(FIRST_TAG)).toBeOnTheScreen(); |
| 108 | + }); |
| 109 | + await waitFor(() => { |
| 110 | + expect(screen.getByText(SECOND_TAG)).toBeOnTheScreen(); |
| 111 | + }); |
| 112 | + |
| 113 | + // Long press on the first tag to trigger the select action |
| 114 | + |
| 115 | + fireEvent(screen.getByTestId(`base-list-item-Tag One`), 'onLongPress'); |
| 116 | + |
| 117 | + await waitForBatchedUpdatesWithAct(); |
| 118 | + |
| 119 | + // Wait for the "Select" option to appear |
| 120 | + await waitFor(() => { |
| 121 | + expect(screen.getByText(translateLocal('common.select'))).toBeOnTheScreen(); |
| 122 | + }); |
| 123 | + |
| 124 | + // Find and click the "Select" menu item. Using getByText, since testID is not reliable here |
| 125 | + const selectMenuItem = screen.getByText(translateLocal('common.select')); |
| 126 | + expect(selectMenuItem).toBeOnTheScreen(); |
| 127 | + |
| 128 | + // Create a mock event object that matches GestureResponderEvent. Needed for onPress in MenuItem to be called |
| 129 | + const mockEvent = { |
| 130 | + nativeEvent: {}, |
| 131 | + type: 'press', |
| 132 | + target: selectMenuItem, |
| 133 | + currentTarget: selectMenuItem, |
| 134 | + }; |
| 135 | + fireEvent.press(selectMenuItem, mockEvent); |
| 136 | + |
| 137 | + await waitForBatchedUpdatesWithAct(); |
| 138 | + |
| 139 | + // Long press again on the second tag to trigger the deselect action |
| 140 | + fireEvent(screen.getByTestId('base-list-item-Tag One'), 'onLongPress'); |
| 141 | + await waitForBatchedUpdatesWithAct(); |
| 142 | + |
| 143 | + // Wait for the "Deselect" option to appear |
| 144 | + await waitFor(() => { |
| 145 | + expect(screen.getByText(translateLocal('common.deselect'))).toBeOnTheScreen(); |
| 146 | + }); |
| 147 | + |
| 148 | + unmount(); |
| 149 | + await waitForBatchedUpdatesWithAct(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments