Skip to content

Commit

Permalink
MAN-352 add unit tests for middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
neil-mills committed Feb 1, 2025
1 parent cf46d5d commit 1495824
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 11 deletions.
71 changes: 71 additions & 0 deletions server/middleware/filterActivityLog.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Request } from 'express'
import { filterActivityLog } from './filterActivityLog'
import { AppResponse } from '../@types'

describe('/middleware/filterActivityLog', () => {
const nextSpy = jest.fn()
const crn = 'X756510'
const res = {
locals: {},
redirect: jest.fn().mockReturnThis(),
} as unknown as AppResponse

const req = {
body: {},
params: {},
query: {},
session: {
cache: {
activityLog: {
results: null,
filters: null,
},
},
},
method: 'GET',
} as Request

it(`should redirect the page if 'submit' is in the request query`, () => {
req.url = `case/${crn}/activity-log?keywords=&dateFrom=&dateTo=&submit=true&page=2`
req.params.crn = crn
req.query = { keywords: '', dateFrom: '', dateTo: '', submit: 'true' }
req.session.cache.activityLog.filters = {
keywords: 'some keywords',
dateFrom: '21/1/2025',
dateTo: '31/1/2025',
compliance: ['Option 1', 'Option 2', 'Option 3'],
}
const redirectSpy = jest.spyOn(res, 'redirect')
filterActivityLog(req, res, nextSpy)
expect(redirectSpy).toHaveBeenCalledWith(`case/${crn}/activity-log?keywords=&dateFrom=&dateTo=`)
})

it(`should hydrate the selected filter values from cache, if the cache exists, no values exist in the query and form has not been submitted`, () => {
req.url = `case/${crn}/activity-log?keywords=&dateFrom=&dateTo=&page=2`
req.query = { keywords: '', dateFrom: '', dateTo: '' }
req.session.cache.activityLog.filters = {
keywords: 'some keywords',
dateFrom: '12/1/2025',
dateTo: '27/1/2025',
compliance: ['no outcome', 'complied'],
}
filterActivityLog(req, res, nextSpy)
expect(res.locals.filters.query).toEqual({
...req.session.cache.activityLog.filters,
})
})

it('should hydrate the selected filter values from the request query if form has been submitted but query exists', () => {
req.url = `case/${crn}/activity-log?keywords=some+keyword&dateFrom=12/1/2025&dateTo=27/1/2025&compliance=no+outcome&compliance=complied&page=2`
req.query = {
keywords: 'some keyword',
dateFrom: '12/1/2025',
dateTo: '27/1/2025',
compliance: ['no outcome', 'complied'],
}
filterActivityLog(req, res, nextSpy)
expect(res.locals.filters.query).toEqual({
...req.query,
})
})
})
12 changes: 1 addition & 11 deletions server/middleware/filterActivityLog.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
/* eslint-disable no-param-reassign */

import { DateTime } from 'luxon'
import {
Route,
ActivityLogCacheItem,
ActivityLogFilters,
ActivityLogFiltersResponse,
SelectedFilterItem,
Option,
} from '../@types'
import { Route, ActivityLogFilters, ActivityLogFiltersResponse, SelectedFilterItem, Option } from '../@types'

export const filterActivityLog: Route<void> = (req, res, next) => {
if (req?.query?.submit) {
Expand All @@ -35,7 +28,6 @@ export const filterActivityLog: Route<void> = (req, res, next) => {

let query: ActivityLogFilters = { keywords, dateFrom, dateTo, compliance }
const hasQuery = keywords || (dateFrom && dateTo) || compliance?.length
// if not submitted and no query values exist, then attempt to hydrate from cache

if (req?.session?.cache?.activityLog?.filters && !req?.query?.submit && !hasQuery) {
query = req?.session?.cache?.activityLog?.filters
Expand All @@ -56,9 +48,7 @@ export const filterActivityLog: Route<void> = (req, res, next) => {
}
query = { ...query, clearFilterKey, clearFilterValue }

console.log(query)
const errors = req?.session?.errors

const baseUrl = `/case/${crn}/activity-log`
if (!Array.isArray(query.compliance)) {
query.compliance = [query.compliance]
Expand Down

0 comments on commit 1495824

Please sign in to comment.