From 08ec922d119f6a6d4e1c061fa107b1b0e524fc23 Mon Sep 17 00:00:00 2001 From: Sahil Budhwar Date: Tue, 11 Jun 2024 10:18:37 +0530 Subject: [PATCH] fix(WorkspacedPage): add unit tests for Workspaced component --- src/pages/__tests__/WorkspacedPage.spec.tsx | 60 +++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/pages/__tests__/WorkspacedPage.spec.tsx diff --git a/src/pages/__tests__/WorkspacedPage.spec.tsx b/src/pages/__tests__/WorkspacedPage.spec.tsx new file mode 100644 index 000000000..ae84af7ad --- /dev/null +++ b/src/pages/__tests__/WorkspacedPage.spec.tsx @@ -0,0 +1,60 @@ +import * as React from 'react'; +import { useLocation, useParams } from 'react-router-dom'; +import { render, screen } from '@testing-library/react'; +import { useWorkspaceInfo } from '../../utils/workspace-context-utils'; +import WorkspacedPage from '../WorkspacedPage'; + +jest.mock('react-router-dom', () => { + const actual = jest.requireActual('react-router-dom'); + return { + ...actual, + useNavigate: () => () => {}, + useParams: jest.fn(), + useLocation: jest.fn(), + Navigate: ({ to }) =>

{to}

, + }; +}); + +jest.mock('../../utils/workspace-context-utils', () => ({ + useWorkspaceInfo: jest.fn(), +})); + +jest.mock('react', () => { + const actual = jest.requireActual('react'); + return { + ...actual, + useContext: jest.fn(), + }; +}); + +const mockUseParams = useParams as jest.Mock; +const mockUseLocation = useLocation as jest.Mock; +const mockUseWorkspaceInfo = useWorkspaceInfo as jest.Mock; +const mockUseContext = React.useContext as jest.Mock; +const getMockPathname = () => `/application-pipeline`; + +describe('WorkspacedPage', () => { + it('should redirect user to a url', () => { + mockUseLocation.mockReturnValue({ pathname: getMockPathname() }); + mockUseParams.mockReturnValue({ + workspaceName: 'mock-workspace', + }); + mockUseWorkspaceInfo.mockReturnValue({ workspace: 'mock-workspace' }); + mockUseContext.mockReturnValue({}); + render(); + const navigate = screen.getByRole('navigate'); + expect(navigate.innerHTML).toEqual('/application-pipeline/workspaces/mock-workspace'); + }); + + it('should use lastUsedWorkspace in case workspace is not found', () => { + mockUseLocation.mockReturnValue({ pathname: getMockPathname() }); + mockUseParams.mockReturnValue({ + workspaceName: undefined, + }); + mockUseWorkspaceInfo.mockReturnValue({ workspace: undefined }); + mockUseContext.mockReturnValue({ lastUsedWorkspace: 'mock-last-used-workspace' }); + render(); + const navigate = screen.getByRole('navigate'); + expect(navigate.innerHTML).toEqual('/application-pipeline/workspaces/mock-last-used-workspace'); + }); +});