Skip to content

Commit

Permalink
Merge pull request #4229 from nationalarchives/TDRD-367-reload-file-c…
Browse files Browse the repository at this point in the history
…hecks-page

TDRD-367: reload file checks page
  • Loading branch information
Tom-Hallett authored Oct 22, 2024
2 parents 55cc2dc + f894438 commit 2bdc936
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
16 changes: 14 additions & 2 deletions npm/src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,24 @@ export class Checks {

updateFileCheckProgress: (
isJudgmentUser: boolean,
goToNextPage: (formId: string) => void
goToNextPage: (formId: string) => void,
checksPageRefreshInterval: number
) => void | Error = (
isJudgmentUser: boolean,
goToNextPage: (formId: string) => void
goToNextPage: (formId: string) => void,
checksPageRefreshInterval: number
) => {
if (isJudgmentUser) {
this.checkJudgmentTransferProgress(goToNextPage)
} else {
const refreshPageIntervalId: ReturnType<typeof setInterval> = setInterval(
async () => {
clearInterval(intervalId)
window.location.reload()
},
checksPageRefreshInterval
)

const intervalId: ReturnType<typeof setInterval> = setInterval(
async () => {
const fileChecksProgress: IFileCheckProgress | Error =
Expand All @@ -49,9 +59,11 @@ export class Checks {
const checksCompleted = haveFileChecksCompleted(fileChecksProgress)
if (checksCompleted) {
clearInterval(intervalId)
clearInterval(refreshPageIntervalId)
displayChecksCompletedBanner("file-checks")
}
} else {
clearInterval(refreshPageIntervalId)
return fileChecksProgress
}
},
Expand Down
6 changes: 5 additions & 1 deletion npm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ export const renderModules = async () => {
const nextPageModule = await import(
"./nextpageredirect/next-page-redirect"
)
//interval for page reload set at 90% of token validity period
const checksPageRefreshInterval =
(keycloak.tokenParsed?.exp * 1000 - Date.now()) * 0.9
const resultOrError = new checksModule.Checks().updateFileCheckProgress(
isJudgmentUser,
nextPageModule.goToNextPage
nextPageModule.goToNextPage,
checksPageRefreshInterval
)
if (errorHandlingModule.isError(resultOrError)) {
errorHandlingModule.handleUploadError(resultOrError)
Expand Down
30 changes: 22 additions & 8 deletions npm/test/update-check-progress.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ import { Checks } from "../src/checks"
import {hasDraftMetadataValidationCompleted, haveFileChecksCompleted} from "../src/checks/verify-checks-have-completed"
import { displayChecksCompletedBanner } from "../src/checks/display-checks-completed-banner"

//stop console errors from window.location.reload in these tests that arise as described here: https://remarkablemark.org/blog/2018/11/17/mock-window-location/
const original = window.location;

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

afterAll(() => {
Object.defineProperty(window, 'location', { configurable: true, value: original });
});

jest.mock('uuid', () => 'eb7b7961-395d-4b4c-afc6-9ebcadaf0150')

jest.mock(
Expand Down Expand Up @@ -125,9 +139,9 @@ const mockDisplayChecksHaveCompletedBanner: () => void = () =>

test("'updateFileCheckProgress' calls setInterval correctly", async () => {
jest.spyOn(global, "setInterval")
await checks.updateFileCheckProgress(false, mockGoToNextPage)
await checks.updateFileCheckProgress(false, mockGoToNextPage, 300000)
jest.runOnlyPendingTimers()
expect(setInterval).toBeCalledTimes(1)
expect(setInterval).toBeCalledTimes(2)
})

test("'updateDraftMetadataValidationProgress' calls setInterval correctly", async () => {
Expand All @@ -153,7 +167,7 @@ test("'updateFileCheckProgress' shows a standard user, the notification banner a

mockDisplayChecksHaveCompletedBanner()

checks.updateFileCheckProgress(false, mockGoToNextPage)
checks.updateFileCheckProgress(false, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(haveFileChecksCompleted).toBeCalled()
Expand Down Expand Up @@ -220,7 +234,7 @@ test("'updateFileCheckProgress' shows a standard user, no banner and a disabled
)
mockDisplayChecksHaveCompletedBanner()

checks.updateFileCheckProgress(false, mockGoToNextPage)
checks.updateFileCheckProgress(false, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(haveFileChecksCompleted).toBeCalled()
Expand Down Expand Up @@ -262,7 +276,7 @@ test("'updateFileCheckProgress' shows a standard user, no banner and a disabled
)
mockDisplayChecksHaveCompletedBanner()

checks.updateFileCheckProgress(false, mockGoToNextPage)
checks.updateFileCheckProgress(false, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(haveFileChecksCompleted).toBeCalled()
Expand Down Expand Up @@ -298,7 +312,7 @@ test("'updateFileCheckProgress' calls goToNextPage for a judgment user, if all c

mockTransferProgress("complete")

checks.updateFileCheckProgress(true, mockGoToNextPage)
checks.updateFileCheckProgress(true, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(mockGoToNextPage).toHaveBeenCalled()
Expand All @@ -311,7 +325,7 @@ test("'updateFileCheckProgress' does not call goToNextPage for a judgment user i
)
mockTransferProgress("inProgress")

checks.updateFileCheckProgress(true, mockGoToNextPage)
checks.updateFileCheckProgress(true, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(mockGoToNextPage).toHaveBeenCalled()
Expand All @@ -324,7 +338,7 @@ test("'updateFileCheckProgress' does not call goToNextPage for a judgment user i
)
mockTransferProgress("noData")

checks.updateFileCheckProgress(true, mockGoToNextPage)
checks.updateFileCheckProgress(true, mockGoToNextPage, 300000)
await jest.runOnlyPendingTimers()

expect(mockGoToNextPage).not.toHaveBeenCalled()
Expand Down

0 comments on commit 2bdc936

Please sign in to comment.