|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +import {NavigationContainer} from '@react-navigation/native'; |
| 3 | +import {act, fireEvent, render, screen} from '@testing-library/react-native'; |
| 4 | +import React from 'react'; |
| 5 | +import Onyx from 'react-native-onyx'; |
| 6 | +import {WRITE_COMMANDS} from '@libs/API/types'; |
| 7 | +import * as SequentialQueue from '@libs/Network/SequentialQueue'; |
| 8 | +import createResponsiveStackNavigator from '@navigation/AppNavigator/createResponsiveStackNavigator'; |
| 9 | +import type {SettingsNavigatorParamList} from '@navigation/types'; |
| 10 | +import WorkspaceUpgradePage from '@pages/workspace/upgrade/WorkspaceUpgradePage'; |
| 11 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 12 | +import SCREENS from '@src/SCREENS'; |
| 13 | +import type {Policy} from '@src/types/onyx'; |
| 14 | +import * as LHNTestUtils from '../utils/LHNTestUtils'; |
| 15 | +import * as TestHelper from '../utils/TestHelper'; |
| 16 | +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; |
| 17 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 18 | + |
| 19 | +TestHelper.setupGlobalFetchMock(); |
| 20 | + |
| 21 | +const RootStack = createResponsiveStackNavigator<SettingsNavigatorParamList>(); |
| 22 | + |
| 23 | +const renderPage = (initialRouteName: typeof SCREENS.WORKSPACE.UPGRADE, initialParams: SettingsNavigatorParamList[typeof SCREENS.WORKSPACE.UPGRADE]) => { |
| 24 | + return render( |
| 25 | + <NavigationContainer> |
| 26 | + <RootStack.Navigator initialRouteName={initialRouteName}> |
| 27 | + <RootStack.Screen |
| 28 | + name={SCREENS.WORKSPACE.UPGRADE} |
| 29 | + component={WorkspaceUpgradePage} |
| 30 | + initialParams={initialParams} |
| 31 | + /> |
| 32 | + </RootStack.Navigator> |
| 33 | + </NavigationContainer>, |
| 34 | + ); |
| 35 | +}; |
| 36 | + |
| 37 | +describe('WorkspaceUpgrade', () => { |
| 38 | + beforeAll(() => { |
| 39 | + Onyx.init({ |
| 40 | + keys: ONYXKEYS, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(async () => { |
| 45 | + await SequentialQueue.waitForIdle(); |
| 46 | + await act(async () => { |
| 47 | + await Onyx.clear(); |
| 48 | + }); |
| 49 | + |
| 50 | + jest.clearAllMocks(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should enable policy rules', async () => { |
| 54 | + const policy: Policy = LHNTestUtils.getFakePolicy(); |
| 55 | + |
| 56 | + // Given that a policy is initialized in Onyx |
| 57 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); |
| 58 | + |
| 59 | + // And WorkspaceUpgradePage for rules is opened |
| 60 | + const {unmount} = renderPage(SCREENS.WORKSPACE.UPGRADE, {policyID: policy.id, featureName: 'rules'}); |
| 61 | + |
| 62 | + // When the policy is upgraded by clicking on the Upgrade button |
| 63 | + fireEvent.press(screen.getByTestId('upgrade-button')); |
| 64 | + await waitForBatchedUpdatesWithAct(); |
| 65 | + |
| 66 | + // Then "Upgrade to Corporate" API request should be made |
| 67 | + TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.UPGRADE_TO_CORPORATE, 1); |
| 68 | + |
| 69 | + // When WorkspaceUpgradePage is unmounted |
| 70 | + unmount(); |
| 71 | + await waitForBatchedUpdates(); |
| 72 | + |
| 73 | + // Then "Set policy rules enabled" API request should be made |
| 74 | + TestHelper.expectAPICommandToHaveBeenCalled(WRITE_COMMANDS.SET_POLICY_RULES_ENABLED, 1); |
| 75 | + }); |
| 76 | +}); |
0 commit comments