|
| 1 | +import type { Express } from 'express' |
| 2 | +import request from 'supertest' |
| 3 | +import * as cheerio from 'cheerio' |
| 4 | +import { SessionData } from 'express-session' |
| 5 | +import { FieldValidationError } from 'express-validator' |
| 6 | +import { appWithAllRoutes, FlashData, flashProvider } from '../testutils/appSetup' |
| 7 | + |
| 8 | +let app: Express |
| 9 | +let flashData: FlashData |
| 10 | +let sessionData: SessionData |
| 11 | + |
| 12 | +const url = '/block-visit-dates/block-date-or-session' |
| 13 | +const date = '2024-09-06' |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + sessionData = { blockDateOrSession: { date, backLinkHref: '#back-link-from-session' } } as SessionData |
| 17 | + |
| 18 | + app = appWithAllRoutes({ sessionData }) |
| 19 | + |
| 20 | + flashData = {} |
| 21 | + flashProvider.mockImplementation((key: keyof FlashData) => flashData[key]) |
| 22 | +}) |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + jest.resetAllMocks() |
| 26 | +}) |
| 27 | + |
| 28 | +describe('Choose date or session block', () => { |
| 29 | + describe(`GET ${url}`, () => { |
| 30 | + it('should redirect to blocked dates listing page if no new block date in session', () => { |
| 31 | + sessionData.blockDateOrSession = undefined |
| 32 | + return request(app).get(url).expect(302).expect('location', '/block-visit-dates') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should display choose date or session block page', () => { |
| 36 | + return request(app) |
| 37 | + .get(url) |
| 38 | + .expect('Content-Type', /html/) |
| 39 | + .expect(res => { |
| 40 | + const $ = cheerio.load(res.text) |
| 41 | + expect($('.govuk-back-link').attr('href')).toBe('/block-visit-dates') |
| 42 | + expect($('h1').text()).toBe('What would you like to block on Friday 6 September 2024?') |
| 43 | + |
| 44 | + expect($('input[name=blockType]').length).toBe(2) |
| 45 | + expect($('input[name=blockType]').eq(0).val()).toBe('date') |
| 46 | + expect($('input[name=blockType]').eq(1).val()).toBe('session') |
| 47 | + expect($('input[name=blockType]:checked').length).toBe(0) |
| 48 | + |
| 49 | + expect($('[data-test=submit]').text().trim()).toBe('Continue') |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should render validation errors', () => { |
| 54 | + const validationError = { path: 'blockType', msg: 'No answer selected' } as FieldValidationError |
| 55 | + |
| 56 | + flashData = { errors: [validationError] } |
| 57 | + |
| 58 | + return request(app) |
| 59 | + .get(url) |
| 60 | + .expect('Content-Type', /html/) |
| 61 | + .expect(res => { |
| 62 | + const $ = cheerio.load(res.text) |
| 63 | + expect($('.govuk-error-summary a[href="#blockType-error"]').text()).toBe(validationError.msg) |
| 64 | + expect($('#blockType-error').text()).toContain(validationError.msg) |
| 65 | + }) |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + describe(`POST ${url}`, () => { |
| 70 | + it('should redirect to blocked dates listing page if no new block date in session', () => { |
| 71 | + sessionData.blockDateOrSession = undefined |
| 72 | + return request(app).post(url).expect(302).expect('location', '/block-visit-dates') |
| 73 | + }) |
| 74 | + |
| 75 | + it('should redirect to block new date page if this is selected', () => { |
| 76 | + return request(app) |
| 77 | + .post(url) |
| 78 | + .send({ blockType: 'date' }) |
| 79 | + .expect(302) |
| 80 | + .expect('location', '/block-visit-dates/block-new-date') |
| 81 | + .expect(() => { |
| 82 | + expect(flashProvider).toHaveBeenCalledTimes(0) |
| 83 | + expect(sessionData.blockDateOrSession.backLinkHref).toBe('/block-visit-dates/block-date-or-session') |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + it('should redirect to block new session page if this is selected', () => { |
| 88 | + return request(app) |
| 89 | + .post(url) |
| 90 | + .send({ blockType: 'session' }) |
| 91 | + .expect(302) |
| 92 | + .expect('location', '/block-visit-dates/block-new-session') |
| 93 | + .expect(() => { |
| 94 | + expect(flashProvider).toHaveBeenCalledTimes(0) |
| 95 | + expect(sessionData.blockDateOrSession.backLinkHref).toBe('/block-visit-dates/block-date-or-session') |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should set form validation errors and redirect to same page', () => { |
| 100 | + const expectedValidationError: FieldValidationError = { |
| 101 | + location: 'body', |
| 102 | + msg: 'No answer selected', |
| 103 | + path: 'blockType', |
| 104 | + type: 'field', |
| 105 | + value: 'invalid', |
| 106 | + } |
| 107 | + |
| 108 | + return request(app) |
| 109 | + .post(url) |
| 110 | + .send({ blockType: 'invalid' }) |
| 111 | + .expect(302) |
| 112 | + .expect('location', '/block-visit-dates/block-date-or-session') |
| 113 | + .expect(() => { |
| 114 | + expect(flashProvider).toHaveBeenCalledWith('errors', [expectedValidationError]) |
| 115 | + expect(flashProvider).toHaveBeenCalledTimes(1) |
| 116 | + }) |
| 117 | + }) |
| 118 | + }) |
| 119 | +}) |
0 commit comments