|
| 1 | +const { test } = require('ava') |
| 2 | +const request = require('axios') |
| 3 | +const moment = require('moment') |
| 4 | + |
| 5 | +const server = require('../server') |
| 6 | + |
| 7 | +test.beforeEach(async t => { |
| 8 | + // This runs after each test and other test hooks, even if they failed |
| 9 | + const rdb = require('rethinkdbdash')({ |
| 10 | + servers: [{ host: process.env.RDB_HOST, port: process.env.RDB_PORT }] |
| 11 | + }) |
| 12 | + const tableList = await rdb.tableList().run() |
| 13 | + if (tableList.includes('author')) { |
| 14 | + await rdb.tableDrop('author').run() |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +// Run the server |
| 19 | +server.start({ port: 0 }, (err, fastify) => { |
| 20 | + test.serial( |
| 21 | + 'The authors endpoint should return an empty array when no entries are saved', |
| 22 | + async t => { |
| 23 | + if (err) t.fail() |
| 24 | + const response = await request.get( |
| 25 | + `http://localhost:${fastify.server.address().port}/authors` |
| 26 | + ) |
| 27 | + t.is(response.status, 200) |
| 28 | + t.deepEqual(response.data, []) |
| 29 | + } |
| 30 | + ) |
| 31 | + |
| 32 | + test.serial( |
| 33 | + 'The authors endpoint should accept a POST call with a new author', |
| 34 | + async t => { |
| 35 | + if (err) t.fail() |
| 36 | + const input = { |
| 37 | + display_name: 'David Guijarro', |
| 38 | + |
| 39 | + } |
| 40 | + const output = { |
| 41 | + id: 'david-guijarro', |
| 42 | + display_name: 'David Guijarro', |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + const postResponse = await request.post( |
| 47 | + `http://localhost:${fastify.server.address().port}/authors`, |
| 48 | + input |
| 49 | + ) |
| 50 | + t.is(postResponse.status, 201) |
| 51 | + const getResponse = await request.get( |
| 52 | + `http://localhost:${fastify.server.address().port}/authors` |
| 53 | + ) |
| 54 | + t.is(getResponse.status, 200) |
| 55 | + t.deepEqual(getResponse.data, [output]) |
| 56 | + } |
| 57 | + ) |
| 58 | +}) |
0 commit comments