|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2021 The Pybricks Authors |
| 3 | + |
| 4 | +import { IToaster } from '@blueprintjs/core'; |
| 5 | +import { AsyncSaga } from '../../test'; |
| 6 | +import { Action } from '../actions'; |
| 7 | +import { |
| 8 | + BleDeviceFailToConnectReasonType, |
| 9 | + didFailToConnect as bleDidFailToConnect, |
| 10 | +} from '../actions/ble'; |
| 11 | +import { storageChanged } from '../actions/editor'; |
| 12 | +import { |
| 13 | + BootloaderConnectionFailureReason, |
| 14 | + didFailToConnect as bootloaderDidFailToConnect, |
| 15 | +} from '../actions/lwp3-bootloader'; |
| 16 | +import { didFailToCompile } from '../actions/mpy'; |
| 17 | +import { add } from '../actions/notification'; |
| 18 | +import { didSucceed, didUpdate } from '../actions/service-worker'; |
| 19 | +import notification from './notification'; |
| 20 | + |
| 21 | +test.each([ |
| 22 | + bleDidFailToConnect({ reason: BleDeviceFailToConnectReasonType.NoWebBluetooth }), |
| 23 | + bleDidFailToConnect({ reason: BleDeviceFailToConnectReasonType.NoGatt }), |
| 24 | + bleDidFailToConnect({ reason: BleDeviceFailToConnectReasonType.NoService }), |
| 25 | + bleDidFailToConnect({ |
| 26 | + reason: BleDeviceFailToConnectReasonType.Unknown, |
| 27 | + err: { name: 'test', message: 'unknown' }, |
| 28 | + }), |
| 29 | + bootloaderDidFailToConnect(BootloaderConnectionFailureReason.Unknown), |
| 30 | + bootloaderDidFailToConnect(BootloaderConnectionFailureReason.NoWebBluetooth), |
| 31 | + bootloaderDidFailToConnect(BootloaderConnectionFailureReason.GattServiceNotFound), |
| 32 | + storageChanged('test'), |
| 33 | + didFailToCompile('reason'), |
| 34 | + add('warning', 'message'), |
| 35 | + add('error', 'message', 'url'), |
| 36 | + didUpdate({} as ServiceWorkerRegistration), |
| 37 | + didSucceed({} as ServiceWorkerRegistration), |
| 38 | +])('actions that should show notification: %o', async (action: Action) => { |
| 39 | + const getToasts = jest.fn().mockReturnValue([]); |
| 40 | + const show = jest.fn(); |
| 41 | + const dismiss = jest.fn(); |
| 42 | + const clear = jest.fn(); |
| 43 | + |
| 44 | + const toaster: IToaster = { |
| 45 | + getToasts, |
| 46 | + show, |
| 47 | + dismiss, |
| 48 | + clear, |
| 49 | + }; |
| 50 | + |
| 51 | + const saga = new AsyncSaga(notification, { notification: { toaster } }); |
| 52 | + |
| 53 | + saga.put(action); |
| 54 | + |
| 55 | + expect(show).toBeCalled(); |
| 56 | + expect(dismiss).not.toBeCalled(); |
| 57 | + expect(clear).not.toBeCalled(); |
| 58 | + |
| 59 | + await saga.end(); |
| 60 | +}); |
| 61 | + |
| 62 | +test.each([ |
| 63 | + bleDidFailToConnect({ reason: BleDeviceFailToConnectReasonType.Canceled }), |
| 64 | + bootloaderDidFailToConnect(BootloaderConnectionFailureReason.Canceled), |
| 65 | +])('actions that should not show a notification: %o', async (action: Action) => { |
| 66 | + const getToasts = jest.fn().mockReturnValue([]); |
| 67 | + const show = jest.fn(); |
| 68 | + const dismiss = jest.fn(); |
| 69 | + const clear = jest.fn(); |
| 70 | + |
| 71 | + const toaster: IToaster = { |
| 72 | + getToasts, |
| 73 | + show, |
| 74 | + dismiss, |
| 75 | + clear, |
| 76 | + }; |
| 77 | + |
| 78 | + const saga = new AsyncSaga(notification, { notification: { toaster } }); |
| 79 | + |
| 80 | + saga.put(action); |
| 81 | + |
| 82 | + expect(getToasts).not.toBeCalled(); |
| 83 | + expect(show).not.toBeCalled(); |
| 84 | + expect(dismiss).not.toBeCalled(); |
| 85 | + expect(clear).not.toBeCalled(); |
| 86 | + |
| 87 | + await saga.end(); |
| 88 | +}); |
0 commit comments