Skip to content

Commit e391f63

Browse files
committed
fix(ReduceStore): When setState invalid object, throw assertion error
1 parent 588c4a4 commit e391f63

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/ReduceStore.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// LICENSE : MIT
22
"use strict";
3+
const assert = require("assert");
34
import {Store} from "almin";
45
export default class ReduceStore extends Store {
56
constructor() {
@@ -18,6 +19,12 @@ export default class ReduceStore extends Store {
1819
* @param {ReduceState} newState
1920
*/
2021
setState(newState) {
22+
if (process.env.NODE_ENV === 'development') {
23+
assert(newState instanceof this.state.constructor, `newState should be instanceof exist this.state.constructor.
24+
newState: ${newState}
25+
this.state.constructor: ${this.state.constructor}
26+
`);
27+
}
2128
if (this.state.equals(newState)) {
2229
return;
2330
}

test/ReduceStore-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,22 @@ describe("ReduceStore", () => {
3232
useCase.execute();
3333
});
3434
});
35+
context("when wrong setState", () => {
36+
it("should throw assertionError", () => {
37+
class TestStore extends ReduceStore {
38+
constructor() {
39+
super();
40+
this.state = new AlwaysNewState();
41+
}
42+
}
43+
const store = new TestStore();
44+
try {
45+
const invalidStateShape = {};
46+
store.setState(invalidStateShape);
47+
throw new Error("SHOULD NOT THROWN ERROR");
48+
} catch (error) {
49+
assert(error.name === "AssertionError");
50+
}
51+
});
52+
});
3553
});

0 commit comments

Comments
 (0)