-
Notifications
You must be signed in to change notification settings - Fork 967
Expand file tree
/
Copy pathuseCustomCommands.test.tsx
More file actions
120 lines (101 loc) · 2.84 KB
/
useCustomCommands.test.tsx
File metadata and controls
120 lines (101 loc) · 2.84 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
117
118
119
120
/* eslint-disable react/display-name */
import React from "react"
import { act, renderHook } from "@testing-library/react-hooks"
import ReactotronContext from "../Reactotron"
import { CommandType } from "reactotron-core-contract"
import useCustomCommands from "./useCustomCommands"
function buildContextValues({ addCommandListener = null } = {}) {
return {
commands: [],
sendCommand: jest.fn(),
clearCommands: jest.fn(),
addCommandListener: addCommandListener || jest.fn(),
isDispatchModalOpen: false,
dispatchModalInitialAction: "",
openDispatchModal: jest.fn(),
closeDispatchModal: jest.fn(),
isSubscriptionModalOpen: false,
openSubscriptionModal: jest.fn(),
closeSubscriptionModal: jest.fn(),
isDiagnosticModalOpen: false,
openDiagnosticModal: jest.fn(),
closeDiagnosticModal: jest.fn(),
}
}
describe("contexts/CustomCommands/useCustomCommands", () => {
it("should add and remove custom commands", () => {
let addCallback = null
const contextValues = buildContextValues({
addCommandListener: (callback) => {
addCallback = callback
},
})
const { result } = renderHook(() => useCustomCommands(), {
wrapper: ({ children }) => (
<ReactotronContext.Provider value={contextValues}>{children}</ReactotronContext.Provider>
),
})
expect(result.current.customCommands).toEqual([])
act(() => {
addCallback({
type: CommandType.CustomCommandRegister,
clientId: "1234",
payload: {
id: 0,
},
})
})
expect(result.current.customCommands).toEqual([
{
clientId: "1234",
id: 0,
},
])
act(() => {
addCallback({
type: CommandType.CustomCommandUnregister,
clientId: "1234",
payload: {
id: 0,
},
})
})
expect(result.current.customCommands).toEqual([])
})
it("should clear all commands after a reconnect", () => {
let addCallback = null
const contextValues = buildContextValues({
addCommandListener: (callback) => {
addCallback = callback
},
})
const { result } = renderHook(() => useCustomCommands(), {
wrapper: ({ children }) => (
<ReactotronContext.Provider value={contextValues}>{children}</ReactotronContext.Provider>
),
})
expect(result.current.customCommands).toEqual([])
act(() => {
addCallback({
type: CommandType.CustomCommandRegister,
clientId: "1234",
payload: {
id: 0,
},
})
})
expect(result.current.customCommands).toEqual([
{
clientId: "1234",
id: 0,
},
])
act(() => {
addCallback({
type: CommandType.ClientIntro,
clientId: "1234",
})
})
expect(result.current.customCommands).toEqual([])
})
})