From 5115ecd130292992e5aaf0d2298771c91ac39817 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 11:24:05 +0100 Subject: [PATCH 1/5] computed tests --- src/new.spec.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/new.spec.js b/src/new.spec.js index 6f4d90e..f9b07f7 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -4,6 +4,7 @@ const assert = require("assert"); const { createElement } = require("react"); const { configure, mount } = require("enzyme"); +const CircularComputedError = require("./_CircularComputedError"); const { withStore } = require("./"); configure({ adapter: new (require("enzyme-adapter-react-16"))() }); @@ -111,5 +112,36 @@ describe("withStore", () => { expect(getState().baz).toBe("baz"); }); + + it("cannot access itself", () => { + const { getState } = makeTestInstance({ + initialState: () => ({}), + computed: { + circularComputed: ({ circularComputed }) => {}, + }, + }); + + expect(() => { + return getState().circularComputed; + }).toThrowError(new CircularComputedError("circularComputed")); + }); + + it("receive read-only props", () => { + const props = { bar: 2 }; + const { getState } = makeTestInstance( + { + initialState: () => ({}), + computed: { + baz: (_, props) => { + assert(isReadOnly(props)); + return props.bar * 2; + }, + }, + }, + props + ); + + expect(getState().baz).toBe(4); + }); }); }); From 6184b5aba6c071976e6f8652203f62bcfb8a4865 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 13:53:33 +0100 Subject: [PATCH 2/5] fix --- src/new.spec.js | 51 +++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index f9b07f7..c759ab5 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -93,22 +93,29 @@ describe("withStore", () => { }); describe("computed", () => { - it("receive read-only state", () => { - const { getState } = makeTestInstance({ - initialState: () => ({ - foo: "foo", - }), - computed: { - bar: () => "bar", - baz(state) { - assert(isReadOnly(state)); - expect(state.foo).toBe("foo"); - expect(state.bar).toBe("bar"); + it("receive read-only state and props", () => { + const props = { qux: 2 }; + const { getState } = makeTestInstance( + { + initialState: () => ({ + foo: "foo", + }), + computed: { + bar: () => "bar", + baz(state, props) { + assert(isReadOnly(state)); + assert(isReadOnly(props)); - return "baz"; + expect(state.foo).toBe("foo"); + expect(state.bar).toBe("bar"); + expect(props.qux).toBe(2); + + return "baz"; + }, }, }, - }); + props + ); expect(getState().baz).toBe("baz"); }); @@ -125,23 +132,5 @@ describe("withStore", () => { return getState().circularComputed; }).toThrowError(new CircularComputedError("circularComputed")); }); - - it("receive read-only props", () => { - const props = { bar: 2 }; - const { getState } = makeTestInstance( - { - initialState: () => ({}), - computed: { - baz: (_, props) => { - assert(isReadOnly(props)); - return props.bar * 2; - }, - }, - }, - props - ); - - expect(getState().baz).toBe(4); - }); }); }); From 0dde66902139d11d22b2fd9be29aec916afbe78e Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 14:00:32 +0100 Subject: [PATCH 3/5] fix --- src/new.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index c759ab5..9010914 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -94,7 +94,7 @@ describe("withStore", () => { describe("computed", () => { it("receive read-only state and props", () => { - const props = { qux: 2 }; + const props = { qux: "qux" }; const { getState } = makeTestInstance( { initialState: () => ({ @@ -108,7 +108,7 @@ describe("withStore", () => { expect(state.foo).toBe("foo"); expect(state.bar).toBe("bar"); - expect(props.qux).toBe(2); + expect(props.qux).toBe("qux"); return "baz"; }, From 5d529c98ab206a1a82ab62ca3874829049f74168 Mon Sep 17 00:00:00 2001 From: Mohamedox Date: Wed, 13 Mar 2019 14:06:07 +0100 Subject: [PATCH 4/5] fix --- 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 9010914..e9cdf60 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -130,7 +130,7 @@ describe("withStore", () => { expect(() => { return getState().circularComputed; - }).toThrowError(new CircularComputedError("circularComputed")); + }).toThrow(CircularComputedError); }); }); }); From bb829f018da38d67a137e8865d75f53e03826365 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Wed, 13 Mar 2019 14:23:03 +0100 Subject: [PATCH 5/5] Update new.spec.js --- src/new.spec.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/new.spec.js b/src/new.spec.js index e9cdf60..d77cd03 100644 --- a/src/new.spec.js +++ b/src/new.spec.js @@ -122,15 +122,12 @@ describe("withStore", () => { it("cannot access itself", () => { const { getState } = makeTestInstance({ - initialState: () => ({}), computed: { - circularComputed: ({ circularComputed }) => {}, + circular: ({ circular }) => {}, }, }); - expect(() => { - return getState().circularComputed; - }).toThrow(CircularComputedError); + expect(() => getState().circular).toThrow(CircularComputedError); }); }); });