-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.mjs
148 lines (131 loc) · 3.69 KB
/
index.spec.mjs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import 'isomorphic-fetch';
import 'regenerator-runtime/runtime';
import createTestServer from 'create-test-server';
import noutaa, {
GET,
POST,
PUT,
PATCH,
HEAD,
DELETE,
} from './index';
const beerList = [
{
name: 'La Réserve',
brand: 'Grimbergen',
strength: 8.5,
},
{
name: 'Brooklyn Pilsner',
brand: 'Brooklyn Brewery',
strength: 5.1,
type: 'pilsner',
},
{
name: 'Brooklyn East India Pale Ale',
brand: 'Brooklyn Brewery',
strength: 6.9,
type: 'ipa',
},
];
const handleBeer = async (req, res) => {
const beer = beerList[Number(req.params.id)];
if (beer) {
res.send(beer);
} else {
res.status(404).send();
}
};
const handleBeerList = async (req, res) => {
let order = beerList;
if (req.body && req.body.constructor === Array) {
order = order.filter((beer, index) => req.body.includes(index));
} else {
const min = req.body.min || req.query.min || 0;
const max = req.body.max || req.query.max || 100;
order = order.filter(({ strength }) => strength >= min);
order = order.filter(({ strength }) => strength <= max);
}
res.send(order);
};
describe('noutaa', () => {
let server;
beforeAll(async () => {
server = await createTestServer();
server.all('/beer', handleBeerList);
server.all('/beer/:id', handleBeer);
});
afterAll(async () => server.close());
it('Simple fetch()', async () => {
const response = await noutaa(`${server.url}/beer`);
const json = await response.json();
expect(json).toEqual(beerList);
});
it('With query params', async () => {
const response = await GET(`${server.url}/beer`, {
params: { max: 6 },
});
const json = await response.json();
expect(json).toEqual([beerList[1]]);
});
it('With merged query params', async () => {
const response = await GET(`${server.url}/beer?max=7`, {
params: { min: 6 },
});
const json = await response.json();
expect(json).toEqual([beerList[2]]);
});
it('Sending JSON Object in "json" param', async () => {
const response = await POST(`${server.url}/beer`, {
json: { min: 8 },
});
const json = await response.json();
expect(json).toEqual([beerList[0]]);
});
it('Sending JSON Array in "json" param', async () => {
const response = await PUT(`${server.url}/beer`, {
json: [0, 2],
});
const json = await response.json();
expect(json).toEqual([beerList[0], beerList[2]]);
});
it('Sending Object in "urlencoded" param', async () => {
const response = await PATCH(`${server.url}/beer`, {
urlencoded: { max: 6 },
});
const json = await response.json();
expect(json).toEqual([beerList[1]]);
});
it('Sending URLSearchParams in "urlencoded" param', async () => {
const urlencoded = new URLSearchParams();
urlencoded.append('max', 6);
const response = await POST(`${server.url}/beer`, {
urlencoded,
});
const json = await response.json();
expect(json).toEqual([beerList[1]]);
});
it('Throwing responses that are not OK (out of 200s range)', async () => {
try {
await DELETE(`${server.url}/beer/999`);
} catch (error) {
if (error.code) {
expect(error.code).toEqual(404);
}
}
});
it('Parse JSON responses with "accept" param', async () => {
const response = await GET(`${server.url}/beer/0`, {
accept: 'json',
});
expect(response).toEqual(beerList[0]);
});
it('HEAD method', async () => {
const response = await HEAD(`${server.url}/beer`);
expect(response.status).toEqual(200);
});
it('DELETE() function', async () => {
const response = await DELETE(`${server.url}/beer/1`);
expect(response.status).toEqual(200);
});
});