/
test.js
76 lines (70 loc) · 2.85 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const test = require('ava')
const express = require('express')
const request = require('supertest')
const quickfixtureMiddleware = require('./index.js')
const path = require('path')
test.beforeEach(t => {
t.context.server = express()
})
test('it loads', t => {
const instance = quickfixtureMiddleware('./fixtures')
t.is(typeof(t.context.server.use(instance)),'function')
})
test("it throws an error if the fixture directory is not specified", t => {
const error = t.throws(() => quickfixtureMiddleware(), TypeError)
t.is(error.message, 'Fixture path required and must be a string')
})
test("it throws an error if the fixture directory is not a string", t => {
const error = t.throws(() => quickfixtureMiddleware({ directory: 'fixtures' }), TypeError)
t.is(error.message, 'Fixture path required and must be a string')
})
test("it throws an error if the fixture directory is not found", t => {
const error = t.throws(() => quickfixtureMiddleware('./foo'), Error)
t.is(error.message, 'Fixture directory specified ./foo does not exist')
})
test('it can GET a fixture using the default function', async t => {
const instance = quickfixtureMiddleware('./fixtures')
t.context.server.use(instance)
const res = await request(t.context.server)
.get('/foo/bar?baz=buz')
t.is(res.status, 200)
t.deepEqual(res.body, { "success": true })
})
test('it ignores POSTs using default function', async t => {
const instance = quickfixtureMiddleware('./fixtures')
t.context.server.use(instance)
t.context.server.post('/post-test', (req, res, next) => res.sendStatus(418))
const getRes = await request(t.context.server)
.get('/post-test')
t.is(getRes.status, 404)
const postRes = await request(t.context.server)
.post('/post-test')
t.is(postRes.status, 418)
})
test('it accepts a custom function', async t => {
const instance = quickfixtureMiddleware('./fixtures', ({req, fixturesDir}) => {
if (req.method === 'POST') return path.join(fixturesDir, 'foo--bar__baz=buz.json')
})
t.context.server.use(instance)
t.context.server.post('/post-test', (req, res, next) => res.sendStatus(418))
const getRes = await request(t.context.server)
.get('/post-test')
t.is(getRes.status, 404)
const postRes = await request(t.context.server)
.post('/post-test')
t.is(postRes.status, 200)
})
test('it accepts a custom function that returns a fixture-literal', async t => {
const instance = quickfixtureMiddleware('./fixtures', ({req, fixturesDir}) => {
if (req.method === 'POST') return { 'foo': 'bar' }
})
t.context.server.use(instance)
t.context.server.post('/post-test', (req, res, next) => res.sendStatus(418))
const getRes = await request(t.context.server)
.get('/post-test')
t.is(getRes.status, 404)
const postRes = await request(t.context.server)
.post('/post-test')
t.is(postRes.status, 200)
t.deepEqual(postRes.body, { 'foo': 'bar' })
})