Skip to content

Commit

Permalink
Merge pull request #52438 from Krishna2323/krishna2323/issue/51402
Browse files Browse the repository at this point in the history
  • Loading branch information
blimpich authored Dec 9, 2024
2 parents 6368c96 + 793b688 commit dc07fc6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/components/WorkspaceSwitcherButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function WorkspaceSwitcherButton({policy, onSwitchWorkspace}: WorkspaceSwitcherB
accessibilityRole={CONST.ROLE.BUTTON}
accessibilityLabel={translate('common.workspaces')}
accessible
testID="WorkspaceSwitcherButton"
onPress={() => {
onSwitchWorkspace?.();
pressableRef?.current?.blur();
Expand Down
7 changes: 6 additions & 1 deletion src/pages/WorkspaceSwitcherPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {useIsFocused} from '@react-navigation/native';
import React, {useCallback, useMemo} from 'react';
import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
Expand Down Expand Up @@ -35,6 +36,7 @@ function WorkspaceSwitcherPage() {
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const {translate} = useLocalize();
const {activeWorkspaceID, setActiveWorkspaceID} = useActiveWorkspace();
const isFocused = useIsFocused();

const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [reportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS);
Expand Down Expand Up @@ -77,6 +79,9 @@ function WorkspaceSwitcherPage() {

const selectPolicy = useCallback(
(policyID?: string) => {
if (!isFocused) {
return;
}
const newPolicyID = policyID === activeWorkspaceID ? undefined : policyID;

setActiveWorkspaceID(newPolicyID);
Expand All @@ -85,7 +90,7 @@ function WorkspaceSwitcherPage() {
Navigation.navigateWithSwitchPolicyID({policyID: newPolicyID});
}
},
[activeWorkspaceID, setActiveWorkspaceID],
[activeWorkspaceID, setActiveWorkspaceID, isFocused],
);

const usersWorkspaces = useMemo<WorkspaceListItem[]>(() => {
Expand Down
104 changes: 104 additions & 0 deletions tests/ui/WorkspaceSwitcherTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import * as NativeNavigation from '@react-navigation/native';
import {act, fireEvent, render, screen} from '@testing-library/react-native';
import React from 'react';
import Onyx from 'react-native-onyx';
import * as Localize from '@libs/Localize';
import type Navigation from '@libs/Navigation/Navigation';
import * as AppActions from '@userActions/App';
import * as User from '@userActions/User';
import App from '@src/App';
import ONYXKEYS from '@src/ONYXKEYS';
import type {NativeNavigationMock} from '../../__mocks__/@react-navigation/native';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import PusherHelper from '../utils/PusherHelper';
import * as TestHelper from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

// We need a large timeout here as we are lazy loading React Navigation screens and this test is running against the entire mounted App
jest.setTimeout(60000);

jest.mock('@react-navigation/native', () => {
const actualNav = jest.requireActual<typeof Navigation>('@react-navigation/native');
return {
...actualNav,
useIsFocused: jest.fn(),
triggerTransitionEnd: jest.fn(),
};
});
TestHelper.setupApp();

async function signInAndGetApp(): Promise<void> {
// Render the App and sign in as a test user.
render(<App />);
await waitForBatchedUpdatesWithAct();

const hintText = Localize.translateLocal('loginForm.loginForm');
const loginForm = await screen.findAllByLabelText(hintText);
expect(loginForm).toHaveLength(1);

await act(async () => {
await TestHelper.signInWithTestUser();
});
await waitForBatchedUpdatesWithAct();

User.subscribeToUserEvents();
await waitForBatchedUpdates();

AppActions.setSidebarLoaded();

await waitForBatchedUpdatesWithAct();
}

async function navigateToWorkspaceSwitcher(): Promise<void> {
const workspaceSwitcherButton = await screen.findByTestId('WorkspaceSwitcherButton');
fireEvent(workspaceSwitcherButton, 'press');
await act(() => {
(NativeNavigation as NativeNavigationMock).triggerTransitionEnd();
});
await waitForBatchedUpdatesWithAct();
}

describe('WorkspaceSwitcherPage', () => {
beforeEach(() => {
jest.clearAllMocks();
Onyx.clear();

// Unsubscribe to pusher channels
PusherHelper.teardown();
});

it('navigates away when a workspace is selected and `isFocused` is true', async () => {
await signInAndGetApp();
(NativeNavigation.useIsFocused as jest.Mock).mockReturnValue(true);

await Onyx.mergeCollection(ONYXKEYS.COLLECTION.POLICY, {
[`${ONYXKEYS.COLLECTION.POLICY}1` as const]: LHNTestUtils.getFakePolicy('1', 'Workspace A'),
[`${ONYXKEYS.COLLECTION.POLICY}2` as const]: LHNTestUtils.getFakePolicy('2', 'Workspace B'),
[`${ONYXKEYS.COLLECTION.POLICY}3` as const]: LHNTestUtils.getFakePolicy('3', 'Workspace C'),
});

await navigateToWorkspaceSwitcher();

const workspaceRowB = screen.getByLabelText('Workspace B');
fireEvent.press(workspaceRowB);
expect(screen.queryByTestId('WorkspaceSwitcherPage')).toBeNull();
});

it('does not navigate away when a workspace is selected and `isFocused` is false', async () => {
await signInAndGetApp();
(NativeNavigation.useIsFocused as jest.Mock).mockReturnValue(false);

await Onyx.mergeCollection(ONYXKEYS.COLLECTION.POLICY, {
[`${ONYXKEYS.COLLECTION.POLICY}1` as const]: LHNTestUtils.getFakePolicy('1', 'Workspace A'),
[`${ONYXKEYS.COLLECTION.POLICY}2` as const]: LHNTestUtils.getFakePolicy('2', 'Workspace B'),
[`${ONYXKEYS.COLLECTION.POLICY}3` as const]: LHNTestUtils.getFakePolicy('3', 'Workspace C'),
});

await navigateToWorkspaceSwitcher();

const workspaceRowB = screen.getByLabelText('Workspace B');
fireEvent.press(workspaceRowB);
expect(screen.getByTestId('WorkspaceSwitcherPage')).toBeOnTheScreen();
});
});

0 comments on commit dc07fc6

Please sign in to comment.