diff --git a/src/sagas/app.test.ts b/src/sagas/app.test.ts new file mode 100644 index 00000000..44fa538e --- /dev/null +++ b/src/sagas/app.test.ts @@ -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 = { + 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(); +}); diff --git a/src/sagas/app.ts b/src/sagas/app.ts index ceae5066..1a4b2041 100644 --- a/src/sagas/app.ts +++ b/src/sagas/app.ts @@ -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(() =>