From 6342752f256d47195ef15b3866c58ea3fcae5ee8 Mon Sep 17 00:00:00 2001 From: mheggelund Date: Wed, 6 Mar 2024 09:55:15 +0100 Subject: [PATCH] chore: temp --- package.json | 1 + src/App.test.tsx | 44 ++++++++- .../AreaCoordinates/AreaCoordinates.tsx | 6 +- .../tests/AreaCoordinates.test.tsx | 42 +++++++++ src/pages/Browse/Browse.test.tsx | 94 +++++++++++++++++++ 5 files changed, 185 insertions(+), 2 deletions(-) create mode 100644 src/components/AreaCoordinates/tests/AreaCoordinates.test.tsx create mode 100644 src/pages/Browse/Browse.test.tsx diff --git a/package.json b/package.json index ed417c24..57cbaad4 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "axios": "^1.6.0", "env-cmd": "^10.1.0", "lodash": "^4.17.21", + "msal-react-tester": "^0.3.1", "openapi-fetch": "^0.7.2", "openapi-typescript-codegen": "^0.25.0", "react": "^18.2.0", diff --git a/src/App.test.tsx b/src/App.test.tsx index a0d135cb..78251b01 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -1,6 +1,48 @@ -import { render } from '@testing-library/react'; +import { cleanup, render, screen } from '@testing-library/react'; +import { MsalReactTester } from 'msal-react-tester'; import { App } from './App'; +let msalTester: MsalReactTester; +beforeEach(() => { + // new instance of msal tester for each test + msalTester = new MsalReactTester(); + + // spy all required msal things + msalTester.spyMsal(); +}); + +afterEach(() => { + cleanup(); + msalTester.resetSpyMsal(); +}); + test('renders without crashing', () => { render(); }); + +test('renders what we want', () => { + render(); + + expect(screen.getByText(/You are not authorized/i)).toBeInTheDocument(); +}); + +/** + * + +test('renders what we want when logged in', async () => { + msalTester.isLogged(); + + render( + + + + + , + ); + await msalTester.waitForRedirect(); + + expect( + screen.getByRole('h1', { name: /Browse all models/i }), + ).toBeInTheDocument(); +}); + */ diff --git a/src/components/AreaCoordinates/AreaCoordinates.tsx b/src/components/AreaCoordinates/AreaCoordinates.tsx index f1094b4a..6672dc7f 100644 --- a/src/components/AreaCoordinates/AreaCoordinates.tsx +++ b/src/components/AreaCoordinates/AreaCoordinates.tsx @@ -353,7 +353,11 @@ export const AreaCoordinates = ({ ) : ( - )} diff --git a/src/components/AreaCoordinates/tests/AreaCoordinates.test.tsx b/src/components/AreaCoordinates/tests/AreaCoordinates.test.tsx new file mode 100644 index 00000000..48ca03a1 --- /dev/null +++ b/src/components/AreaCoordinates/tests/AreaCoordinates.test.tsx @@ -0,0 +1,42 @@ +import { cleanup } from '@testing-library/react'; + +// const mocArea: ListModelAreaTypesQueryResponse = { +// success: true, +// count: 1, +// data: [{ modelAreaTypeId: '111', name: 'mocArea', description: 'Test' }], +// }; + +// jest.mock('model-area', () => ({ +// useQuery: jest +// .fn() +// .mockReturnValue({ data: { ...mocArea }, isLoading: false, error: {} }), +// })); + +afterEach(() => cleanup()); + +// const wrapper = ({ children }: { children: any }) => ( +// +// {children} +// +// ); + +test('on render, area select is present.', () => { + // render( + // + // + // + // + // , + // ); + // Wait for the authentication redirect to resolve + // const { result } = renderHook(() => useFetchModelAreas(), { + // wrapper, + // }); + // await waitFor(() => expect(result.current.isSuccess).toBe(true)); + // const editButton = screen.getByRole('button', { name: /edit/i }); + // expect(editButton).toBeEnabled(); +}); + +// test('Should render a button', async () => { +// render(); +// }); diff --git a/src/pages/Browse/Browse.test.tsx b/src/pages/Browse/Browse.test.tsx new file mode 100644 index 00000000..c69acfe6 --- /dev/null +++ b/src/pages/Browse/Browse.test.tsx @@ -0,0 +1,94 @@ +/* + +import { MsalProvider } from '@azure/msal-react'; +import { QueryClientProvider } from '@tanstack/react-query'; +import { cleanup, render, screen } from '@testing-library/react'; +import { MsalReactTester } from 'msal-react-tester'; +import { PropsWithChildren } from 'react'; +import { MemoryRouter } from 'react-router-dom'; +import { queryClient } from '../../auth/queryClient'; +import { Browse } from './Browse'; + +let msalTester: MsalReactTester; +beforeEach(() => { + // new instance of msal tester for each test + msalTester = new MsalReactTester(); + + // spy all required msal things + msalTester.spyMsal(); +}); + +afterEach(() => { + cleanup(); + msalTester.resetSpyMsal(); +}); + +// function setup() { +// const utils = render( +// +// +// +// +// , +// { wrapper: BrowserRouter }, +// ); + +// const add = screen.getByRole('button'); +// const clickAdd = () => fireEvent.click(add); +// return { ...utils, add, clickAdd }; +// } + + +jest.mock('../../pages/Browse/Browse', () => ({ + __esModule: true, + HomePage: () =>
, +})); + +function wrapper({ children }: PropsWithChildren) { + const setRoute = '/'; + + return ( + + + {children} + + + ); +} + +test('should render main page', async () => { + msalTester.isLogged(); + render(, { wrapper }); + await msalTester.waitForRedirect(); + + expect( + screen.getByRole('h1', { name: /Browse all models/i }), + ).toBeInTheDocument(); +}); + +*/ + +// Er her for at fila skal kunne committes, bare fjern sammen med det utkommenterte over +import { cleanup, render, screen } from '@testing-library/react'; +import { MsalReactTester } from 'msal-react-tester'; +import { App } from '../../App'; + +let msalTester: MsalReactTester; +beforeEach(() => { + // new instance of msal tester for each test + msalTester = new MsalReactTester(); + + // spy all required msal things + msalTester.spyMsal(); +}); + +afterEach(() => { + cleanup(); + msalTester.resetSpyMsal(); +}); + +test('renders what we want', () => { + render(); + + expect(screen.getByText(/You are not authorized/i)).toBeInTheDocument(); +});