Skip to content

Commit

Permalink
#1205 - fix unit tests for react 18 & OG compatibility changes
Browse files Browse the repository at this point in the history
  • Loading branch information
louise-davies committed Apr 26, 2023
1 parent b1c03f9 commit d139560
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 36 deletions.
25 changes: 17 additions & 8 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ describe('App', () => {
it('renders without crashing', () => {
const div = document.createElement('div');
const root = createRoot(div);
root.render(<App useSuspense={false} />);
root.unmount();
act(() => {
root.render(<App useSuspense={false} />);
});
act(() => {
root.unmount();
});
});

it('should show preloader when react-i18next is not ready', () => {
Expand Down Expand Up @@ -59,11 +63,14 @@ describe('App', () => {
order: 0,
},
};
document.dispatchEvent(
new CustomEvent('scigateway', {
detail: registerRouteAction,
})
);

act(() => {
document.dispatchEvent(
new CustomEvent('scigateway', {
detail: registerRouteAction,
})
);
});

// go to plugin page
await fireEvent.click(screen.getByRole('link', { name: 'Test plugin' }));
Expand All @@ -81,7 +88,9 @@ describe('App', () => {
})
);

jest.runOnlyPendingTimers();
act(() => {
jest.runOnlyPendingTimers();
});

await act(async () => {
await flushPromises();
Expand Down
4 changes: 3 additions & 1 deletion src/__snapshots__/example.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
exports[`Example component renders correctly 1`] = `
<DocumentFragment>
<div>
test notification
<div>
warning test notification
</div>
</div>
</DocumentFragment>
`;
6 changes: 3 additions & 3 deletions src/__snapshots__/pageContainer.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ exports[`PageContainer - Tests renders correctly 1`] = `
class="MuiDrawer-root MuiDrawer-docked css-ak80xd-MuiDrawer-docked"
>
<div
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-8bwi45-MuiPaper-root-MuiDrawer-paper"
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-1b4jzf5-MuiPaper-root-MuiDrawer-paper"
style="visibility: hidden; webkit-transform: translateX(-0px); transform: translateX(-0px);"
>
<div
Expand All @@ -192,7 +192,7 @@ exports[`PageContainer - Tests renders correctly 1`] = `
class="react-joyride"
/>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div
id="dg-homepage"
Expand Down Expand Up @@ -446,7 +446,7 @@ exports[`PageContainer - Tests renders correctly 1`] = `
</div>
</div>
<div
class="css-1co1cbj"
class="css-1j8dxwd"
>
<div
style="text-align: left; font-weight: bold; font-size: 14px; text-indent: 16px; display: inline-block;"
Expand Down
15 changes: 5 additions & 10 deletions src/authentication/jwtAuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ describe('jwt auth provider', () => {
it('should call the api to authenticate', async () => {
(mockAxios.post as jest.Mock).mockImplementation(() =>
Promise.resolve({
data: {
token: testToken,
},
data: testToken,
})
);

Expand Down Expand Up @@ -99,12 +97,9 @@ describe('jwt auth provider', () => {

await jwtAuthProvider.verifyLogIn();

expect(mockAxios.post).toBeCalledWith(
'http://localhost:8000/api/jwt/checkToken',
{
token: testToken,
}
);
expect(mockAxios.post).toBeCalledWith('http://localhost:8000/verify', {
token: testToken,
});
});

it('should call refresh if the access token has expired', async () => {
Expand Down Expand Up @@ -137,7 +132,7 @@ describe('jwt auth provider', () => {
await jwtAuthProvider.refresh();

expect(mockAxios.post).toHaveBeenCalledWith(
'http://localhost:8000/api/jwt/refresh',
'http://localhost:8000/refresh',
{
token: testToken,
}
Expand Down
4 changes: 3 additions & 1 deletion src/example.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ describe('Example component', () => {

it('renders correctly', () => {
// update the notification
state.scigateway.notifications = ['test notification'];
state.scigateway.notifications = [
{ severity: 'warning', message: 'test notification' },
];

const { asFragment } = render(
<ExampleComponent store={mockStore(state)} />
Expand Down
2 changes: 1 addition & 1 deletion src/footer/__snapshots__/footer.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Footer component footer renders correctly 1`] = `
<DocumentFragment>
<div
class="css-1co1cbj"
class="css-1j8dxwd"
>
<div
style="text-align: left; font-weight: bold; font-size: 14px; text-indent: 16px; display: inline-block;"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports[`Navigation drawer component Navigation drawer renders correctly when cl
class="MuiDrawer-root MuiDrawer-docked css-ak80xd-MuiDrawer-docked"
>
<div
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-8bwi45-MuiPaper-root-MuiDrawer-paper"
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-1b4jzf5-MuiPaper-root-MuiDrawer-paper"
style="visibility: hidden; webkit-transform: translateX(-0px); transform: translateX(-0px);"
>
<div
Expand All @@ -33,7 +33,7 @@ exports[`Navigation drawer component Navigation drawer renders correctly when op
class="MuiDrawer-root MuiDrawer-docked css-ak80xd-MuiDrawer-docked"
>
<div
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-8bwi45-MuiPaper-root-MuiDrawer-paper"
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation0 MuiDrawer-paper MuiDrawer-paperAnchorLeft MuiDrawer-paperAnchorDockedLeft css-1b4jzf5-MuiPaper-root-MuiDrawer-paper"
>
<div
class="MuiBox-root css-1pvs5pb"
Expand Down
12 changes: 6 additions & 6 deletions src/routing/__snapshots__/routing.component.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Routing component redirects to the homepage if navigating to login page while logged in 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div
id="dg-homepage"
Expand Down Expand Up @@ -262,7 +262,7 @@ exports[`Routing component redirects to the homepage if navigating to login page
exports[`Routing component renders a route for a plugin when site is under maintenance and user is admin 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div>
<div
Expand All @@ -278,7 +278,7 @@ exports[`Routing component renders a route for a plugin when site is under maint
exports[`Routing component renders a route for admin page 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div>
Mocked AdminPage
Expand All @@ -290,7 +290,7 @@ exports[`Routing component renders a route for admin page 1`] = `
exports[`Routing component renders a route for maintenance page when site is under maintenance and user is not admin 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
Mocked MaintenancePage
</div>
Expand All @@ -300,7 +300,7 @@ exports[`Routing component renders a route for maintenance page when site is und
exports[`Routing component renders component with no plugin routes 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div
id="dg-homepage"
Expand Down Expand Up @@ -559,7 +559,7 @@ exports[`Routing component renders component with no plugin routes 1`] = `
exports[`Routing component renders component with plugins 1`] = `
<DocumentFragment>
<div
class="css-9peh1a"
class="css-1sr0u83"
>
<div
id="dg-homepage"
Expand Down
20 changes: 16 additions & 4 deletions src/routing/authorisedRoute.component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import {
} from '../state/actions/scigateway.actions';
import { flushPromises } from '../setupTests';
import { Provider } from 'react-redux';
import { render, waitFor, screen, RenderResult } from '@testing-library/react';
import {
act,
render,
waitFor,
screen,
RenderResult,
} from '@testing-library/react';
import {
createStore,
combineReducers,
Expand Down Expand Up @@ -273,15 +279,21 @@ describe('AuthorisedRoute component', () => {
componentToProtect: ComponentToProtect,
});

dispatch(siteLoadingUpdate(false));
act(() => {
dispatch(siteLoadingUpdate(false));
});

await waitFor(() =>
expect(getDispatchedActions()).toContainEqual(requestPluginRerender())
);
clearDispatchedActions();

dispatch({ type: SignOutType });
dispatch(verifyUsernameAndPassword('username', 'password'));
act(() => {
dispatch({ type: SignOutType });
});
act(() => {
dispatch(verifyUsernameAndPassword('username', 'password'));
});

await waitFor(() =>
expect(getDispatchedActions()).toContainEqual(requestPluginRerender())
Expand Down

0 comments on commit d139560

Please sign in to comment.