|
| 1 | +import {Request, Response} from 'express'; |
| 2 | +import {faker} from '@faker-js/faker'; |
| 3 | +import * as TE from 'fp-ts/TaskEither'; |
| 4 | +import {Dependencies} from '../../src/dependencies'; |
| 5 | +import {formGet} from '../../src/http/form-get'; |
| 6 | +import { |
| 7 | + html, |
| 8 | + CompleteHtmlDocument, |
| 9 | + safe, |
| 10 | + sanitizeString, |
| 11 | + toLoggedInContent, |
| 12 | +} from '../../src/types/html'; |
| 13 | +import {Form} from '../../src/types/form'; |
| 14 | +import {initTestFramework, TestFramework} from '../read-models/test-framework'; |
| 15 | +import { arbitraryUser } from '../types/user.helper'; |
| 16 | + |
| 17 | +describe('formGet', () => { |
| 18 | + const user = arbitraryUser(); |
| 19 | + let framework: TestFramework; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + framework = await initTestFramework(); |
| 23 | + await framework.commands.memberNumbers.linkNumberToEmail({ |
| 24 | + email: user.emailAddress, |
| 25 | + memberNumber: user.memberNumber, |
| 26 | + name: undefined, |
| 27 | + formOfAddress: undefined, |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + framework.close(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('responds with a rendered page when the form loads successfully', async () => { |
| 36 | + const form: Form<{message: string}> = { |
| 37 | + constructForm: input => _context => TE.right({message: (input as any).id}), |
| 38 | + renderForm: ({message}) => toLoggedInContent(html`Test form`)(html`<p>${sanitizeString(message)}</p>`), |
| 39 | + }; |
| 40 | + const req = { |
| 41 | + session: {passport: {user}}, |
| 42 | + query: {}, |
| 43 | + params: {id: 'hello'}, |
| 44 | + } as unknown as Request; |
| 45 | + const res = { |
| 46 | + status: jest.fn(), |
| 47 | + send: jest.fn(), |
| 48 | + redirect: jest.fn(), |
| 49 | + }; |
| 50 | + res.status.mockReturnValue(res); |
| 51 | + res.send.mockReturnValue(res); |
| 52 | + |
| 53 | + await formGet(framework, form)( |
| 54 | + req, |
| 55 | + res as unknown as Response<CompleteHtmlDocument> |
| 56 | + ); |
| 57 | + |
| 58 | + expect(res.redirect).not.toHaveBeenCalled(); |
| 59 | + expect(res.status).toHaveBeenCalledWith(200); |
| 60 | + expect(res.send).toHaveBeenCalledWith(expect.stringContaining('hello')); |
| 61 | + }); |
| 62 | +}); |
0 commit comments