main
js 58 lines 1.4 KB
Raw
1 /**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @flow
8 */
9
10 describe('connectToDevTools', () => {
11 let connectToDevTools;
12 let hook;
13
14 beforeEach(() => {
15 jest.resetModules();
16 delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
17
18 const backend = require('react-devtools-core/src/backend');
19 backend.initialize();
20 connectToDevTools = backend.connectToDevTools;
21 hook = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
22 });
23
24 afterEach(() => {
25 jest.clearAllTimers();
26 delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
27 });
28
29 function createWebSocket(): WebSocket {
30 return {
31 CLOSED: 3,
32 OPEN: 1,
33 readyState: 1,
34 send: jest.fn(),
35 } as any as WebSocket;
36 }
37
38 it('shuts down cleanly when the WebSocket closes', () => {
39 const websocket = createWebSocket();
40 const onShutdown = jest.fn();
41 const unsubscribe = hook.sub('shutdown', onShutdown);
42
43 try {
44 connectToDevTools({websocket});
45 websocket.onopen();
46 websocket.readyState = websocket.CLOSED;
47 websocket.onclose();
48 jest.runAllTimers();
49
50 expect(onShutdown).toHaveBeenCalledTimes(1);
51 expect(global.consoleWarnMock).not.toHaveBeenCalledWith(
52 'Bridge was already shutdown.',
53 );
54 } finally {
55 unsubscribe();
56 }
57 });
58 });