-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurl-map.test.ts
More file actions
116 lines (105 loc) · 4.56 KB
/
url-map.test.ts
File metadata and controls
116 lines (105 loc) · 4.56 KB
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
import { beforeEach, expect, test } from 'bun:test'
const urlMap = {
foo: {
getUsers: { method: 'GET' as const, url: '/users' },
createUser: { method: 'POST' as const, url: '/users' },
updateUser: { method: 'PUT' as const, url: '/users/{id}' },
deleteUser: { method: 'DELETE' as const, url: '/users/{id}' },
},
}
beforeEach(() => {
// reset the module cache
})
const random = Math.random()
test.each([
['getUsers', '/users', JSON.stringify(urlMap)],
['createUser', '/users', JSON.stringify(urlMap)],
['updateUser', '/users/{id}', JSON.stringify(urlMap)],
['deleteUser', '/users/{id}', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns url for existing key: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
// Add query parameter to bypass module cache and reload
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo')?.url).toBe(expected)
})
test.each([
['nonExistentKey', 'nonExistentKey', JSON.stringify(urlMap)],
['unknown', 'unknown', JSON.stringify(urlMap)],
['', '', JSON.stringify(urlMap)],
['/users', '/users', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns key itself when key does not exist: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo').url).toBe(expected)
})
test.each([
['getUsers', { method: 'GET', url: '/users' }, JSON.stringify(urlMap)],
['createUser', { method: 'POST', url: '/users' }, JSON.stringify(urlMap)],
['updateUser', { method: 'PUT', url: '/users/{id}' }, JSON.stringify(urlMap)],
[
'deleteUser',
{ method: 'DELETE', url: '/users/{id}' },
JSON.stringify(urlMap),
],
] as const)('getApiEndpointInfo returns UrlMapValue for existing key: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo')).toEqual(expected)
})
test.each([
[
'nonExistentKey',
{ method: 'GET', url: 'nonExistentKey' },
JSON.stringify(urlMap),
],
['unknown', { method: 'GET', url: 'unknown' }, JSON.stringify(urlMap)],
['', { method: 'GET', url: '' }, JSON.stringify(urlMap)],
['/users', { method: 'GET', url: '/users' }, JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns default for non-existent key: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map`)
expect(getApiEndpointInfo(key, '', 'GET')).toEqual(expected)
})
test.each([
['anyKey', 'anyKey', '{}'],
['test', 'test', '{}'],
] as const)('getApiEndpointInfo works with empty URL map: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo', 'GET').url).toBe(expected)
})
test.each([
['anyKey', { method: 'GET', url: 'anyKey' }, '{}'],
['test', { method: 'GET', url: 'test' }, '{}'],
] as const)('getApiEndpointInfo works with empty URL map: %s -> %s', async (key, expected, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo', 'GET')).toEqual(expected)
})
test.each([
[['GET', 'anyKey'], '{}'],
[['POST', 'anyKey'], '{}'],
[['PUT', 'anyKey'], '{}'],
[['DELETE', 'anyKey'], '{}'],
[['PATCH', 'anyKey'], '{}'],
] as const)('getApiEndpointInfo works with empty URL map: %s -> %s', async (key, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key[1], 'foo', key[0]).method).toEqual(key[0])
})
test.each([
['anyKey', 'anyKey'],
['test', 'test'],
] as const)('getApiEndpointInfo works when DEVUP_API_URL_MAP is not set: %s -> %s', async (key, expected) => {
delete process.env.DEVUP_API_URL_MAP
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo', 'GET').url).toBe(expected)
})
test.each([
['anyKey', 'anyKey'],
['test', 'test'],
] as const)('getApiEndpointInfo works when DEVUP_API_URL_MAP is not set: %s -> %s', async (key, expected) => {
delete process.env.DEVUP_API_URL_MAP
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo', 'GET').url).toBe(expected)
})