Files
pybricks-code/src/__mocks__/serviceWorkerRegistration.ts
T
David Lechner 1469d2146e app: rework service worker registration
- registration is moved from top-level index to app/sagas.
- mock implementation is provided for tests
- actions are renamed to include "app" prefix
- actions are changed to not use non-serializable arguments
- reducers are changed to not use non-serializable state
2022-03-09 15:48:30 -06:00

53 lines
1.3 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
// mock implementation of serviceWorkerRegistration for testing
type Config = {
onSuccess?: (r: ServiceWorkerRegistration) => void;
onUpdate?: (r: ServiceWorkerRegistration) => void;
};
/** The config that was registered, if any. */
let globalConfig: Config | undefined;
/**
* Mocks the register() function.
*
* This just saves the config for later use.
*
* @param config The config.
*/
export function register(config?: Config): void {
globalConfig = config;
}
/**
* Fires the onSuccess callback that was registered, if any.
*
* @param registration The registration to pass to the callback.
*/
export function _fireOnSuccess(registration: ServiceWorkerRegistration): void {
if (globalConfig && globalConfig.onSuccess) {
globalConfig.onSuccess(registration);
}
}
/**
* Fires the onUpdate callback that was registered, if any.
*
* @param registration The registration to pass to the callback.
*/
export function _fireOnUpdate(registration: ServiceWorkerRegistration): void {
if (globalConfig && globalConfig.onUpdate) {
globalConfig.onUpdate(registration);
}
}
/**
* Mocks the unregister() function.
*/
export function unregister(): void {
// nothing to do for now
}