Skip to content

Commit

Permalink
Merge pull request #808 from hmcts/FACT-2096-prefix-search-fix
Browse files Browse the repository at this point in the history
FACT-2096 - Prefix Search Fix
  • Loading branch information
amstevenson authored Dec 9, 2024
2 parents a87f263 + 21c588e commit d660e1c
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 5 deletions.
36 changes: 36 additions & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"@types/chai-as-promised": "^8.0.1",
"@types/cucumber": "^7.0.3",
"@types/jest": "^29.5.14",
"@types/jest-when": "^3.5.5",
"@types/supertest": "^2.0.16",
"@types/webpack-dev-middleware": "^5.3.0",
"@typescript-eslint/eslint-plugin": "^5.62.0",
Expand All @@ -98,6 +99,7 @@
"eslint": "^8.57.1",
"html-webpack-plugin": "^5.6.3",
"jest": "^29.7.0",
"jest-when": "^3.6.0",
"mini-css-extract-plugin": "^2.9.2",
"nock": "^13.5.5",
"nodemon": "^2.0.22",
Expand Down
2 changes: 1 addition & 1 deletion src/main/controllers/search/CourtPrefixSearchController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CourtPrefixSearchController {
if (hasError) {
data.errorType = error;
}
data.results = await this.api.courtPrefixSearch(prefix);
data.results = prefix && prefix.length == 1 ? await this.api.courtPrefixSearch(prefix): [];
res.render('search/prefix-search', data);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mockRequest } from '../../../utils/mockRequest';
import { mockResponse } from '../../../utils/mockResponse';
import { when } from 'jest-when';
import { PageData } from '../../../../../main/interfaces/PageData';
import {CourtPrefixSearchController} from '../../../../../main/controllers/search/CourtPrefixSearchController';

