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('Bridge', () => {
    
  11.   let Bridge;
    
  12. 
    
  13.   beforeEach(() => {
    
  14.     Bridge = require('react-devtools-shared/src/bridge').default;
    
  15.   });
    
  16. 
    
  17.   // @reactVersion >=16.0
    
  18.   it('should shutdown properly', () => {
    
  19.     const wall = {
    
  20.       listen: jest.fn(() => () => {}),
    
  21.       send: jest.fn(),
    
  22.     };
    
  23.     const bridge = new Bridge(wall);
    
  24.     const shutdownCallback = jest.fn();
    
  25.     bridge.addListener('shutdown', shutdownCallback);
    
  26. 
    
  27.     // Check that we're wired up correctly.
    
  28.     bridge.send('reloadAppForProfiling');
    
  29.     jest.runAllTimers();
    
  30.     expect(wall.send).toHaveBeenCalledWith('reloadAppForProfiling');
    
  31. 
    
  32.     // Should flush pending messages and then shut down.
    
  33.     wall.send.mockClear();
    
  34.     bridge.send('update', '1');
    
  35.     bridge.send('update', '2');
    
  36.     bridge.shutdown();
    
  37.     jest.runAllTimers();
    
  38.     expect(wall.send).toHaveBeenCalledWith('update', '1');
    
  39.     expect(wall.send).toHaveBeenCalledWith('update', '2');
    
  40.     expect(wall.send).toHaveBeenCalledWith('shutdown');
    
  41.     expect(shutdownCallback).toHaveBeenCalledTimes(1);
    
  42. 
    
  43.     // Verify that the Bridge doesn't send messages after shutdown.
    
  44.     jest.spyOn(console, 'warn').mockImplementation(() => {});
    
  45.     wall.send.mockClear();
    
  46.     bridge.send('should not send');
    
  47.     jest.runAllTimers();
    
  48.     expect(wall.send).not.toHaveBeenCalled();
    
  49.     expect(console.warn).toHaveBeenCalledWith(
    
  50.       'Cannot send message "should not send" through a Bridge that has been shutdown.',
    
  51.     );
    
  52.   });
    
  53. });