-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
71 lines (62 loc) · 2.13 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
const { Response } = require('node-fetch'),
{ parse } = require('url')
url = 'http://localhost:2525'
require = require('esm')(module)
const { default: queryJSON } = require('.')
const user = {
"id": 1,
"name": "George Washington",
"username": "ceo01",
"email": "[email protected]",
},
users = [
{
"id": 1,
"name": "George Washington",
"username": "ceo01",
"email": "[email protected]",
},
{
"id": 2,
"name": "John Adams",
"username": "ceo02",
"email": "[email protected]",
}
]
global.fetch = jest.fn(url => {
const { pathname } = parse(url)
switch (pathname) {
case '/':
return new Response('HOME')
case '/one':
return new Response(JSON.stringify(user))
case '/multiple':
return new Response(JSON.stringify(users))
case '/numbers':
return new Response(JSON.stringify([1, 2, 3]))
default:
return new Response('NF')
}
})
test('Returns a failure object for a missing target', async () => {
const answer = await queryJSON(url, 'email')
expect(answer).toHaveProperty('failure')
const answer2 = await queryJSON(url + '/numbers', 'email')
expect(answer2).toHaveProperty('failure')
})
test('Returns a success object when the target property is located', async () => {
const answer = await queryJSON(url + '/one', 'email')
expect(answer).toHaveProperty('success')
})
test('Returns a success object when the target property is located in an array', async () => {
const answer = await queryJSON(url + '/multiple', 'username')
expect(answer).toHaveProperty('success')
})
test('Returns a success property that matches the vendor response', async () => {
const answer = await queryJSON(url + '/multiple', 'username')
expect(answer.success).toEqual(users)
})
test('Returns a failure vendor_response property that matches the vendor response', async () => {
const answer = await queryJSON(url + '/oops', 'data')
expect(answer.failure.vendor_response).toEqual('NF')
})