Expand Down Expand Up @@ -35,6 +36,8 @@ describe('Court Prefix Search Controller', () => {
prefix: 'Y'
};
const res = mockResponse();
api.courtPrefixSearch = jest.fn();
when(api.courtPrefixSearch as jest.Mock).calledWith('Y').mockReturnValue(response.data);
await controller.get(req, res);
const expectedData: PageData = {
...i18n.search['prefix-search'],
Expand All @@ -45,9 +48,10 @@ describe('Court Prefix Search Controller', () => {
results: response.data
};
expect(res.render).toBeCalledWith('search/prefix-search', expectedData);
expect(api.courtPrefixSearch).toBeCalledWith('Y');
});

test('Should render the court prefix search page when not searching with prefix', async () => {
test('Should render the court prefix search page but not call API when not searching with prefix/prefix undefined', async () => {
const response: any = { data: []};
const api: any = {
courtPrefixSearch: async () => response.data,
Expand All @@ -58,6 +62,7 @@ describe('Court Prefix Search Controller', () => {
req.query = {
};
const res = mockResponse();
api.courtPrefixSearch = jest.fn();
await controller.get(req, res);
const expectedData: PageData = {
...i18n.search['prefix-search'],
Expand All @@ -68,20 +73,24 @@ describe('Court Prefix Search Controller', () => {
results: []
};
expect(res.render).toBeCalledWith('search/prefix-search', expectedData);
expect(api.courtPrefixSearch).not.toBeCalled();
});

test('Should render the court prefix search page when searching with prefix with no results', async () => {
test('Should render the court prefix search page when searching with prefix returns no results', async () => {
const response: any = { data: []};
const api: any = {
courtPrefixSearch: async () => response.data,
};
const controller = new CourtPrefixSearchController(api);

api.courtPrefixSearch = jest.fn();
const req = mockRequest(i18n);
req.query = {
prefix: 'X'
};
const res = mockResponse();

when(api.courtPrefixSearch as jest.Mock).calledWith('X').mockReturnValue([]);

await controller.get(req, res);
const expectedData: PageData = {
...i18n.search['prefix-search'],
Expand All @@ -92,5 +101,58 @@ describe('Court Prefix Search Controller', () => {
results: []
};
expect(res.render).toBeCalledWith('search/prefix-search', expectedData);
expect(api.courtPrefixSearch).toBeCalledWith('X');
});

test('Should render the court prefix search page but not call API when prefix has more than 1 character', async () => {
const response: any = { data: []};
const api: any = {
courtPrefixSearch: async () => response.data,
};
const controller = new CourtPrefixSearchController(api);
api.courtPrefixSearch = jest.fn();
const req = mockRequest(i18n);
req.query = {
prefix: 'XY'
};
const res = mockResponse();

await controller.get(req, res);
const expectedData: PageData = {
...i18n.search['prefix-search'],
path: '/search-by-prefix',
error: false,
hasNoResults: false,
prefix: 'XY',
results: []
};
expect(res.render).toBeCalledWith('search/prefix-search', expectedData);
expect(api.courtPrefixSearch).not.toBeCalled();
});

test('Should render the court prefix search page but not call API when prefix has less than 1 character', async () => {
const response: any = { data: []};
const api: any = {
courtPrefixSearch: async () => response.data,
};
const controller = new CourtPrefixSearchController(api);
api.courtPrefixSearch = jest.fn();
const req = mockRequest(i18n);
req.query = {
prefix: ''
};
const res = mockResponse();

await controller.get(req, res);
const expectedData: PageData = {
...i18n.search['prefix-search'],
path: '/search-by-prefix',
error: false,
hasNoResults: false,
prefix: '',
results: []
};
expect(res.render).toBeCalledWith('search/prefix-search', expectedData);
expect(api.courtPrefixSearch).not.toBeCalled();
});
});
22 changes: 21 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3365,7 +3365,16 @@ __metadata:
languageName: node
linkType: hard

"@types/jest@npm:^29.5.14":
"@types/jest-when@npm:^3.5.5":
version: 3.5.5
resolution: "@types/jest-when@npm:3.5.5"
dependencies:
"@types/jest": "*"
checksum: 816c1ed5554182c43261d23c2902506c316bc0368f2ea1e632a0a93d05d8f6f2918afa6a406c2f5fdd4d277e042b8d2ba797993dc3c2fd2d5f53cda946696772
languageName: node
linkType: hard

"@types/jest@npm:*, @types/jest@npm:^29.5.14":
version: 29.5.14
resolution: "@types/jest@npm:29.5.14"
dependencies:
Expand Down Expand Up @@ -7209,6 +7218,7 @@ __metadata:
"@types/http-proxy-middleware": ^1.0.0
"@types/i18next": ^13.0.0
"@types/jest": ^29.5.14
"@types/jest-when": ^3.5.5
"@types/lodash": ^4.17.13
"@types/node": ^18.19.64
"@types/nunjucks": ^3.2.6
Expand Down Expand Up @@ -7249,6 +7259,7 @@ __metadata:
i18next: ^21.10.0
i18next-http-middleware: ^3.6.0
jest: ^29.7.0
jest-when: ^3.6.0
jquery: ^3.7.1
js-yaml: ^4.1.0
lodash: ^4.17.21
Expand Down Expand Up @@ -9673,6 +9684,15 @@ __metadata:
languageName: node
linkType: hard

"jest-when@npm:^3.6.0":
version: 3.6.0
resolution: "jest-when@npm:3.6.0"
peerDependencies:
jest: ">= 25"
checksum: ed32ed84e5802bb6fec98966cdf862ce59677551be33d3795e6ac7a6207acf467ed559573d671c8d94c59f7fc0780cf358d2d165d81cdc7d9611250d975ee024
languageName: node
linkType: hard

"jest-worker@npm:^27.4.5":
version: 27.5.1
resolution: "jest-worker@npm:27.5.1"
Expand Down

0 comments on commit d660e1c

Please sign in to comment.