diff --git a/packages/xarc-react-redux/test/spec/index.spec.tsx b/packages/xarc-react-redux/test/spec/index.spec.tsx index c3204cf3f..347c3353b 100644 --- a/packages/xarc-react-redux/test/spec/index.spec.tsx +++ b/packages/xarc-react-redux/test/spec/index.spec.tsx @@ -10,13 +10,16 @@ import sinon from "sinon"; import { createStore, reduxFeature, ReduxFeature } from "../../src/browser/index"; const { createElement } = React; // eslint-disable-line + const mockPrepare = async initialState => { return { initialState: "init-state-" + initialState }; }; + const options = { React, prepare: mockPrepare }; + const MockComponent = () => { return (
@@ -255,6 +258,61 @@ describe("reactReduxFeature", function () { expect(res.props).to.eql({}); }); + it("should render subapp with decorator", async () => { + const container = new SubAppContainer({}); + envHooks.getContainer = () => container; + + const fake1 = sinon.fake(); + const fake2 = sinon.fake(); + + const factory = reduxFeature({ + ...options, + reducers: true, + decorators: [ + { + decor: fake1, + rootEpic: "test-epic1", + rootSaga: "test-saga1" + }, + { + decor: fake2, + rootEpic: "test-epic2", + rootSaga: "test-saga2" + } + ] + }); + + const def = { + name: "test", + getModule() { + return Promise.resolve({}); + }, + _features: {} + } as SubAppDef; + + container.declare("test", def); + + factory.add(def); + + def._module = { reduxReducers: x => x }; + (def._features.redux as any)._store = createStore(x => x); + + await def._features.redux.execute({ + input: { + Component: MockComponent + }, + csrData: { + name: "test", + getInitialState: () => "test" + }, + reload: false + }); + + expect(fake1.called).to.eql(true); + expect(fake2.called).to.eql(true); + expect(fake1.calledBefore(fake2)).to.eql(true); + }); + it("should render subapp with simple reducer on node side", async () => { const container = new SubAppContainer({}); envHooks.getContainer = () => container; diff --git a/packages/xarc-react-redux/test/spec/redux-shared-store.spec.ts b/packages/xarc-react-redux/test/spec/redux-shared-store.spec.ts index 409491c4f..f538d08b6 100644 --- a/packages/xarc-react-redux/test/spec/redux-shared-store.spec.ts +++ b/packages/xarc-react-redux/test/spec/redux-shared-store.spec.ts @@ -5,6 +5,7 @@ import sinon from "sinon"; import { initContainer, setStoreContainer, + clearSharedStore, getSharedStore, setSharedStore, replaceReducer, @@ -14,20 +15,26 @@ import { describe("shareStore", () => { beforeEach(() => { - setStoreContainer({ namedStores: {} }); + clearSharedStore(); }); - it("should initContainer with store container input", () => { - const storeContainerWithNamedStore = { namedStores: { a: "test" } }; + describe("should initContainer", () => { + beforeEach(() => { + clearSharedStore(); + }); - expect(initContainer(storeContainerWithNamedStore)).equal(storeContainerWithNamedStore); - const storeContainerWithoutNamedStore = { a: "test" }; + it("should initContainer with store container input", () => { + const storeContainerWithNamedStore = { namedStores: { a: "test" } }; - expect(initContainer(storeContainerWithoutNamedStore)).equal(storeContainerWithoutNamedStore); - }); + expect(initContainer(storeContainerWithNamedStore)).equal(storeContainerWithNamedStore); + const storeContainerWithoutNamedStore = { a: "test" }; - it("should initContainer without store container", () => { - expect(initContainer(undefined)).eql({ namedStores: {} }); + expect(initContainer(storeContainerWithoutNamedStore)).equal(storeContainerWithoutNamedStore); + }); + + it("should initContainer without store container", () => { + expect(initContainer(undefined)).eql({ namedStores: {} }); + }); }); it("should setStoreContainer", () => { @@ -38,6 +45,15 @@ describe("shareStore", () => { setStoreContainer({ namedStores: {} }); }); + it("should clearSharedStore", () => { + expect(setStoreContainer({ namedStores: { a: "test" } })).to.eql({ + namedStores: { a: "test" } + }); + clearSharedStore(); + + expect(getSharedStore("namedStores", null)).to.eql({}); + }); + it("should getSharedStore", () => { expect(getSharedStore(true, { namedStores: { _: "test_" } })).equal("test_"); @@ -61,6 +77,8 @@ describe("shareStore", () => { }); it("should replaceReducer", () => { + const spy = sinon.spy(); + const reducerNamesSym = "- reducer owner names -"; const originalReplaceReducerSym = "- original replace reducer -"; @@ -68,7 +86,6 @@ describe("shareStore", () => { name: "reducers", reduxShareStore: true }; - const spy = sinon.spy(); const storeContainer = { namedStores: { @@ -91,149 +108,226 @@ describe("shareStore", () => { sinon.restore(); }); - it("should createSharedStore with shared store", () => { - const reducerNamesSym = "- reducer owner names -"; - const originalReplaceReducerSym = "- original replace reducer -"; + describe("should createSharedStore", () => { + beforeEach(() => { + clearSharedStore(); + }); - const info = { - name: "reducers", - reduxShareStore: true, - reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" } - }; + it("should createSharedStore throw error when using reduxShareStore to share stores with reduxCreateStore", () => { + const info = { + reduxShareStore: true, + _genReduxCreateStore: undefined, + reduxCreateStore: x => x + }; + + try { + expect(createSharedStore({}, info, {})).throws( + "Error: When using reduxShareStore to share stores, you cannot have reduxCreateStore" + ); + } catch (e) { + // eslint-disable-next-line + console.log("This Error is expected \n" + e); + } + }); - const storeContainer = { - namedStores: { - _: { - store: { [reducerNamesSym]: x => x || "1", [originalReplaceReducerSym]: y => y || "2" }, - reducerContainer: { - [reducerNamesSym]: ["test1", "test2"], - test1: { a: "a", b: "b" }, - test2: { c: "c", d: "d" }, - reducers: {} + it("should createSharedStore with shared store", () => { + const reducerNamesSym = "- reducer owner names -"; + const originalReplaceReducerSym = "- original replace reducer -"; + + const info = { + name: "reducers", + reduxShareStore: true, + reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" } + }; + + const storeContainer = { + namedStores: { + _: { + store: { [reducerNamesSym]: x => x || "1", [originalReplaceReducerSym]: y => y || "2" }, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } } } - } - }; - - expect(createSharedStore({ a: "test" }, info, storeContainer)).to.equal( - storeContainer.namedStores._.store - ); - }); + }; - it("should createSharedStore without shared store", () => { - const reducerNamesSym = "- reducer owner names -"; - const originalReplaceReducerSym = "- original replace reducer -"; - - const info = { - name: "reducers", - reduxShareStore: true, - reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" } - }; + expect(createSharedStore({ a: "test" }, info, storeContainer)).to.equal( + storeContainer.namedStores._.store + ); + }); - const storeContainer = { - namedStores: { - _: { - store: undefined, - reducerContainer: { - [reducerNamesSym]: ["test1", "test2"], - test1: { a: "a", b: "b" }, - test2: { c: "c", d: "d" }, - reducers: {} + it("should createSharedStore without named store", () => { + const reducerNamesSym = "- reducer owner names -"; + const originalReplaceReducerSym = "- original replace reducer -"; + + const info = { + name: "reducers", + reduxShareStore: true, + reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" } + }; + + const storeContainer = { + namedStores: { + _: { + store: undefined, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } } } - } - }; + }; + + const store = createSharedStore({ a: "test" }, info, storeContainer); + expect(store[originalReplaceReducerSym]).to.be.a("function"); + + const spy = sinon.spy(); + + const info2 = { + name: "reducers", + reduxShareStore: true + }; + + const storeContainer2 = { + namedStores: { + _: { + store: { [reducerNamesSym]: x => x, [originalReplaceReducerSym]: spy }, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } + } + } + }; - expect( - createSharedStore({ a: "test" }, info, storeContainer)[originalReplaceReducerSym] - ).to.be.a("function"); - }); + const mockReducers = { t1: x => x, t2: y => y + 1 }; + store.replaceReducer(mockReducers, info2, storeContainer2); - it("should createSharedStore with reduxCreateStore", () => { - const reducerNamesSym = "- reducer owner names -"; - const spy = sinon.spy(); - const info = { - name: "reducers", - reduxShareStore: false, - reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" }, - reduxCreateStore: spy, - _genReduxCreateStore: false - }; + expect(spy.called).to.equal(true); + sinon.restore(); + }); - const mockInitState = { a: "test" }; - const storeContainer = { - namedStores: { - _: { - store: undefined, - reducerContainer: { - [reducerNamesSym]: ["test1", "test2"], - test1: { a: "a", b: "b" }, - test2: { c: "c", d: "d" }, - reducers: {} + it("should createSharedStore with reduxCreateStore", () => { + const reducerNamesSym = "- reducer owner names -"; + const spy = sinon.spy(); + const info = { + name: "reducers", + reduxShareStore: false, + reduxReducers: { t1: x => x || "1", t2: y => y + 1 || "2" }, + reduxCreateStore: spy, + _genReduxCreateStore: undefined + }; + + const mockInitState = { a: "test" }; + const storeContainer = { + namedStores: { + _: { + store: undefined, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } } } - } - }; + }; - createSharedStore(mockInitState, info, storeContainer); - expect(spy.calledOnceWith(mockInitState)).to.equal(true); - sinon.restore(); - }); - - it("should createSharedStore with function reducer type", () => { - const reducerNamesSym = "- reducer owner names -"; - const info = { - name: "reducers", - reduxShareStore: false, - reduxReducers: x => x || "1", - reduxCreateStore: false, - _genReduxCreateStore: true - }; + createSharedStore(mockInitState, info, storeContainer); + expect(spy.calledOnceWith(mockInitState)).to.equal(true); + sinon.restore(); + }); - const mockInitState = { a: "test" }; - const storeContainer = { - namedStores: { - _: { - store: undefined, - reducerContainer: { - [reducerNamesSym]: ["test1", "test2"], - test1: { a: "a", b: "b" }, - test2: { c: "c", d: "d" }, - reducers: {} + it("should createSharedStore with function reducer type", () => { + const reducerNamesSym = "- reducer owner names -"; + const info = { + name: "reducers", + reduxShareStore: false, + reduxReducers: x => x || "1", + reduxCreateStore: undefined, + _genReduxCreateStore: true + }; + + const mockInitState = { a: "test" }; + const storeContainer = { + namedStores: { + _: { + store: undefined, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } } } - } - }; + }; - expect(createSharedStore(mockInitState, info, storeContainer)).to.be.an("object"); - }); + expect(createSharedStore(mockInitState, info, storeContainer)).to.be.an("object"); + }); - it("should createSharedStore with object reducer type", () => { - const reducerNamesSym = "- reducer owner names -"; - const info = { - name: "reducers", - reduxShareStore: false, - reduxReducers: { a: x => x || "1", b: y => y || "2" }, - reduxCreateStore: false, - _genReduxCreateStore: true - }; + it("should createSharedStore with object reducer type", () => { + const reducerNamesSym = "- reducer owner names -"; + const info = { + name: "reducers", + reduxShareStore: false, + reduxReducers: { a: x => x || "1", b: y => y || "2" }, + reduxCreateStore: undefined, + _genReduxCreateStore: true + }; + + const mockInitState = { a: "test" }; + const storeContainer = { + namedStores: { + _: { + store: undefined, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } + } + } + }; - const mockInitState = { a: "test" }; - const storeContainer = { - namedStores: { - _: { - store: undefined, - reducerContainer: { - [reducerNamesSym]: ["test1", "test2"], - test1: { a: "a", b: "b" }, - test2: { c: "c", d: "d" }, - reducers: {} + expect(createSharedStore(mockInitState, info, storeContainer)).to.be.an("object"); + }); + + it("should createSharedStore with non-object, non-function reducer type", () => { + const reducerNamesSym = "- reducer owner names -"; + const info = { + name: "reducers", + reduxShareStore: false, + reduxReducers: undefined, + reduxCreateStore: undefined, + _genReduxCreateStore: true + }; + + const mockInitState = { a: "test" }; + const storeContainer = { + namedStores: { + _: { + store: undefined, + reducerContainer: { + [reducerNamesSym]: ["test1", "test2"], + test1: { a: "a", b: "b" }, + test2: { c: "c", d: "d" }, + reducers: {} + } } } - } - }; + }; - expect(createSharedStore(mockInitState, info, storeContainer)).to.be.an("object"); + expect(createSharedStore(mockInitState, info, storeContainer)).to.be.an("object"); + }); }); it("Should getReduxCreateStore", () => {