|
1 | 1 | import request from 'supertest'; |
2 | 2 | import { app } from '../app'; |
| 3 | +import { db } from '../database'; |
| 4 | +import type { Result } from 'tinypg'; |
3 | 5 |
|
4 | 6 | const agent = request.agent(app); |
5 | 7 |
|
6 | 8 | describe('/canonicalFields', () => { |
7 | 9 | describe('/', () => { |
8 | | - it('should return HTTP Status Code 200 OK', async () => { |
| 10 | + it('returns an empty array when no data is present', async () => { |
9 | 11 | await agent |
10 | 12 | .get('/canonicalFields') |
11 | | - .expect(200); |
| 13 | + .expect(200, []); |
| 14 | + }); |
| 15 | + |
| 16 | + it('returns all canonical fields present in the database', async () => { |
| 17 | + await db.query(` |
| 18 | + INSERT INTO canonical_fields ( |
| 19 | + label, |
| 20 | + short_code, |
| 21 | + data_type, |
| 22 | + created_at |
| 23 | + ) |
| 24 | + VALUES |
| 25 | + ( 'First Name', 'firstName', 'string', '2022-07-20 12:00:00+0000' ), |
| 26 | + ( 'Last Name', 'lastName', 'string', '2022-07-20 12:00:00+0000' ); |
| 27 | + `); |
| 28 | + await agent |
| 29 | + .get('/canonicalFields') |
| 30 | + .expect( |
| 31 | + 200, |
| 32 | + [ |
| 33 | + { |
| 34 | + createdAt: '2022-07-20T12:00:00.000Z', |
| 35 | + dataType: 'string', |
| 36 | + id: 1, |
| 37 | + label: 'First Name', |
| 38 | + shortCode: 'firstName', |
| 39 | + }, |
| 40 | + { |
| 41 | + createdAt: '2022-07-20T12:00:00.000Z', |
| 42 | + dataType: 'string', |
| 43 | + id: 2, |
| 44 | + label: 'Last Name', |
| 45 | + shortCode: 'lastName', |
| 46 | + }, |
| 47 | + ], |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should error if the database returns an unexpected data structure', async () => { |
| 52 | + jest.spyOn(db, 'sql') |
| 53 | + .mockImplementationOnce(async () => ({ |
| 54 | + rows: [{ foo: 'not a valid result' }], |
| 55 | + }) as Result<object>); |
| 56 | + const result = await agent |
| 57 | + .get('/canonicalFields') |
| 58 | + .expect(500); |
| 59 | + expect(result.body).toMatchObject({ |
| 60 | + name: 'ValidationError', |
| 61 | + errors: expect.any(Array) as unknown[], |
| 62 | + }); |
12 | 63 | }); |
13 | 64 | }); |
14 | 65 | }); |
0 commit comments