Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for maintenance state every 5 minutes and force refresh if changed #1315

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('App', () => {
expect(screen.getByText('Loading...')).toBeInTheDocument();
});

it('loadMaintenanceState dispatched when maintenance changes', async () => {
it('should dispatch loadMaintenanceState and force refresh the page when maintenance changes', async () => {
// mock so token verify succeeds
(axios.post as jest.Mock).mockImplementation(() =>
Promise.resolve({
Expand All @@ -54,6 +54,11 @@ describe('App', () => {
);
window.matchMedia = jest.fn().mockReturnValue({ matches: true });

Object.defineProperty(window, 'location', {
configurable: true,
value: { reload: jest.fn() },
});

jest.useFakeTimers();

render(<AppSansHoc t={jest.fn()} i18n={{}} tReady={true} />);
Expand Down Expand Up @@ -98,5 +103,26 @@ describe('App', () => {

expect(screen.getByText('Maintenance')).toBeInTheDocument();
expect(screen.getByText('test message')).toBeInTheDocument();

// should not refresh page when maintenance state changes from false to true
expect(window.location.reload).not.toHaveBeenCalled();

(axios.get as jest.Mock).mockImplementation(() =>
Promise.resolve({
data: {
show: false,
message: 'test message',
},
})
);

jest.runOnlyPendingTimers();

await act(async () => {
await flushPromises();
});

// should refresh page when maintenance state changes from true to false
expect(window.location.reload).toHaveBeenCalled();
});
});
8 changes: 6 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const toastrConfig = (): React.ReactElement => (
class App extends React.Component<WithTranslation> {
public componentDidMount(): void {
// Check for changes in maintenance state. Ensures that state changes are
// loaded when a user does not reload the site for longer than an hour.
// loaded when a user does not reload the site for longer than 5 minutes.
setInterval(() => {
const provider = getState().scigateway.authorisation.provider;
if (provider.fetchMaintenanceState) {
Expand All @@ -81,10 +81,14 @@ class App extends React.Component<WithTranslation> {
storedMaintenanceState.message !== fetchedMaintenanceState.message
) {
dispatch(loadMaintenanceState(fetchedMaintenanceState));

// Reload the page if maintenance state changes from true to false
if (storedMaintenanceState.show && !fetchedMaintenanceState.show)
window.location.reload();
}
});
}
}, 1000 * 60 * 60);
}, 1000 * 60 * 5);
}

public render(): React.ReactElement {
Expand Down