|
| 1 | +import React from 'react' |
| 2 | +import {rest} from 'msw' |
| 3 | +import {render, screen, waitFor} from '@testing-library/react' |
| 4 | +import {CandidatesPageObject} from '../../test/page_objects/Candidates' |
| 5 | +import CandidatesPage from '../components/candidates/CandidatesPage' |
| 6 | +import {candidates as candidates_fixture} from '../../test/fixtures/candidates.json' |
| 7 | +import {httpSetup} from '../../test/helpers/httpSetup' |
| 8 | + |
| 9 | +describe('Candidates page', () => { |
| 10 | + let editCandidateRequestPayload = {} |
| 11 | + let createCandidateRequestPayload = {} |
| 12 | + const page = new CandidatesPageObject(screen) |
| 13 | + const serverMock = new httpSetup() |
| 14 | + |
| 15 | + beforeAll(() => serverMock.listen()) |
| 16 | + afterAll(() => serverMock.close()) |
| 17 | + |
| 18 | + it('displays an error when no candidates are found', async () => { |
| 19 | + serverMock.httpMockGet('candidates', []) |
| 20 | + render(<CandidatesPage />) |
| 21 | + |
| 22 | + await page.initialLoad() |
| 23 | + await page.pageErrors('No candidates found') |
| 24 | + }) |
| 25 | + |
| 26 | + it('displays a list of candidates when available', async () => { |
| 27 | + serverMock.httpMockGet('candidates', candidates_fixture) |
| 28 | + render(<CandidatesPage />) |
| 29 | + |
| 30 | + await page.initialLoad() |
| 31 | + page.validateElementLength('candidate-item', candidates_fixture.length) |
| 32 | + }) |
| 33 | + |
| 34 | + it('updates an existing candidate', async () => { |
| 35 | + const testCandidate = candidates_fixture[0] |
| 36 | + const expectedEditCandidateRequestPayload = { |
| 37 | + |
| 38 | + name: 'Bart Simpson', |
| 39 | + notes: 'Hello World', |
| 40 | + phone: '393281929', |
| 41 | + step: 'Rejected', |
| 42 | + } |
| 43 | + |
| 44 | + serverMock.server.use( |
| 45 | + rest.put(`/api/candidates/${testCandidate.id}`, async (req, res, ctx) => { |
| 46 | + editCandidateRequestPayload = req.body |
| 47 | + return res(ctx.json({id: 'f'})) |
| 48 | + }), |
| 49 | + ) |
| 50 | + |
| 51 | + render(<CandidatesPage />) |
| 52 | + |
| 53 | + await page.initialLoad() |
| 54 | + |
| 55 | + page.clickCandidate(testCandidate.name) |
| 56 | + page.clickEdit(testCandidate.id) |
| 57 | + page.inputEmail('[email protected]') |
| 58 | + page.inputName('Bart Simpson') |
| 59 | + page.inputNotes('Hello World') |
| 60 | + page.inputPhoneNumber('393281929') |
| 61 | + page.inputStep('Rejected') |
| 62 | + page.clickSaveChanges() |
| 63 | + |
| 64 | + await waitFor(() => { |
| 65 | + expect(editCandidateRequestPayload).toMatchObject( |
| 66 | + expectedEditCandidateRequestPayload, |
| 67 | + ) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + it('creates a new candidate', async () => { |
| 72 | + const expectedCreateCandidateRequestPayload = { |
| 73 | + |
| 74 | + name: 'Bart Simpson', |
| 75 | + notes: 'Hello World', |
| 76 | + phone: '393281929', |
| 77 | + step: 'Rejected', |
| 78 | + } |
| 79 | + |
| 80 | + serverMock.server.use( |
| 81 | + rest.post('/api/candidates/', async (req, res, ctx) => { |
| 82 | + createCandidateRequestPayload = req.body |
| 83 | + return res(ctx.json({id: 'f'})) |
| 84 | + }), |
| 85 | + ) |
| 86 | + |
| 87 | + render(<CandidatesPage />) |
| 88 | + |
| 89 | + await page.initialLoad() |
| 90 | + |
| 91 | + page.clickAdd() |
| 92 | + page.inputEmail('[email protected]') |
| 93 | + page.inputName('Bart Simpson') |
| 94 | + page.inputNotes('Hello World') |
| 95 | + page.inputPhoneNumber('393281929') |
| 96 | + page.inputStep('Rejected') |
| 97 | + page.clickSubmit() |
| 98 | + |
| 99 | + await waitFor(() => { |
| 100 | + expect(createCandidateRequestPayload).toMatchObject( |
| 101 | + expectedCreateCandidateRequestPayload, |
| 102 | + ) |
| 103 | + }) |
| 104 | + }) |
| 105 | +}) |
0 commit comments