mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
add test for editor saga
This commit is contained in:
committed by
David Lechner
parent
31c3e2b52e
commit
5c3ec89e3b
@@ -73,6 +73,7 @@
|
||||
"eslint-plugin-import": "^2.20.2",
|
||||
"eslint-plugin-prettier": "^3.1.2",
|
||||
"eslint-plugin-react": "^7.20.0",
|
||||
"jest-mock-extended": "^1.0.8",
|
||||
"prettier": "^2.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2020 The Pybricks Authors
|
||||
|
||||
import { Ace } from 'ace-builds';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import { AsyncSaga } from '../../test';
|
||||
import { reloadProgram } from '../actions/editor';
|
||||
import editor from './editor';
|
||||
|
||||
test('reloadProgram', async () => {
|
||||
const saga = new AsyncSaga(editor);
|
||||
|
||||
const mockEditor = mock<Ace.EditSession>();
|
||||
|
||||
saga.setState({ editor: { current: mockEditor } });
|
||||
saga.put(reloadProgram());
|
||||
|
||||
expect(mockEditor.setValue).toHaveBeenCalled();
|
||||
|
||||
await saga.end();
|
||||
});
|
||||
+10
-2
@@ -3,21 +3,25 @@
|
||||
|
||||
import { END, MulticastChannel, Saga, Task, runSaga, stdChannel } from 'redux-saga';
|
||||
import { Action } from '../src/actions';
|
||||
import { RootState } from '../src/reducers';
|
||||
|
||||
export class AsyncSaga {
|
||||
private channel: MulticastChannel<Action>;
|
||||
private dispatches: (Action | END)[];
|
||||
private takers: { put: (action: Action | END) => void }[];
|
||||
private channel: MulticastChannel<Action>;
|
||||
private state: Partial<RootState>;
|
||||
private task: Task;
|
||||
|
||||
public constructor(saga: Saga) {
|
||||
this.channel = stdChannel();
|
||||
this.dispatches = [];
|
||||
this.takers = [];
|
||||
this.channel = stdChannel();
|
||||
this.state = {};
|
||||
this.task = runSaga(
|
||||
{
|
||||
channel: this.channel,
|
||||
dispatch: this.dispatch.bind(this),
|
||||
getState: () => this.state,
|
||||
onError: (e) => fail(e),
|
||||
},
|
||||
saga,
|
||||
@@ -56,6 +60,10 @@ export class AsyncSaga {
|
||||
return Promise.resolve(next);
|
||||
}
|
||||
|
||||
public setState(state: Partial<RootState>): void {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public async end(): Promise<void> {
|
||||
this.task.cancel();
|
||||
await this.task.toPromise();
|
||||
|
||||
@@ -6565,6 +6565,13 @@ jest-message-util@^24.9.0:
|
||||
slash "^2.0.0"
|
||||
stack-utils "^1.0.1"
|
||||
|
||||
jest-mock-extended@^1.0.8:
|
||||
version "1.0.8"
|
||||
resolved "https://registry.yarnpkg.com/jest-mock-extended/-/jest-mock-extended-1.0.8.tgz#af6cccf78c7ef0c9a0b5fbae4e2625a792f0d6b8"
|
||||
integrity sha512-lwRgbnngx6Woh0OOapes3uJQzngCj69OIhKnXYybEWwE0O1w7UyBJwLciHH6D/MCedogA90qxhEkEANrA20Sfw==
|
||||
dependencies:
|
||||
ts-essentials "^4.0.0"
|
||||
|
||||
jest-mock@^24.0.0, jest-mock@^24.9.0:
|
||||
version "24.9.0"
|
||||
resolved "https://registry.yarnpkg.com/jest-mock/-/jest-mock-24.9.0.tgz#c22835541ee379b908673ad51087a2185c13f1c6"
|
||||
@@ -11200,6 +11207,11 @@ trim-newlines@^1.0.0:
|
||||
dependencies:
|
||||
glob "^7.1.2"
|
||||
|
||||
ts-essentials@^4.0.0:
|
||||
version "4.0.0"
|
||||
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-4.0.0.tgz#506c42b270bbd0465574b90416533175b09205ab"
|
||||
integrity sha512-uQJX+SRY9mtbKU+g9kl5Fi7AEMofPCvHfJkQlaygpPmHPZrtgaBqbWFOYyiA47RhnSwwnXdepUJrgqUYxoUyhQ==
|
||||
|
||||
ts-pnp@1.1.6:
|
||||
version "1.1.6"
|
||||
resolved "https://registry.yarnpkg.com/ts-pnp/-/ts-pnp-1.1.6.tgz#389a24396d425a0d3162e96d2b4638900fdc289a"
|
||||
|
||||
Reference in New Issue
Block a user