From d5be5258af363eab91d63806d98294477e20b739 Mon Sep 17 00:00:00 2001 From: Harshil Sharma Date: Wed, 3 Nov 2021 10:29:17 +0530 Subject: [PATCH] Prevented workspace switcher from settiong dashboard as the last visited workspace (#1676) * Prevented workspace switcher from settiong dashboard as the last visited workspace ID * Fixed tests --- .../workspaceSwitcher.test.tsx.snap | 325 +++++++++--------- .../workspaceSwitcher.test.tsx | 92 ++++- .../workspaceSwitcher/workspaceSwitcher.tsx | 3 +- 3 files changed, 237 insertions(+), 183 deletions(-) diff --git a/webapp/src/components/workspaceSwitcher/__snapshots__/workspaceSwitcher.test.tsx.snap b/webapp/src/components/workspaceSwitcher/__snapshots__/workspaceSwitcher.test.tsx.snap index b6f03dcfa..207fd786d 100644 --- a/webapp/src/components/workspaceSwitcher/__snapshots__/workspaceSwitcher.test.tsx.snap +++ b/webapp/src/components/workspaceSwitcher/__snapshots__/workspaceSwitcher.test.tsx.snap @@ -3,117 +3,17 @@ exports[`components/workspaceSwitcher/WorkspaceSwitcher 2 more workspaces available 1`] = `
- - - - 3 results available. Use Up and Down to choose options, press Enter to select the currently focused option, press Escape to exit the menu. + + Dashboard - -
-
-
- Search... -
-
-
- -
-
-
-
-
- -
-
-
-
-
-
- Dashboard -
-
-
-
- Workspace 2 -
-
- 2 Boards -
-
-
-
- Workspace 3 -
-
- 3 Boards -
-
-
+
@@ -122,82 +22,163 @@ exports[`components/workspaceSwitcher/WorkspaceSwitcher 2 more workspaces availa exports[`components/workspaceSwitcher/WorkspaceSwitcher no more workspaces available 1`] = `
- - - - 1 result available. Use Up and Down to choose options, press Enter to select the currently focused option, press Escape to exit the menu. - -
-
-
- Search... -
-
-
- -
-
-
-
-
- -
+ + Dashboard + + +
+
+
+`; + +exports[`components/workspaceSwitcher/WorkspaceSwitcher open menu 1`] = ` +
+
+
+ + Dashboard + +
+ + + + 4 results available. Use Up and Down to choose options, press Enter to select the currently focused option, press Escape to exit the menu. + +
- Dashboard + Search... +
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
+ Dashboard +
+
+
+
+ Workspace 1 +
+
+ 1 Board +
+
+
+
+ Workspace 2 +
+
+ 2 Boards +
+
+
+
+ Workspace 3 +
+
+ 3 Boards +
diff --git a/webapp/src/components/workspaceSwitcher/workspaceSwitcher.test.tsx b/webapp/src/components/workspaceSwitcher/workspaceSwitcher.test.tsx index f5731b66f..31b093828 100644 --- a/webapp/src/components/workspaceSwitcher/workspaceSwitcher.test.tsx +++ b/webapp/src/components/workspaceSwitcher/workspaceSwitcher.test.tsx @@ -7,11 +7,19 @@ import {Provider as ReduxProvider} from 'react-redux' import {render} from '@testing-library/react' +import userEvent from '@testing-library/user-event' + +import {createMemoryHistory} from 'history' + +import {Router} from 'react-router-dom' + import {wrapIntl} from '../../testUtils' import {UserWorkspace} from '../../user' -import WorkspaceOptions from './workspaceOptions' +import {UserSettings} from '../../userSettings' + +import WorkspaceSwitcher from './workspaceSwitcher' describe('components/workspaceSwitcher/WorkspaceSwitcher', () => { const mockStore = configureStore([]) @@ -43,10 +51,7 @@ describe('components/workspaceSwitcher/WorkspaceSwitcher', () => { const component = wrapIntl( - {}} - activeWorkspaceId={workspace1.id} - /> + , ) @@ -63,14 +68,83 @@ describe('components/workspaceSwitcher/WorkspaceSwitcher', () => { const component = wrapIntl( - {}} - activeWorkspaceId={workspace1.id} - /> + , ) const {container} = render(component) expect(container).toMatchSnapshot() }) + + test('open menu', () => { + const store = mockStore({ + workspace: { + userWorkspaces: new Array(workspace1, workspace2, workspace3), + }, + }) + + const component = wrapIntl( + + + , + ) + + const {container} = render(component) + const switcher = container.querySelector('.WorkspaceSwitcher') + + expect(switcher).toBeDefined() + expect(switcher).not.toBeNull() + + userEvent.click(switcher as Element) + expect(container).toMatchSnapshot() + }) + + test('switch workspaces', () => { + const store = mockStore({ + workspace: { + userWorkspaces: new Array(workspace1, workspace2, workspace3), + }, + }) + const history = createMemoryHistory() + history.push = jest.fn() + + const component = wrapIntl( + + + + + , + ) + + const {container} = render(component) + const switcher = container.querySelector('.WorkspaceSwitcher') + expect(switcher).toBeDefined() + expect(switcher).not.toBeNull() + + userEvent.click(switcher as Element) + const workspace2Option = container.querySelector('.WorkspaceOptions__menu-list > div:nth-child(3)') + expect(workspace2Option).toBeDefined() + expect(workspace2Option).not.toBeNull() + userEvent.click(workspace2Option as Element) + expect(history.push).toBeCalledWith('/workspace/workspace_2') + expect(UserSettings.lastWorkspaceId).toBe('workspace_2') + + userEvent.click(switcher as Element) + const workspace3Option = container.querySelector('.WorkspaceOptions__menu-list > div:nth-child(4)') + expect(workspace3Option).toBeDefined() + expect(workspace3Option).not.toBeNull() + userEvent.click(workspace3Option as Element) + expect(history.push).toBeCalledWith('/workspace/workspace_3') + expect(UserSettings.lastWorkspaceId).toBe('workspace_3') + + userEvent.click(switcher as Element) + const dashboardOption = container.querySelector('.WorkspaceOptions__menu-list > div:nth-child(1)') + expect(dashboardOption).toBeDefined() + expect(dashboardOption).not.toBeNull() + userEvent.click(dashboardOption as Element) + expect(history.push).toBeCalledWith('/dashboard') + + // last workspace ID should not have changed + expect(UserSettings.lastWorkspaceId).toBe('workspace_3') + }) }) diff --git a/webapp/src/components/workspaceSwitcher/workspaceSwitcher.tsx b/webapp/src/components/workspaceSwitcher/workspaceSwitcher.tsx index f3dede2eb..979e1680b 100644 --- a/webapp/src/components/workspaceSwitcher/workspaceSwitcher.tsx +++ b/webapp/src/components/workspaceSwitcher/workspaceSwitcher.tsx @@ -62,9 +62,8 @@ const WorkspaceSwitcher = (props: Props): JSX.Element => { newPath = '/dashboard' } else { newPath = `/workspace/${workspaceId}` + UserSettings.lastWorkspaceId = workspaceId } - - UserSettings.lastWorkspaceId = workspaceId history.push(newPath) }} />