Skip to content

Commit

Permalink
Merge pull request #1376 from ral-facilities/increase-toast-functiona…
Browse files Browse the repository at this point in the history
…ilty-#355

Increase toast functionailty #355
  • Loading branch information
joshuadkitenge authored Mar 7, 2024
2 parents 4df6e62 + e663688 commit b51eecf
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
31 changes: 31 additions & 0 deletions cypress/e2e/login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,37 @@ describe('Login', () => {
cy.get('#demo_plugin').contains('Demo Plugin').should('be.visible');
});

it('can remove toasts with esc keydown when a plugin error occurs', () => {
cy.intercept('/settings.json', {
plugins: [
{
name: 'demo_plugin',
src: '/plugins/main.js',
enable: true,
location: 'main',
},
],
'ui-strings': 'res/default.json',
'auth-provider': 'icat',
authUrl: 'http://localhost:8000',
autoLogin: true,
'help-tour-steps': [],
});
verifyResponse = verifySuccess;
loginResponse = loginSuccess;
cy.visit('/plugin1');

cy.contains(
'Failed to load plugin demo_plugin from /plugins/main.js.'
).should('exist');

cy.get('body').type('{esc}');

cy.contains(
'Failed to load plugin demo_plugin from /plugins/main.js.'
).should('not.exist');
});

it('should be able to switch authenticators and still be "auto logged in"', () => {
verifyResponse = verifySuccess;
loginResponse = loginSuccess;
Expand Down
21 changes: 21 additions & 0 deletions src/pageContainer.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Tour from './tour/tour.component';
import CookieConsent from './cookieConsent/cookieConsent.component';
import Footer from './footer/footer.component';
import { useMediaQuery } from '@mui/material';
import { toastr } from 'react-redux-toastr';

const RootDiv = styled('div')(({ theme }) => ({
position: 'relative',
Expand All @@ -19,6 +20,26 @@ const PageContainer = (): React.ReactElement => {
const theme = useTheme();
const isViewportMdOrLarger = useMediaQuery(theme.breakpoints.up('md'));

React.useEffect(() => {
const clearToasts = (): void => {
toastr.clean();
};

const handleKeyPress = (event: KeyboardEvent): void => {
if (event instanceof KeyboardEvent && event.key === 'Escape') {
clearToasts();
}
};

// Add event listeners for keydown events
document.addEventListener('keydown', handleKeyPress);

// Clean up the event listeners when component unmounts
return () => {
document.removeEventListener('keydown', handleKeyPress);
};
}, []);

return (
<RootDiv>
<Preloader fullScreen={true} />
Expand Down
22 changes: 21 additions & 1 deletion src/pageContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { MemoryRouter } from 'react-router-dom';
import PageContainer from './pageContainer.component';
import { StateType } from './state/state.types';
import { authState, initialState } from './state/reducers/scigateway.reducer';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { Provider } from 'react-redux';
import { ThemeProvider } from '@mui/material';
import { buildTheme } from './theming';
import { toastr } from 'react-redux-toastr';
import userEvent from '@testing-library/user-event';

jest.mock('@mui/material', () => ({
__esmodule: true,
Expand Down Expand Up @@ -43,4 +45,22 @@ describe('PageContainer - Tests', () => {

expect(asFragment()).toMatchSnapshot();
});

it('calls toastr.clean() when escape is clicked', async () => {
const cleanSpy = jest.spyOn(toastr, 'clean');
render(
<Provider store={configureStore([thunk])(state)}>
<ThemeProvider theme={buildTheme(false)}>
<MemoryRouter initialEntries={[{ key: 'testKey' }]}>
<PageContainer />
</MemoryRouter>
</ThemeProvider>
</Provider>
);

const element = screen.getByLabelText('home-page');
await userEvent.type(element, '{Escape}');
expect(cleanSpy).toHaveBeenCalled();
cleanSpy.mockRestore();
});
});

0 comments on commit b51eecf

Please sign in to comment.