Skip to content

Commit

Permalink
Update references to filter-permits to point at find-permit, implemen…
Browse files Browse the repository at this point in the history
…t old find-permit logic in client code, update data for injectWithCookies to contain newCost fields
  • Loading branch information
jaucourt committed Sep 20, 2023
1 parent 6c67931 commit 6c2e5d1
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 11 deletions.
52 changes: 52 additions & 0 deletions packages/gafl-webapp-service/src/__mocks__/data/permits.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export default [
isForFulfilment: false,
isCounterSales: true,
cost: 6,
newCost: 7,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42289'
},
{
Expand Down Expand Up @@ -71,6 +73,8 @@ export default [
isForFulfilment: false,
isCounterSales: true,
cost: 6,
newCost: 7,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42290'
},
{
Expand Down Expand Up @@ -108,6 +112,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 12,
newCost: 13,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42361'
},
{
Expand Down Expand Up @@ -145,6 +151,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 12,
newCost: 13,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42363'
},
{
Expand Down Expand Up @@ -182,6 +190,8 @@ export default [
isForFulfilment: false,
isCounterSales: true,
cost: 12,
newCost: 13,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42305'
},
{
Expand Down Expand Up @@ -219,6 +229,8 @@ export default [
isForFulfilment: false,
isCounterSales: true,
cost: 12,
newCost: 13,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42307'
},
{
Expand Down Expand Up @@ -256,6 +268,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 27,
newCost: 28,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42365'
},
{
Expand Down Expand Up @@ -293,6 +307,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 27,
newCost: 28,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42370'
},
{
Expand Down Expand Up @@ -330,6 +346,8 @@ export default [
isForFulfilment: false,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -367,6 +385,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 30,
newCost: 31,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42309'
},
{
Expand Down Expand Up @@ -404,6 +424,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 20,
newCost: 21,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42335'
},
{
Expand Down Expand Up @@ -441,6 +463,8 @@ export default [
isForFulfilment: false,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -478,6 +502,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 20,
newCost: 21,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42337'
},
{
Expand Down Expand Up @@ -515,6 +541,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 20,
newCost: 21,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42339'
},
{
Expand Down Expand Up @@ -552,6 +580,8 @@ export default [
isForFulfilment: false,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -589,6 +619,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 45,
newCost: 46,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42341'
},
{
Expand Down Expand Up @@ -626,6 +658,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 30,
newCost: 31,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42343'
},
{
Expand Down Expand Up @@ -663,6 +697,8 @@ export default [
isForFulfilment: false,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -700,6 +736,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 30,
newCost: 31,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42345'
},
{
Expand Down Expand Up @@ -737,6 +775,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 30,
newCost: 31,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42347'
},
{
Expand Down Expand Up @@ -774,6 +814,8 @@ export default [
isForFulfilment: true,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -811,6 +853,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 82,
newCost: 83,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42372'
},
{
Expand Down Expand Up @@ -848,6 +892,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 54,
newCost: 55,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42374'
},
{
Expand Down Expand Up @@ -885,6 +931,8 @@ export default [
isForFulfilment: true,
isCounterSales: false,
cost: 0,
newCost: 0,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: null
},
{
Expand Down Expand Up @@ -922,6 +970,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 54,
newCost: 55,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42376'
},
{
Expand Down Expand Up @@ -959,6 +1009,8 @@ export default [
isForFulfilment: true,
isCounterSales: true,
cost: 54,
newCost: 55,
newCostStartDate: '2023-04-01T00:00:00.000Z',
itemId: '42378'
}
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getData } from '../route'
import { LICENCE_SUMMARY_SEEN } from '../../../../constants.js'
import { DATE_OF_BIRTH, LICENCE_LENGTH, LICENCE_TO_START, LICENCE_TYPE, NAME, NEW_TRANSACTION, NEW_PRICES } from '../../../../uri.js'
import { findPermit } from '../../../../processors/find-permit.js'
import findPermit from '../../../../processors/find-permit.js'
import hashPermission from '../../../../processors/hash-permission.js'
import { licenceTypeDisplay } from '../../../../processors/licence-type-display.js'
import { addLanguageCodeToUri } from '../../../../processors/uri-helper.js'
import mappingConstants from '../../../../processors/mapping-constants.js'
Expand Down Expand Up @@ -40,14 +41,18 @@ jest.mock('../../../../processors/mapping-constants.js', () => ({
none: 'Not Proof'
}
}))
jest.mock('../../../../processors/find-permit.js', () => ({
findPermit: jest.fn((_permission, _request) => {})
}))
jest.mock('../../../../processors/find-permit.js')
jest.mock('../../../../processors/hash-permission.js')
jest.mock('../../../../processors/price-display.js', () => ({
displayPermissionPrice: jest.fn(() => '#6')
}))

const getMockRequest = ({ currentPermission = getMockPermission(), statusCache = {}, statusCacheSet = () => {} } = {}) => ({
const getMockRequest = ({
currentPermission = getMockPermission(),
statusCache = {},
statusCacheSet = () => {},
transactionCacheSet = () => {}
} = {}) => ({
cache: () => ({
helpers: {
status: {
Expand All @@ -56,7 +61,7 @@ const getMockRequest = ({ currentPermission = getMockPermission(), statusCache =
},
transaction: {
getCurrentPermission: async () => currentPermission,
setCurrentPermission: () => {}
setCurrentPermission: transactionCacheSet
}
}
}),
Expand Down Expand Up @@ -168,6 +173,9 @@ const getMockContinuingPermission = () => ({
})

describe('licence-summary > route', () => {
beforeAll(() => {
hashPermission.mockReturnValue('lkjhgfdertyu0987654rftghj')
})
beforeEach(jest.clearAllMocks)

describe('sets from summary on status cache', () => {
Expand Down Expand Up @@ -224,12 +232,70 @@ describe('licence-summary > route', () => {
it.each([
{ desc: 'renewal', currentPermission: getMockPermission() },
{ desc: 'new', currentPermission: getMockNewPermission() }
])('calls findPermit with permission and request where permission is a $desc permission', async ({ currentPermission }) => {
])('calls findPermit with permission where permission is a $desc permission', async ({ currentPermission }) => {
const mockRequest = getMockRequest({ currentPermission })
await getData(mockRequest)
expect(findPermit).toHaveBeenCalledWith(currentPermission, mockRequest)
})

it('attaches the permit to the permission', async () => {
const currentPermisison = getMockPermission()
const mockRequest = getMockRequest({ currentPermisison })
const permit = { cost: 10 }
hashPermission.mockReturnValueOnce('dfghj3456789')
findPermit.mockReturnValueOnce(permit)

await getData(mockRequest)

expect(displayPermissionPrice).toHaveBeenCalledWith(expect.objectContaining({ permit }), expect.any(Object))
})

it('hashes the permission', async () => {
const currentPermission = getMockPermission()
const mockRequest = getMockRequest({ currentPermission })
const hash = Symbol('hash')
hashPermission.mockReturnValueOnce(hash)

await getData(mockRequest)

expect(licenceTypeDisplay).toHaveBeenCalledWith(expect.objectContaining({ hash }), expect.any(Object))
})

it('only retrieves the permit if the hash has changed', async () => {
const hash = Symbol('hash')
const currentPermission = { ...getMockPermission(), hash }
const mockRequest = getMockRequest({ currentPermission })
hashPermission.mockReturnValueOnce(hash)
await getData(mockRequest)

expect(findPermit).not.toHaveBeenCalled()
})

it('persists modified permission', async () => {
const hash = Symbol('hash')
const currentPermission = getMockPermission()
const transactionCacheSet = jest.fn()
const mockRequest = getMockRequest({ currentPermission, transactionCacheSet })
const permit = { cost: 10 }
hashPermission.mockReturnValueOnce(hash)
findPermit.mockReturnValueOnce(permit)

await getData(mockRequest)

expect(transactionCacheSet).toHaveBeenCalledWith(expect.objectContaining({ ...currentPermission, permit, hash }))
})

it("doesn't persist modified permission if hash hasn't changed", async () => {
const currentPermission = getMockPermission()
currentPermission.hash = hashPermission()
const transactionCacheSet = jest.fn()
const mockRequest = getMockRequest({ currentPermission, transactionCacheSet })

await getData(mockRequest)

expect(transactionCacheSet).not.toHaveBeenCalled()
})

it.each([[NEW_TRANSACTION.uri], [NEW_PRICES.uri]])('addLanguageCodeToUri is called with request and %s', async uri => {
const mockRequest = getMockRequest()
await getData(mockRequest)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import moment from 'moment-timezone'
import pageRoute from '../../../routes/page-route.js'
import GetDataRedirect from '../../../handlers/get-data-redirect.js'
import { findPermit } from '../../../processors/find-permit.js'
import findPermit from '../../../processors/find-permit.js'
import hashPermission from '../../../processors/hash-permission.js'
import { displayStartTime } from '../../../processors/date-and-time-display.js'
import { licenceTypeDisplay } from '../../../processors/licence-type-display.js'
import {
Expand Down Expand Up @@ -189,7 +190,12 @@ export const getData = async request => {
status.fromSummary = getFromSummary(status.fromSummary, permission.isRenewal)
await request.cache().helpers.status.setCurrentPermission(status)
debug('retrieving permit info')
await findPermit(permission, request)
const hash = hashPermission(permission)
if (permission.hash !== hash) {
permission.permit = await findPermit(permission, request)
permission.hash = hash
await request.cache().helpers.transaction.setCurrentPermission(permission)
}
debug('retrieved permit', JSON.stringify(permission))

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pricingDetail } from '../pricing-summary.js'

jest.mock('../filter-permits.js', () => ({
jest.mock('../find-permit.js', () => ({
getPermitsJoinPermitConcessions: () => [
{
id: '9d1b34a0-0c66-e611-80dc-c4346bad0190',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { licenseTypes } from '../pages/licence-details/licence-type/route.js'
import { LICENCE_TYPE } from '../uri.js'
import { getPermitsJoinPermitConcessions } from './filter-permits.js'
import { getPermitsJoinPermitConcessions } from './find-permit.js'
import * as concessionHelper from '../processors/concession-helper.js'
import * as constants from './mapping-constants.js'
import moment from 'moment'
Expand Down

0 comments on commit 6c2e5d1

Please sign in to comment.