From 534ee0827ced86b0d7ffb79acc4dee2171cbe888 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Thu, 14 Mar 2019 15:49:12 +0100 Subject: [PATCH 1/6] effects tests --- src/new.spec.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/new.spec.js b/src/new.spec.js index 71829d4..cd53242 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -234,4 +234,55 @@ describe("withStore", () => { }); }); }); + + describe("effects", () => { + it("receives the passed arguments", () => { + const args = ["bar", "baz"]; + const { effects } = makeTestInstance({ + effects: { + foo: (...rest) => { + expect(rest).toEqual(args); + }, + }, + }); + return effects.foo(...args); + }); + + it("are called with read-only effects and props and writable state in context", () => { + const state = { foo: "bar" }; + const { effects, getParentProps } = makeTestInstance({ + initialState: () => state, + effects: { + foo() { + assert(isReadOnly(this.effects)); + expect(this.effects).toBe(effects); + + assert(isReadOnly(this.props)); + expect(this.props).toBe(getParentProps()); + + expect(state.foo).toBe(this.state.foo); + this.state.foo = "baz"; + expect(this.state.foo).toBe("baz"); + }, + }, + }); + return effects.foo(); + }); + + it("can use other effects", () => { + const { effects } = makeTestInstance({ + initialState: () => ({ qux: "qux" }), + effects: { + async foo() { + await this.effects.bar(); + expect(this.state.qux).toBe("fred"); + }, + bar() { + this.state.qux = "fred"; + }, + }, + }); + return effects.foo(); + }); + }); }); From 85d362822930761ad733ebd6d5ab6ab1c34994dc Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Thu, 14 Mar 2019 16:17:28 +0100 Subject: [PATCH 2/6] fix --- src/new.spec.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/new.spec.js b/src/new.spec.js index cd53242..9c7d86b 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -248,12 +248,16 @@ describe("withStore", () => { return effects.foo(...args); }); - it("are called with read-only effects and props and writable state in context", () => { + it("are called with read-only effects and props and resetState and writable state in context", () => { const state = { foo: "bar" }; const { effects, getParentProps } = makeTestInstance({ initialState: () => state, effects: { foo() { + assert(isReadOnly(this)); + + expect(typeof this.resetState).toBe("function"); + assert(isReadOnly(this.effects)); expect(this.effects).toBe(effects); From 4066c62c774db4c1495605f4af4de0e7357576c7 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Fri, 15 Mar 2019 10:28:16 +0100 Subject: [PATCH 3/6] fix --- src/new.spec.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index 9c7d86b..6d3b36a 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -249,7 +249,7 @@ describe("withStore", () => { }); it("are called with read-only effects and props and resetState and writable state in context", () => { - const state = { foo: "bar" }; + const state = { qux: "qux" }; const { effects, getParentProps } = makeTestInstance({ initialState: () => state, effects: { @@ -259,14 +259,15 @@ describe("withStore", () => { expect(typeof this.resetState).toBe("function"); assert(isReadOnly(this.effects)); - expect(this.effects).toBe(effects); + expect(ownProps(this.effects)).toEqual(["foo", "_setState"]); assert(isReadOnly(this.props)); expect(this.props).toBe(getParentProps()); - expect(state.foo).toBe(this.state.foo); - this.state.foo = "baz"; - expect(this.state.foo).toBe("baz"); + expect(ownProps(this.state)).toEqual(["qux"]); + expect(state.qux).toBe(this.state.qux); + this.state.qux = "baz"; + expect(this.state.qux).toBe("baz"); }, }, }); From c8a58b51c87c87ae142c5e728ade3fa56bc4a04f Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 15 Mar 2019 11:06:39 +0100 Subject: [PATCH 4/6] Update new.spec.js --- src/new.spec.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index 6d3b36a..407cf1f 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -249,25 +249,24 @@ describe("withStore", () => { }); it("are called with read-only effects and props and resetState and writable state in context", () => { - const state = { qux: "qux" }; const { effects, getParentProps } = makeTestInstance({ - initialState: () => state, + initialState: () =>({ myEntry: "bar" }), effects: { - foo() { + myEffect() { assert(isReadOnly(this)); + assert(isReadOnly(this.effects)); + expect(ownProps(this.effects)).toEqual(["myEffect", "_setState"]); + expect(typeof this.resetState).toBe("function"); - assert(isReadOnly(this.effects)); - expect(ownProps(this.effects)).toEqual(["foo", "_setState"]); + expect(ownProps(this.state)).toEqual(["myEntry"]); + expect(this.state.myEntry).toBe("bar); + this.state.myEntry = "baz"; + expect(this.state.myEntry).toBe("baz"); assert(isReadOnly(this.props)); expect(this.props).toBe(getParentProps()); - - expect(ownProps(this.state)).toEqual(["qux"]); - expect(state.qux).toBe(this.state.qux); - this.state.qux = "baz"; - expect(this.state.qux).toBe("baz"); }, }, }); From b36dfb74aff335efa7d923b074981c6f71599012 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Fri, 15 Mar 2019 11:10:55 +0100 Subject: [PATCH 5/6] Update new.spec.js --- src/new.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/new.spec.js b/src/new.spec.js index 407cf1f..9e3bf1e 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -261,7 +261,7 @@ describe("withStore", () => { expect(typeof this.resetState).toBe("function"); expect(ownProps(this.state)).toEqual(["myEntry"]); - expect(this.state.myEntry).toBe("bar); + expect(this.state.myEntry).toBe("bar"); this.state.myEntry = "baz"; expect(this.state.myEntry).toBe("baz"); From 9b10ddaf9f4b04b058839238e71feb4470abdeae Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Fri, 15 Mar 2019 11:16:09 +0100 Subject: [PATCH 6/6] format code --- src/new.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index 9e3bf1e..4cd43fa 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -240,7 +240,7 @@ describe("withStore", () => { const args = ["bar", "baz"]; const { effects } = makeTestInstance({ effects: { - foo: (...rest) => { + foo(...rest) { expect(rest).toEqual(args); }, }, @@ -250,7 +250,7 @@ describe("withStore", () => { it("are called with read-only effects and props and resetState and writable state in context", () => { const { effects, getParentProps } = makeTestInstance({ - initialState: () =>({ myEntry: "bar" }), + initialState: () => ({ myEntry: "bar" }), effects: { myEffect() { assert(isReadOnly(this)); @@ -270,7 +270,7 @@ describe("withStore", () => { }, }, }); - return effects.foo(); + return effects.myEffect(); }); it("can use other effects", () => {