-
Notifications
You must be signed in to change notification settings - Fork 3
Accept new class for cache service input #189
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
Merged
alexluckett
merged 8 commits into
feat/DF-370-save-progress
from
feature/injectable-cache-service
Sep 8, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fe0ef89
Renamed saveAndReturn to saveAndExit
jbarnsley10 2e2d7da
Further rename to saveAndExit
jbarnsley10 1e11122
Accept new class for cache service input
alexluckett 71ace3e
Accept a CacheService instance instead of a cache name
alexluckett 932335d
Export CacheService
alexluckett bab69c6
use kebab case to align cache service with file form service
alexluckett 70bb349
Add trailing newline to satisfy editorconfig
alexluckett ea68514
Fix state leakage between tests
alexluckett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import { join } from 'path' | ||
|
|
||
| import { Engine as CatboxMemory } from '@hapi/catbox-memory' | ||
|
|
||
| import { FORM_PREFIX } from '~/src/server/constants.js' | ||
| import { createServer } from '~/src/server/index.js' | ||
| import { CacheService } from '~/src/server/services/cacheService.js' | ||
| import { getCookie, getCookieHeader } from '~/test/utils/get-cookie.js' | ||
|
|
||
| const basePath = `${FORM_PREFIX}/minimal` | ||
|
|
||
| class NewCacheService extends CacheService { | ||
| /** | ||
| * | ||
| * @param {AnyRequest} _request | ||
| * @param {ADDITIONAL_IDENTIFIER} [_additionalIdentifier] | ||
| * @returns | ||
| */ | ||
| Key(_request, _additionalIdentifier) { | ||
| return { | ||
| segment: 'irrelevant', | ||
| id: 'my-custom-identifier' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| describe('CacheService', () => { | ||
| /** @type {Server} */ | ||
| let server | ||
|
|
||
| afterEach(async () => { | ||
| await server.stop() | ||
| }) | ||
|
|
||
| test('the new cache service is utilised', async () => { | ||
| // Spy on CatboxMemory.prototype.set globally | ||
| const setStateSpy = jest.spyOn(NewCacheService.prototype, 'setState') | ||
| const catboxSetSpy = jest.spyOn(CatboxMemory.prototype, 'set') | ||
|
|
||
| server = await createServer({ | ||
| formFileName: 'minimal.js', | ||
| formFilePath: join(import.meta.dirname, 'definitions'), | ||
| cacheServiceCreator: (server) => | ||
| new NewCacheService({ server, cacheName: 'session' }) | ||
| }) | ||
|
|
||
| await server.initialize() | ||
|
|
||
| // Navigate to start | ||
| const headers = undefined | ||
| const response = await server.inject({ | ||
| url: `${basePath}/start`, | ||
| headers | ||
| }) | ||
|
|
||
| // Extract the session cookie | ||
| const csrfToken = getCookie(response, 'crumb') | ||
| const newHeaders = getCookieHeader(response, ['session', 'crumb']) | ||
|
|
||
| // Submit answers | ||
| await server.inject({ | ||
| url: `${basePath}/start`, | ||
| method: 'POST', | ||
| headers: newHeaders, | ||
| payload: { | ||
| crumb: csrfToken, | ||
| field: 'value' | ||
| } | ||
| }) | ||
|
|
||
| // assert our new custom cache is used | ||
| expect(setStateSpy).toHaveBeenCalled() | ||
| setStateSpy.mockRestore() | ||
|
|
||
| // Assert the custom ID 'my-custom-identifier' is used | ||
| expect(catboxSetSpy).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| id: 'my-custom-identifier', | ||
| segment: 'formSubmission' | ||
| }), | ||
| expect.any(Object), | ||
| expect.any(Number) | ||
| ) | ||
| catboxSetSpy.mockRestore() | ||
| }) | ||
|
|
||
| test('the default cache service is utilised', async () => { | ||
| // Spy on CatboxMemory.prototype.set globally | ||
| const setStateSpy = jest.spyOn(CacheService.prototype, 'setState') | ||
|
|
||
| server = await createServer({ | ||
| formFileName: 'minimal.js', | ||
| formFilePath: join(import.meta.dirname, 'definitions') | ||
| }) | ||
|
|
||
| await server.initialize() | ||
|
|
||
| // Navigate to start | ||
| const headers = undefined | ||
| const response = await server.inject({ | ||
| url: `${basePath}/start`, | ||
| headers | ||
| }) | ||
|
|
||
| // Extract the session cookie | ||
| const csrfToken = getCookie(response, 'crumb') | ||
| const newHeaders = getCookieHeader(response, ['session', 'crumb']) | ||
|
|
||
| // Submit answers | ||
| await server.inject({ | ||
| url: `${basePath}/start`, | ||
| method: 'POST', | ||
| headers: newHeaders, | ||
| payload: { | ||
| crumb: csrfToken, | ||
| field: 'value' | ||
| } | ||
| }) | ||
|
|
||
| // assert our new custom cache is used | ||
| expect(setStateSpy).toHaveBeenCalled() | ||
| setStateSpy.mockRestore() | ||
| }) | ||
| }) | ||
|
|
||
| /** | ||
| * @import { Server } from '@hapi/hapi' | ||
| * @import { AnyFormRequest, AnyRequest, FormSubmissionState } from '~/src/server/plugins/engine/types.js' | ||
| * @import { ADDITIONAL_IDENTIFIER } from '~/src/server/services/cacheService.js' | ||
| */ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering why this is pascal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Legacy reasons, it's always been like that for some reason. Worth us changing soon, I'll leave it for now until the dust has settled on these PRs.