|
| 1 | +// Test file uses `require()` style imports because the BluetoothTerminal module is exported only as a CommonJS module. |
| 2 | +/* eslint-disable @typescript-eslint/no-require-imports */ |
| 3 | +const {DeviceMock, WebBluetoothMock} = require('web-bluetooth-mock'); |
| 4 | + |
| 5 | +const BluetoothTerminal = require('./BluetoothTerminal'); |
| 6 | + |
| 7 | +const unexpectedlyDisconnect = (device: any) => { // eslint-disable-line @typescript-eslint/no-explicit-any |
| 8 | + // Simulate an unexpected disconnection by dispatching the corresponding Web Bluetooth API event. |
| 9 | + device.dispatchEvent(new CustomEvent('gattserverdisconnected')); |
| 10 | +}; |
| 11 | + |
| 12 | +describe('Connection', () => { |
| 13 | + // Using `any` type to access private members for testing purposes. This allows for thorough testing of the internal |
| 14 | + // state and behavior while maintaining strong encapsulation in the production code. |
| 15 | + let bt: any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 16 | + let device: any; // eslint-disable-line @typescript-eslint/no-explicit-any |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + bt = new BluetoothTerminal(); |
| 20 | + device = new DeviceMock('Simon', [0xFFE0]); |
| 21 | + navigator.bluetooth = new WebBluetoothMock([device]); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('When connecting to the device...', () => { |
| 25 | + beforeEach(() => { |
| 26 | + jest.spyOn(navigator.bluetooth, 'requestDevice'); |
| 27 | + jest.spyOn(device.gatt, 'connect'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should open the browser Bluetooth device picker and connect to the selected device', async () => { |
| 31 | + await expect(bt.connect()).resolves.toBeUndefined(); |
| 32 | + expect(navigator.bluetooth.requestDevice).toHaveBeenCalledTimes(1); |
| 33 | + expect(device.gatt.connect).toHaveBeenCalledTimes(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should connect to the previously selected device on subsequent calls to avoid prompting the user multiple ' + |
| 37 | + 'times', async () => { |
| 38 | + // The first connection should open the browser Bluetooth device picker. |
| 39 | + await expect(bt.connect()).resolves.toBeUndefined(); |
| 40 | + // The second connection should connect to the previously selected device. |
| 41 | + await expect(bt.connect()).resolves.toBeUndefined(); |
| 42 | + expect(navigator.bluetooth.requestDevice).toHaveBeenCalledTimes(1); |
| 43 | + expect(device.gatt.connect).toHaveBeenCalledTimes(2); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should invoke the onConnect callback after connection', async () => { |
| 47 | + const callback = jest.fn(); |
| 48 | + bt.onConnect(callback); |
| 49 | + |
| 50 | + await expect(bt.connect()).resolves.toBeUndefined(); |
| 51 | + |
| 52 | + expect(callback).toHaveBeenCalledTimes(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should throw an error when no device with a matching service UUID is available', async () => { |
| 56 | + device = new DeviceMock('Simon', [1234]); |
| 57 | + navigator.bluetooth = new WebBluetoothMock([device]); |
| 58 | + jest.spyOn(navigator.bluetooth, 'requestDevice'); |
| 59 | + |
| 60 | + await expect(bt.connect()).rejects.toThrow(); |
| 61 | + expect(navigator.bluetooth.requestDevice).toHaveBeenCalledTimes(1); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('When disconnecting from the device...', () => { |
| 66 | + beforeEach(() => { |
| 67 | + jest.spyOn(device.gatt, 'disconnect'); |
| 68 | + |
| 69 | + return bt.connect(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should disconnect only once despite subsequent calls', () => { |
| 73 | + bt.disconnect(); |
| 74 | + bt.disconnect(); // The second call shouldn't trigger another disconnect. |
| 75 | + |
| 76 | + expect(device.gatt.disconnect).toHaveBeenCalledTimes(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should invoke the onDisconnect callback after disconnection', () => { |
| 80 | + const callback = jest.fn(); |
| 81 | + bt.onDisconnect(callback); |
| 82 | + |
| 83 | + bt.disconnect(); |
| 84 | + |
| 85 | + expect(callback).toHaveBeenCalledTimes(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should avoid disconnecting when the device unexpectedly disconnects', () => { |
| 89 | + // Simulate an unexpected disconnection by setting the Web Bluetooth API connection flag. |
| 90 | + device.gatt.connected = false; |
| 91 | + |
| 92 | + bt.disconnect(); |
| 93 | + |
| 94 | + expect(device.gatt.disconnect).not.toHaveBeenCalled(); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('When the device unexpectedly disconnects...', () => { |
| 99 | + beforeEach(() => { |
| 100 | + jest.spyOn(device.gatt, 'connect'); |
| 101 | + |
| 102 | + return bt.connect(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should invoke the onDisconnect callback', () => { |
| 106 | + const callback = jest.fn(); |
| 107 | + bt.onDisconnect(callback); |
| 108 | + |
| 109 | + unexpectedlyDisconnect(device); |
| 110 | + |
| 111 | + expect(callback).toHaveBeenCalledTimes(1); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should attempt to reconnect to the device', () => { |
| 115 | + // Verify that connect was already called once during test setup. |
| 116 | + expect(device.gatt.connect).toHaveBeenCalledTimes(1); |
| 117 | + |
| 118 | + unexpectedlyDisconnect(device); |
| 119 | + |
| 120 | + // Verify that a reconnection attempt was made. |
| 121 | + expect(device.gatt.connect).toHaveBeenCalledTimes(2); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should invoke the onConnect callback after reconnection', (done) => { |
| 125 | + const callback = jest.fn(); |
| 126 | + bt.onConnect(callback); |
| 127 | + |
| 128 | + unexpectedlyDisconnect(device); |
| 129 | + |
| 130 | + // Using `setTimeout()` to defer verification until after the current call stack has completed. This allows the |
| 131 | + // reconnection Promise chain to resolve before checking if the onConnect callback was triggered. Without this, |
| 132 | + // the check can happen before the async reconnection process finishes. |
| 133 | + setTimeout(() => { |
| 134 | + expect(callback).toHaveBeenCalledTimes(1); |
| 135 | + // Signal Jest that the asynchronous test is complete. |
| 136 | + done(); |
| 137 | + }, 0); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should log an error when a reconnection attempt fails', (done) => { |
| 141 | + jest.spyOn(bt, '_logError'); |
| 142 | + |
| 143 | + // Verify that connect was already called once during test setup. |
| 144 | + expect(device.gatt.connect).toHaveBeenCalledTimes(1); |
| 145 | + |
| 146 | + // Mock the connect method to simulate a connection failure. |
| 147 | + device.gatt.connect = jest.fn(() => Promise.reject(new Error('Simulated error'))); |
| 148 | + |
| 149 | + unexpectedlyDisconnect(device); |
| 150 | + |
| 151 | + // Verify that a reconnection attempt was made. Using `1` since the connect method was just mocked. |
| 152 | + expect(device.gatt.connect).toHaveBeenCalledTimes(1); |
| 153 | + |
| 154 | + // Using `setTimeout()` to wait for the Promise rejection to be processed. This allows for verifying asynchronous |
| 155 | + // behavior after the reconnection failure. |
| 156 | + setTimeout(() => { |
| 157 | + // Verify that the error was properly logged. |
| 158 | + expect(bt._logError).toHaveBeenLastCalledWith( |
| 159 | + '_gattServerDisconnectedListener', |
| 160 | + new Error('Simulated error'), |
| 161 | + expect.any(Function), |
| 162 | + ); |
| 163 | + // Signal Jest that the asynchronous test is complete. |
| 164 | + done(); |
| 165 | + }, 0); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe('When getting the device name...', () => { |
| 170 | + it('should return an empty string when no device is connected', () => { |
| 171 | + expect(bt.getDeviceName()).toBe(''); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should return the device name after connection', async () => { |
| 175 | + await bt.connect(); |
| 176 | + |
| 177 | + expect(bt.getDeviceName()).toBe('Simon'); |
| 178 | + }); |
| 179 | + |
| 180 | + it('should return the device name when the device unexpectedly disconnects', async () => { |
| 181 | + await bt.connect(); |
| 182 | + |
| 183 | + // Simulate an unexpected disconnection by setting the Web Bluetooth API connection flag. |
| 184 | + device.gatt.connected = false; |
| 185 | + |
| 186 | + expect(bt.getDeviceName()).toBe('Simon'); |
| 187 | + }); |
| 188 | + }); |
| 189 | +}); |
0 commit comments