forked from mswjs/msw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printHandlers.test.ts
80 lines (66 loc) · 2.31 KB
/
printHandlers.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
79
80
import * as path from 'path'
import { pageWith } from 'page-with'
import { SetupWorkerApi, rest, graphql } from 'msw'
declare namespace window {
export const msw: {
worker: SetupWorkerApi
rest: typeof rest
graphql: typeof graphql
}
}
function createRuntime() {
return pageWith({
example: path.resolve(__dirname, 'printHandlers.mocks.ts'),
})
}
test('lists rest request handlers', async () => {
const { page, consoleSpy } = await createRuntime()
await page.evaluate(() => {
window.msw.worker.printHandlers()
})
const startGroupCollapsed = consoleSpy.get('startGroupCollapsed')
const log = consoleSpy.get('log')
expect(startGroupCollapsed).toHaveLength(6)
expect(startGroupCollapsed).toContain(
'[rest] GET https://test.mswjs.io/book/:bookId',
)
expect(startGroupCollapsed).toContain('[graphql] query GetUser (origin: *)')
expect(startGroupCollapsed).toContain(
'[graphql] mutation UpdatePost (origin: *)',
)
expect(startGroupCollapsed).toContain('[graphql] all (origin: *)')
expect(startGroupCollapsed).toContain(
'[graphql] query GetRepo (origin: https://api.github.com)',
)
expect(startGroupCollapsed).toContain(
'[graphql] all (origin: https://api.github.com)',
)
const matchSuggestions = log.filter((message) => message.startsWith('Match:'))
expect(matchSuggestions).toHaveLength(1)
expect(matchSuggestions).toEqual([
'Match: https://mswjs.io/repl?path=https://test.mswjs.io/book/:bookId',
])
})
test('includes runtime request handlers', async () => {
const { page, consoleSpy } = await createRuntime()
await page.evaluate(() => {
const { worker, rest, graphql } = window.msw
worker.use(
rest.post('/profile', () => null),
graphql.query('SubmitTransaction', () => null),
)
worker.printHandlers()
})
const startGroupCollapsed = consoleSpy.get('startGroupCollapsed')
const log = consoleSpy.get('log')
expect(startGroupCollapsed).toHaveLength(8)
expect(startGroupCollapsed).toContain('[rest] POST /profile')
expect(startGroupCollapsed).toContain(
'[graphql] query SubmitTransaction (origin: *)',
)
const matchSuggestions = log.filter((message) => message.startsWith('Match:'))
expect(matchSuggestions).toHaveLength(2)
expect(matchSuggestions).toContain(
'Match: https://mswjs.io/repl?path=/profile',
)
})