refactor common test helpers into test/

This way we can share them with multiple tests
This commit is contained in:
David Lechner
2020-06-10 21:59:34 -05:00
committed by David Lechner
parent 8ba4143fa7
commit 31c3e2b52e
16 changed files with 205 additions and 223 deletions
+36
View File
@@ -0,0 +1,36 @@
# Testing
NOTE: This directory contains test helpers. Actual tests are in the `src/`
directory.
## Running tests
yarn test
Launches the test runner in the interactive watch mode.
See the section about [running tests][tests] for more information.
[tests]: https://facebook.github.io/create-react-app/docs/running-tests
## Code coverage
yarn coverage
Launches the test runner in the code coverage mode. Results are displayed in
the terminal.
yarn coverage:html
xdg-open coverage/index.html
Does the same thing except results are converted to more detailed html pages in
the `coverage/` directory. (On macOS, use `open` and on Windows use `explorer`
instead of `xdg-open` to open the html in your default web browser.)
## Writing tests
Tests are written using the [Jest][jest] testing framework.
[jest]: https://jestjs.io/
+15
View File
@@ -0,0 +1,15 @@
const Environment = require('jest-environment-jsdom');
/**
* A custom environment to set the TextEncoder
* Thanks https://stackoverflow.com/a/57713960/1976323
*/
module.exports = class CustomTestEnvironment extends Environment {
async setup() {
await super.setup();
if (typeof TextEncoder === 'undefined') {
const { TextEncoder } = require('util');
this.global.TextEncoder = TextEncoder;
}
}
};
+97
View File
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { END, MulticastChannel, Saga, Task, runSaga, stdChannel } from 'redux-saga';
import { Action } from '../src/actions';
export class AsyncSaga {
private dispatches: (Action | END)[];
private takers: { put: (action: Action | END) => void }[];
private channel: MulticastChannel<Action>;
private task: Task;
public constructor(saga: Saga) {
this.dispatches = [];
this.takers = [];
this.channel = stdChannel();
this.task = runSaga(
{
channel: this.channel,
dispatch: this.dispatch.bind(this),
onError: (e) => fail(e),
},
saga,
);
}
public numPending(): number {
return this.dispatches.length;
}
public put(action: Action): void {
this.channel.put(action);
}
public take(): Promise<Action> {
const next = this.dispatches.shift();
if (next === undefined) {
// if there are no dispatches queued, then queue the taker to be
// completed later
return new Promise((resolve, reject) => {
this.takers.push({
put: (a: Action | END): void => {
if (a.type === END.type) {
reject();
} else {
resolve(a);
}
},
});
});
}
// otherwise complete immediately
if (next.type === END.type) {
return Promise.reject();
}
return Promise.resolve(next);
}
public async end(): Promise<void> {
this.task.cancel();
await this.task.toPromise();
if (this.dispatches.some((x) => x.type !== END.type)) {
fail(`unhandled dispatches remain: ${JSON.stringify(this.dispatches)}`);
}
}
private dispatch(action: Action | END): Action | END {
const taker = this.takers.shift();
if (taker === undefined) {
// if there are no takers waiting, the queue the action
this.dispatches.push(action);
} else {
// otherwise complete the promise
taker.put(action);
}
return action;
}
}
export function delay(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Looks up a nested property in an object.
* @param obj The object
* @param id The property path
*/
export function lookup(obj: object, id: string): string | undefined {
const value = id
.split('.')
.reduce((pv, cv) => pv && (pv as Record<string, object>)[cv], obj);
if (typeof value === 'string') {
return value;
}
return undefined;
}