add test for app sagas

This commit is contained in:
David Lechner
2021-01-19 17:20:56 -06:00
parent bab35e8c45
commit 647985906d
2 changed files with 37 additions and 2 deletions
+37
View File
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 The Pybricks Authors
import { AsyncSaga, delay } from '../../test';
import { reload } from '../actions/app';
import app from './app';
test('reload', async () => {
const saga = new AsyncSaga(app);
// mock registration as if service worker was register on app startup
const registration: Partial<ServiceWorkerRegistration> = {
unregister: jest.fn(),
};
// @ts-expect-error: navigator.serviceWorker is not implemented in JSDOM
navigator.serviceWorker = {
getRegistrations: jest.fn().mockResolvedValue([registration]),
};
// @ts-expect-error: JSDOM implementation of location.reload() causes error
delete window.location;
// @ts-expect-error: JSDOM implementation of location.reload() causes error
window.location = {
reload: jest.fn(),
};
saga.put(reload());
// yield to allow generators to complete
await delay(0);
expect(registration.unregister).toHaveBeenCalled();
expect(location.reload).toHaveBeenCalled();
await saga.end();
});
-2
View File
@@ -5,8 +5,6 @@ import { call, takeEvery } from 'redux-saga/effects';
import { AppActionType } from '../actions/app';
function* reload(): Generator {
console.log('reload');
// unregister the service worker so that when the page reloads, it uses
// the new version
const registrations = (yield call(() =>