1 | import {serial as test} from 'ava'
|
2 | const { clearModuleCache, stubModule } = require('./runkit')
|
3 |
|
4 | const clearFreddoCache = () => clearModuleCache('../index')
|
5 | const stubGot = (returnsVal) => stubModule('got', returnsVal)
|
6 |
|
7 | let freddo, expr, exists
|
8 |
|
9 | test.beforeEach(t => {
|
10 | clearFreddoCache()
|
11 | })
|
12 |
|
13 | test('body', async t => {
|
14 | stubGot({
|
15 | headers: {},
|
16 | statusCode: '',
|
17 | body: { foo: 'bar' }
|
18 | })
|
19 | ;({ freddo } = require('../index'))
|
20 | t.is(await freddo().body({'foo': 'bar'}), true)
|
21 | })
|
22 |
|
23 | test('body with no match', async t => {
|
24 | stubGot({
|
25 | headers: {},
|
26 | statusCode: '',
|
27 | body: { foo: 'bar' }
|
28 | })
|
29 | ;({ freddo } = require('../index'))
|
30 | const error = await t.throwsAsync(async () => {
|
31 | await freddo()
|
32 | .expect('body', {'foo': 'unicorn'})
|
33 | .ensure()
|
34 | })
|
35 | t.is(error.message, 'Expected key "body" to be {"foo":"unicorn"}, but got {"foo":"bar"}')
|
36 | })
|
37 |
|
38 | test('status code', async t => {
|
39 | stubGot({
|
40 | headers: {},
|
41 | statusCode: 200,
|
42 | body: {}
|
43 | })
|
44 | ;({ freddo } = require('../index'))
|
45 | t.is(await freddo().status(200), true)
|
46 | })
|
47 |
|
48 | test('headers', async t => {
|
49 | stubGot({
|
50 | headers: { 'content-type': 'application/json' },
|
51 | statusCode: '',
|
52 | body: {}
|
53 | })
|
54 | ;({ freddo } = require('../index'))
|
55 | t.is(await freddo().header('content-type', 'application/json'), true)
|
56 | })
|
57 |
|
58 | test('redirectsTo', async t => {
|
59 | stubGot({
|
60 | headers: { location: 'http://www.example.org/' },
|
61 | statusCode: 301,
|
62 | body: {}
|
63 | })
|
64 | ;({ freddo } = require('../index'))
|
65 | t.is(await freddo().redirectsTo('http://www.example.org/'), true)
|
66 | })
|
67 |
|
68 | test('invalid key', async t => {
|
69 | stubGot({
|
70 | headers: {},
|
71 | statusCode: '',
|
72 | body: { foo: 'bar' }
|
73 | })
|
74 | ;({ freddo } = require('../index'))
|
75 | const error = await t.throwsAsync(async () => {
|
76 | await freddo().expect('does-not-exist', '')
|
77 | })
|
78 | t.is(error.message, 'Key "does-not-exist" does not exist')
|
79 | })
|
80 |
|
81 | test('no match without ensure', async t => {
|
82 | stubGot({
|
83 | headers: {},
|
84 | statusCode: '',
|
85 | body: { foo: 'bar' }
|
86 | })
|
87 | ;({ freddo } = require('../index'))
|
88 | t.is(await freddo().expect('body', {'foo': 'unicorn'}), false)
|
89 | })
|
90 |
|
91 | test('expr', async t => {
|
92 | stubGot({
|
93 | headers: {},
|
94 | statusCode: '',
|
95 | body: { foo: 'bar' }
|
96 | })
|
97 | ;({ freddo, expr } = require('../index'))
|
98 | t.is(await freddo().expect(expr('.foo'), ([x]) => x == 'bar'), true)
|
99 | })
|
100 |
|
101 | test('exists', async t => {
|
102 | stubGot({
|
103 | headers: {},
|
104 | statusCode: 200,
|
105 | body: { foo: 'bar' }
|
106 | })
|
107 | ;({ freddo, expr, exists } = require('../index'))
|
108 | t.is(await freddo().body(exists, expr('.foo')), true)
|
109 | })
|
110 |
|
111 | test('pass function as expected value', async t => {
|
112 | stubGot({
|
113 | headers: {},
|
114 | statusCode: 404,
|
115 | body: {}
|
116 | })
|
117 | ;({ freddo } = require('../index'))
|
118 | t.is(await freddo().expect('statusCode', (x) => x == 404), true)
|
119 | })
|
120 |
|
121 | test('pass function that returns boolean and error message', async t => {
|
122 | stubGot({
|
123 | headers: {},
|
124 | statusCode: null,
|
125 | body: {}
|
126 | })
|
127 | ;({ freddo } = require('../index'))
|
128 | const error = await t.throwsAsync(async () => {
|
129 | await freddo()
|
130 | .expect('statusCode', () => {
|
131 | return {
|
132 | result: false,
|
133 | error: 'Some custom error message'
|
134 | }
|
135 | })
|
136 | .ensure()
|
137 | })
|
138 | t.is(error.message, 'Some custom error message')
|
139 | })
|
140 |
|
141 | test('pass function that does not return boolean', async t => {
|
142 | stubGot({
|
143 | headers: {},
|
144 | statusCode: 404,
|
145 | body: {}
|
146 | })
|
147 | ;({ freddo } = require('../index'))
|
148 | const error = await t.throwsAsync(async () => {
|
149 | await freddo()
|
150 | .expect('statusCode', (x) => 'unicorn')
|
151 | .ensure()
|
152 | })
|
153 | t.is(error.message, 'Custom assertion functions must return a boolean or a {result, error} object')
|
154 | }) |
\ | No newline at end of file |