diff --git a/src/ble/reducers.test.ts b/src/ble/reducers.test.ts new file mode 100644 index 00000000..cb5edd73 --- /dev/null +++ b/src/ble/reducers.test.ts @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2021 The Pybricks Authors + +import { Action } from '../actions'; +import { + BleDeviceDidFailToConnectReason, + connect, + didConnect, + didDisconnect, + didFailToConnect, + disconnect, +} from './actions'; +import reducers, { BleConnectionState } from './reducers'; + +type State = ReturnType; + +test('initial state', () => { + expect(reducers(undefined, {} as Action)).toMatchInlineSnapshot(` + Object { + "connection": "ble.connection.state.disconnected", + } + `); +}); + +test('connection', () => { + expect( + reducers({ connection: BleConnectionState.Disconnected } as State, connect()) + .connection, + ).toBe(BleConnectionState.Connecting); + expect( + reducers({ connection: BleConnectionState.Connecting } as State, didConnect()) + .connection, + ).toBe(BleConnectionState.Connected); + expect( + reducers( + { connection: BleConnectionState.Connecting } as State, + didFailToConnect({} as BleDeviceDidFailToConnectReason), + ).connection, + ).toBe(BleConnectionState.Disconnected); + expect( + reducers({ connection: BleConnectionState.Connected } as State, disconnect()) + .connection, + ).toBe(BleConnectionState.Disconnecting); + expect( + reducers( + { connection: BleConnectionState.Disconnecting } as State, + didDisconnect(), + ).connection, + ).toBe(BleConnectionState.Disconnected); +});