convert error log from service to saga

This commit is contained in:
David Lechner
2020-06-10 21:59:34 -05:00
committed by David Lechner
parent 17cb5a32d5
commit 292f2d83d2
5 changed files with 86 additions and 37 deletions
+45
View File
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { AsyncSaga } from '../../test';
import { didFailToWrite } from '../actions/ble';
import {
BootloaderConnectionFailureReason,
didError,
didFailToConnect,
} from '../actions/lwp3-bootloader';
import errorLog from './error-log';
test('bleDataDidFailToWrite', async () => {
const saga = new AsyncSaga(errorLog);
console.error = jest.fn();
saga.put(didFailToWrite(0, new Error('test error')));
expect(console.error).toHaveBeenCalledTimes(1);
await saga.end();
});
test('bootloaderDidFailToConnect', async () => {
const saga = new AsyncSaga(errorLog);
console.debug = jest.fn();
saga.put(didFailToConnect(BootloaderConnectionFailureReason.Canceled));
expect(console.debug).toHaveBeenCalledTimes(1);
console.error = jest.fn();
saga.put(didFailToConnect(BootloaderConnectionFailureReason.Unknown));
expect(console.error).toHaveBeenCalledTimes(1);
await saga.end();
});
test('bootloaderDidError', async () => {
const saga = new AsyncSaga(errorLog);
console.error = jest.fn();
saga.put(didError(new Error('test error')));
expect(console.error).toHaveBeenCalledTimes(1);
await saga.end();
});
+38
View File
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { takeEvery } from 'redux-saga/effects';
import { BLEDataActionType, BLEDataDidFailToWriteAction } from '../actions/ble';
import {
BootloaderConnectionActionType,
BootloaderConnectionDidErrorAction,
BootloaderConnectionDidFailToConnectAction,
BootloaderConnectionFailureReason,
} from '../actions/lwp3-bootloader';
function bleDataDidFailToWrite(action: BLEDataDidFailToWriteAction): void {
console.error(action.err);
}
function bootloaderDidFailToConnect(
action: BootloaderConnectionDidFailToConnectAction,
): void {
if (action.reason === BootloaderConnectionFailureReason.Unknown) {
console.error(action.err);
} else {
console.debug(action.err);
}
}
function bootloaderDidError(action: BootloaderConnectionDidErrorAction): void {
console.error(action.err);
}
export default function* (): Generator {
yield takeEvery(BLEDataActionType.DidFailToWrite, bleDataDidFailToWrite);
yield takeEvery(
BootloaderConnectionActionType.DidFailToConnect,
bootloaderDidFailToConnect,
);
yield takeEvery(BootloaderConnectionActionType.DidError, bootloaderDidError);
}
+2 -1
View File
@@ -3,6 +3,7 @@
import { all } from 'redux-saga/effects';
import editor from './editor';
import errorLog from './error-log';
import flashFirmware from './flash-firmare';
import bootloader from './lwp3-bootloader';
import mpy from './mpy';
@@ -10,5 +11,5 @@ import terminal from './terminal';
/* istanbul ignore next */
export default function* (): Generator {
yield all([bootloader(), editor(), flashFirmware(), mpy(), terminal()]);
yield all([bootloader(), editor(), errorLog(), flashFirmware(), mpy(), terminal()]);
}