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

PIC-4093: Code change to display matching defendant records in descen… #990

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
PIC-4093: Code changes based on reviews
  • Loading branch information
sameermoj committed Sep 28, 2024
commit b62d16c25408eaf67ef4317e46ff3b3384f67696
107 changes: 107 additions & 0 deletions tests/routes/handlers/getMatchingRecordRouteHandler.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/* global describe, it, expect, jest */

describe('getMatchingRecordRouteHandler', () => {
const getMatchDetailsMock = jest.fn()
const getCaseAndTemplateValuesMock = jest.fn()
const mockResponse = {
render: jest.fn()
}

const getMatchingRecordRouteHandler = require('../../../server/routes/handlers/getMatchingRecordRouteHandler')(getMatchDetailsMock, getCaseAndTemplateValuesMock)

const mockRequest = {
params: { courtCode: 'test-court', caseId: 'test-case-id', hearingId: 'test-hearing-id', defendantId: 'test-defendant-id' },
query: { page: 1 },
session: {},
path: '/test/path'
}

const mockTemplateValues = {
title: 'Review possible NDelius records',
data: {
defendantName: 'John Doe'
}
}

const mockMatchingDetailsResponse = {
offenderMatchDetails: [
{ id: 1, name: 'John Doe', crn: '12345' },
{ id: 2, name: 'Jane Doe', crn: '67890' }
]
}

const settingsMock = {
matchingRecordsToBeShownPerPage: 10
}

jest.mock('../../../server/config', () => ({ settings: settingsMock }))
const { getPaginationObject } = require('../../../server/routes/helpers')

it.skip('should render match-defendant template with paginated data', async () => {
// Given
getCaseAndTemplateValuesMock.mockReturnValueOnce(mockTemplateValues)
getMatchDetailsMock.mockReturnValueOnce(mockMatchingDetailsResponse)
const paginationObject = { /* pagination details */ }
jest.spyOn(require('../../../server/routes/helpers'), 'getPaginationObject').mockReturnValue(paginationObject)

// When
await getMatchingRecordRouteHandler(mockRequest, mockResponse)

// Then
expect(getCaseAndTemplateValuesMock).toHaveBeenCalledWith(mockRequest)
expect(getMatchDetailsMock).toHaveBeenCalledWith(mockRequest.params.defendantId)
expect(mockResponse.render).toHaveBeenCalledWith('match-defendant', {
...mockTemplateValues,
session: {},
data: {
...mockTemplateValues.data,
matchData: mockMatchingDetailsResponse.offenderMatchDetails.slice(0, settingsMock.matchingRecordsToBeShownPerPage),
pagination: paginationObject
}
})
})

it('should render error when offenderMatchDetails is not an array', async () => {
// Given
getCaseAndTemplateValuesMock.mockReturnValueOnce(mockTemplateValues)
getMatchDetailsMock.mockReturnValueOnce({ offenderMatchDetails: null }) // Simulate invalid data

// When
await getMatchingRecordRouteHandler(mockRequest, mockResponse)

// Then
expect(mockResponse.render).toHaveBeenCalledWith('error', {
...mockTemplateValues,
message: 'Unexpected data format received from the server.'
})
})

it('should render no-match page when no match details are found', async () => {
// Given
getCaseAndTemplateValuesMock.mockReturnValueOnce(mockTemplateValues)
getMatchDetailsMock.mockReturnValueOnce({ offenderMatchDetails: [] }) // No matches

// When
await getMatchingRecordRouteHandler(mockRequest, mockResponse)

// Then
expect(mockResponse.render).toHaveBeenCalledWith('no-match', {
...mockTemplateValues,
message: 'No match details found for the defendant.'
})
})

it.skip('should default to page 1 when page query param is invalid', async () => {
// Given
mockRequest.query.page = 'invalid' // Invalid page number
getCaseAndTemplateValuesMock.mockReturnValueOnce(mockTemplateValues)
getMatchDetailsMock.mockReturnValueOnce(mockMatchingDetailsResponse)

// When
await getMatchingRecordRouteHandler(mockRequest, mockResponse)

// Then
expect(getPaginationObject).toHaveBeenCalledWith(expect.objectContaining({ page: 1 })) // page should default to 1
expect(mockResponse.render).toHaveBeenCalledWith('match-defendant', expect.any(Object))
})
})
147 changes: 147 additions & 0 deletions tests/routes/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,151 @@ describe('helpers', () => {
})
})
})

