UNPKG

@ministryofjustice/probation-search-frontend

Version:

A shared UI component to search for probation cases from within your Express/Nunjucks application

68 lines 2.69 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const caseSearchService_1 = __importDefault(require("./caseSearchService")); const request = (body, query = {}, session = {}) => ({ body, query, session, protocol: 'http', get: () => 'localhost', originalUrl: '/search', url: '/search', }); let req; let res; let next; const environment = 'local'; const hmppsAuthClient = { getToken: jest.fn() }; let service; beforeEach(() => { req = request({}); res = { render: jest.fn(), redirect: jest.fn(), locals: {}, }; next = () => { }; service = new caseSearchService_1.default({ environment, hmppsAuthClient }); }); describe('POST /search', () => { test('stores the query and reloads the page', () => { req = request({ 'probation-search-input': 'test' }); service.post(req, res, next); expect(req.session.probationSearch?.query).toEqual('test'); expect(res.redirect).toHaveBeenCalledWith('http://localhost/search'); }); }); describe('GET /search', () => { test('renders an empty screen when no session', () => { service.get(req, res, next); expect(res.locals.searchResults).toEqual({}); }); test('renders an error message if query is undefined', () => { req.session.probationSearch = {}; service.get(req, res, next); expect(res.locals.searchResults).toEqual({ errorMessage: { text: 'Please enter a search term' }, query: '' }); }); test('renders an error message if query is empty string', () => { req.session.probationSearch = { query: '' }; service.get(req, res, next); expect(res.locals.searchResults).toEqual({ errorMessage: { text: 'Please enter a search term' }, query: '' }); }); it('renders an error message when query is too long', () => { req.session.probationSearch = { query: '12345' }; new caseSearchService_1.default({ environment, hmppsAuthClient, maxQueryLength: 4 }).get(req, res, next); expect(res.locals.searchResults).toEqual({ errorMessage: { text: 'Query must be 4 characters or less.' }, query: '12345', }); }); test('renders empty search screen for empty query when allowEmptyQuery is true', () => { new caseSearchService_1.default({ environment, hmppsAuthClient, allowEmptyQuery: true }).get(req, res, next); expect(res.locals.searchResults).toEqual({}); }); }); //# sourceMappingURL=caseSearchService.test.js.map