Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mWeb - Selector - User lands on the same WS when double tapping on different one on selector. #52438

Merged
merged 19 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
};
});
Comment on lines +21 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not use the actualNav and just return useIsFocused

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried removing the actualNav and we can't remove it as causes issues for other missing methods in nav.

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();
});
});
Loading