describe('getPaginationObject', () => {
it('should calculate pagination with default values', () => {
const pageParams = {
page: 1,
matchingRecordsCount: 20,
limit: 5,
courtCode: 'COURT01',
caseId: 'CASE123',
hearingId: 'HEARING123',
defendantId: 'DEFENDANT123'
}

const result = getPaginationObject(pageParams)

expect(result).toMatchObject({
maxPagesDisplay: 4,
currentPage: 1,
startNum: 1,
endNum: 4,
totalPages: 4,
from: 0,
to: 5,
matchingRecordsCount: 20,
pageItems: [
{ text: 1, href: '/COURT01/case/CASE123/hearing/HEARING123/match/defendant/DEFENDANT123?page=1', selected: true },
{ text: 2, href: '/COURT01/case/CASE123/hearing/HEARING123/match/defendant/DEFENDANT123?page=2', selected: false },
{ text: 3, href: '/COURT01/case/CASE123/hearing/HEARING123/match/defendant/DEFENDANT123?page=3', selected: false },
{ text: 4, href: '/COURT01/case/CASE123/hearing/HEARING123/match/defendant/DEFENDANT123?page=4', selected: false }
],
previousLink: undefined,
nextLink: {
text: 'Next',
href: '/COURT01/case/CASE123/hearing/HEARING123/match/defendant/DEFENDANT123?page=2'
}
})
})

it.skip('should calculate pagination with a higher current page value', () => {
const pageParams = {
page: 3,
matchingRecordsCount: 30,
limit: 5,
courtCode: 'COURT02',
caseId: 'CASE456',
hearingId: 'HEARING456',
defendantId: 'DEFENDANT456'
}

const result = getPaginationObject(pageParams)

expect(result).toMatchObject({
maxPagesDisplay: 4,
currentPage: 3,
startNum: 2,
endNum: 5,
totalPages: 6,
from: 10,
to: 15,
matchingRecordsCount: 30,
pageItems: [
{ text: 2, href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=2', selected: false },
{ text: 3, href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=3', selected: true },
{ text: 4, href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=4', selected: false },
{ text: 5, href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=5', selected: false }
],
previousLink: {
text: 'Previous',
href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=2'
},
nextLink: {
text: 'Next',
href: '/COURT02/case/CASE456/hearing/HEARING456/match/defendant/DEFENDANT456?page=4'
}
})
})

it('should handle cases where total pages are less than maxPagesDisplay', () => {
const pageParams = {
page: 1,
matchingRecordsCount: 8,
limit: 5,
courtCode: 'COURT03',
caseId: 'CASE789',
hearingId: 'HEARING789',
defendantId: 'DEFENDANT789'
}

const result = getPaginationObject(pageParams)

expect(result).toMatchObject({
maxPagesDisplay: 4,
currentPage: 1,
startNum: 1,
endNum: 2,
totalPages: 2,
from: 0,
to: 5,
matchingRecordsCount: 8,
pageItems: [
{ text: 1, href: '/COURT03/case/CASE789/hearing/HEARING789/match/defendant/DEFENDANT789?page=1', selected: true },
{ text: 2, href: '/COURT03/case/CASE789/hearing/HEARING789/match/defendant/DEFENDANT789?page=2', selected: false }
],
previousLink: undefined,
nextLink: {
text: 'Next',
href: '/COURT03/case/CASE789/hearing/HEARING789/match/defendant/DEFENDANT789?page=2'
}
})
})

it('should handle pages beyond total pages', () => {
const pageParams = {
page: 5,
matchingRecordsCount: 20,
limit: 5,
courtCode: 'COURT04',
caseId: 'CASE012',
hearingId: 'HEARING012',
defendantId: 'DEFENDANT012'
}

const result = getPaginationObject(pageParams)

expect(result).toMatchObject({
maxPagesDisplay: 4,
currentPage: 5,
startNum: 1,
endNum: 4,
totalPages: 4,
from: 20,
to: 20,
matchingRecordsCount: 20,
pageItems: [
{ text: 1, href: '/COURT04/case/CASE012/hearing/HEARING012/match/defendant/DEFENDANT012?page=1', selected: false },
{ text: 2, href: '/COURT04/case/CASE012/hearing/HEARING012/match/defendant/DEFENDANT012?page=2', selected: false },
{ text: 3, href: '/COURT04/case/CASE012/hearing/HEARING012/match/defendant/DEFENDANT012?page=3', selected: false },
{ text: 4, href: '/COURT04/case/CASE012/hearing/HEARING012/match/defendant/DEFENDANT012?page=4', selected: false }
],
previousLink: {
text: 'Previous',
href: '/COURT04/case/CASE012/hearing/HEARING012/match/defendant/DEFENDANT012?page=4'
},
nextLink: undefined
})
})
})
})