From e28bc752cc2f0bc8bd2ca9ada27be684b59d557a Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 09:26:06 +0100 Subject: [PATCH 1/6] computed testes --- src/factory.js | 5 ++++- src/new.spec.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/factory.js b/src/factory.js index 19a5f25..2e79d23 100644 --- a/src/factory.js +++ b/src/factory.js @@ -110,7 +110,10 @@ module.exports = ({ Component }) => { try { value = c(stateSpy.proxy, propsSpy.proxy); } catch (error) { - if (error instanceof CircularComputedError) { + if ( + error instanceof CircularComputedError || + error instanceof TypeError + ) { throw error; } diff --git a/src/new.spec.js b/src/new.spec.js index 7a2d118..51174db 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -90,4 +90,39 @@ describe("withStore", () => { expect(ownProps(props)).toEqual(["bar"]); }); }); + + describe("computed", () => { + it("can read state and computed entries", () => { + const { getState } = makeTestInstance({ + initialState: () => ({ + foo: "bar", + }), + computed: { + qux: ({ foo }) => foo, + corge: ({ qux }) => qux, + }, + }); + + expect(getState().qux).toBe("bar"); + expect(getState().corge).toBe("bar"); + }); + + it("cannot write state", () => { + const { getState } = makeTestInstance({ + initialState: () => ({ + foo: "bar", + }), + computed: { + qux: ({ foo }) => { + this.state.foo = "fred"; + return foo; + }, + }, + }); + + expect(() => getState().qux).toThrowError( + new TypeError("Cannot set property 'foo' of undefined") + ); + }); + }); }); From 8335eaa28735cbeafa731bb09daa1fbfb7d54d4c Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 09:31:34 +0100 Subject: [PATCH 2/6] fix --- src/factory.js | 5 +---- src/new.spec.js | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/factory.js b/src/factory.js index 2e79d23..19a5f25 100644 --- a/src/factory.js +++ b/src/factory.js @@ -110,10 +110,7 @@ module.exports = ({ Component }) => { try { value = c(stateSpy.proxy, propsSpy.proxy); } catch (error) { - if ( - error instanceof CircularComputedError || - error instanceof TypeError - ) { + if (error instanceof CircularComputedError) { throw error; } diff --git a/src/new.spec.js b/src/new.spec.js index 51174db..84adda9 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -120,9 +120,7 @@ describe("withStore", () => { }, }); - expect(() => getState().qux).toThrowError( - new TypeError("Cannot set property 'foo' of undefined") - ); + expect(getState().foo).toBe("bar"); }); }); }); From da6be0f79ef280fb3b390b04c0c2580de92a4c67 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 09:43:46 +0100 Subject: [PATCH 3/6] fix --- src/new.spec.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/new.spec.js b/src/new.spec.js index 84adda9..11bfd2e 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -44,6 +44,8 @@ const makeTestInstance = (opts, props) => { const ownProps = Object.getOwnPropertyNames; +const noop = () => {}; + const isReadOnly = object => !Object.isExtensible(object) && ownProps(object).every(name => { @@ -120,6 +122,7 @@ describe("withStore", () => { }, }); + noop(getState().qux); expect(getState().foo).toBe("bar"); }); }); From 895a1fbf134bbe278a6ce7deedc6447870d9d0f6 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 10:00:16 +0100 Subject: [PATCH 4/6] fix --- src/new.spec.js | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index 11bfd2e..5bd4ee4 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -94,7 +94,7 @@ describe("withStore", () => { }); describe("computed", () => { - it("can read state and computed entries", () => { + it("can read state and computed and cannot write state and computed", () => { const { getState } = makeTestInstance({ initialState: () => ({ foo: "bar", @@ -102,28 +102,16 @@ describe("withStore", () => { computed: { qux: ({ foo }) => foo, corge: ({ qux }) => qux, + thud: state => { + assert(isReadOnly(state)); + }, }, }); + noop(getState().thud); + expect(getState().qux).toBe("bar"); expect(getState().corge).toBe("bar"); }); - - it("cannot write state", () => { - const { getState } = makeTestInstance({ - initialState: () => ({ - foo: "bar", - }), - computed: { - qux: ({ foo }) => { - this.state.foo = "fred"; - return foo; - }, - }, - }); - - noop(getState().qux); - expect(getState().foo).toBe("bar"); - }); }); }); From 76034997317f047cb158fe391dfafa975cc18393 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Wed, 13 Mar 2019 10:59:47 +0100 Subject: [PATCH 5/6] Update new.spec.js --- src/new.spec.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index 5bd4ee4..878c5c7 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -44,8 +44,6 @@ const makeTestInstance = (opts, props) => { const ownProps = Object.getOwnPropertyNames; -const noop = () => {}; - const isReadOnly = object => !Object.isExtensible(object) && ownProps(object).every(name => { @@ -94,24 +92,24 @@ describe("withStore", () => { }); describe("computed", () => { - it("can read state and computed and cannot write state and computed", () => { + it("receive read-only state", () => { const { getState } = makeTestInstance({ initialState: () => ({ - foo: "bar", + foo: "foo", }), computed: { - qux: ({ foo }) => foo, - corge: ({ qux }) => qux, - thud: state => { + bar: () => "bar", + baz(state) { assert(isReadOnly(state)); + expect(state.foo).toBe("foo"); + expect(state.bar).toBe("bar"); + + return "baz", }, }, }); - noop(getState().thud); - - expect(getState().qux).toBe("bar"); - expect(getState().corge).toBe("bar"); + expect(getState().baz).toBe("baz"); }); }); }); From a669e6ee19bf2d6d52c7e3a6e572b6191ea4b1fa Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 11:14:04 +0100 Subject: [PATCH 6/6] format code --- 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 878c5c7..6f4d90e 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -104,7 +104,7 @@ describe("withStore", () => { expect(state.foo).toBe("foo"); expect(state.bar).toBe("bar"); - return "baz", + return "baz"; }, }, });