forked from mswjs/msw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listHandlers.test.ts
78 lines (65 loc) · 2.09 KB
/
listHandlers.test.ts
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
/**
* @jest-environment node
*/
import { rest, graphql } from 'msw'
import { setupServer } from 'msw/node'
const resolver = () => null
const github = graphql.link('https://api.github.com')
const server = setupServer(
rest.get('https://test.mswjs.io/book/:bookId', resolver),
graphql.query('GetUser', resolver),
graphql.mutation('UpdatePost', resolver),
graphql.operation(resolver),
github.query('GetRepo', resolver),
github.operation(resolver),
)
beforeAll(() => {
server.listen()
})
afterEach(() => {
server.resetHandlers()
})
afterAll(() => {
server.close()
})
test('lists all current request handlers', () => {
const handlers = server.listHandlers()
const handlerHeaders = handlers.map((handler) => handler.info.header)
expect(handlerHeaders).toEqual([
'GET https://test.mswjs.io/book/:bookId',
'query GetUser (origin: *)',
'mutation UpdatePost (origin: *)',
'all (origin: *)',
'query GetRepo (origin: https://api.github.com)',
'all (origin: https://api.github.com)',
])
})
test('forbids from modifying the list of handlers', () => {
const handlers = server.listHandlers()
expect(() => {
// @ts-expect-error Intentional runtime misusage.
handlers[0] = 1
}).toThrow(/Cannot assign to read only property '\d+' of object/)
expect(() => {
// @ts-expect-error Intentional runtime misusage.
handlers.push(1)
}).toThrow(/Cannot add property \d+, object is not extensible/)
})
test('includes runtime request handlers when listing handlers', () => {
server.use(
rest.get('https://test.mswjs.io/book/:bookId', resolver),
graphql.query('GetRandomNumber', resolver),
)
const handlers = server.listHandlers()
const handlerHeaders = handlers.map((handler) => handler.info.header)
expect(handlerHeaders).toEqual([
'GET https://test.mswjs.io/book/:bookId',
'query GetRandomNumber (origin: *)',
'GET https://test.mswjs.io/book/:bookId',
'query GetUser (origin: *)',
'mutation UpdatePost (origin: *)',
'all (origin: *)',
'query GetRepo (origin: https://api.github.com)',
'all (origin: https://api.github.com)',
])
})