-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
90 lines (71 loc) · 2.86 KB
/
index.spec.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { describe, it, expect, beforeAll } from 'vitest'
const fetch = require('node-fetch')
describe('API', () => {
beforeAll(() => {
require('./index.js')
})
it('success fetch GET /', async () => {
const lng = 120.09575843811
const lat = 23.22219022985
const res = await fetch(`http://localhost:8888?lng=${lng}&lat=${lat}`)
expect(res.status).toBe(200)
const data = await res.json()
expect(data.length).toBe(1)
expect(data[0]).toBeDefined()
expect(typeof data[0].name).toEqual('string')
})
it('failed when wrong query params fetch GET /', async () => {
const lng = 120.09575843811
const lat = 23.22219022985
const res = await fetch(`http://localhost:8888?ln=${lng}&la=${lat}`)
expect(res.status).toBe(400)
const data = await res.json()
expect(data.message).toBeDefined()
})
it('success fetch POST /list', async () => {
const points = [
{ lng: 120.09575843811, lat: 23.22219022985 },
{ lng: 121.5526163, lat: 24.9880455 }
]
const res = await fetch(`http://localhost:8888/list`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(points) })
expect(res.status).toBe(200)
const data = await res.json()
expect(data.length).toBe(2)
expect(Array.isArray(data[0])).toBeTruthy()
expect(data[0].length).toBe(1)
expect(typeof data[0][0].name).toEqual('string')
expect(Array.isArray(data[1])).toBeTruthy()
expect(data[1].length).toBe(1)
expect(typeof data[1][0].name).toEqual('string')
})
it('failed when wrong body content fetch POST /list', async () => {
const points = [
{ ln: 120.09575843811, lat: 23.22219022985 },
{ lng: 121.5526163, lat: 24.9880455 }
]
const res = await fetch(`http://localhost:8888/list`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(points) })
expect(res.status).toBe(400)
const data = await res.json()
expect(data.message).toBeDefined()
})
it('part failed when system error fetch POST /list', async () => {
const points = [
{ lng: 120.09575843811, lat: 23.22219022985 },
{ lng: 121.5526163, lat: 24.9880455 },
{ lng: 120, lat: 23 },
]
const res = await fetch(`http://localhost:8888/list`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(points) })
expect(res.status).toBe(200)
const data = await res.json()
expect(data.length).toBe(3)
expect(Array.isArray(data[0])).toBeTruthy()
expect(data[0].length).toBe(1)
expect(typeof data[0][0].name).toEqual('string')
expect(Array.isArray(data[1])).toBeTruthy()
expect(data[1].length).toBe(1)
expect(typeof data[1][0].name).toEqual('string')
expect(Array.isArray(data[2])).toBeFalsy()
expect(data[2].message).toBeDefined()
expect(typeof data[2].message).toEqual('string')
})
})