Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seed reducers after adding them #80

Merged
merged 3 commits into from May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,6 +26,10 @@ export function getModuleManager<State>(
let _modules: IModule<any>[] = [];
const _moduleIds = new Set();

const _seedReducers = () => {
_dispatch({ type: "@@Internal/ModuleManager/SeedReducers" });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments on the scenario on why this is needed.

};

const _dispatchActions = (actions: AnyAction[]) => {
if (!actions) {
return;
Expand Down Expand Up @@ -69,6 +73,9 @@ export function getModuleManager<State>(
_reducerManager.add(key, reducerMap[key]);
}
}

/* Fire an action so that the newly added reducers can seed their initial state */
_seedReducers();
};

const _removeReducers = (
Expand Down Expand Up @@ -106,6 +113,7 @@ export function getModuleManager<State>(
_moduleIds.add(module.id);
_modules.push(module);
_addReducers(module.reducerMap);

const middlewares = module.middlewares;
if (middlewares) {
_addMiddlewares(middlewares);
Expand Down
Expand Up @@ -35,6 +35,7 @@ it("module manager tests", () => {

// Test initial actions are dispatched for module1
expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
Expand All @@ -45,9 +46,11 @@ it("module manager tests", () => {

// Test initial actions are dispatched for module2
expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial2",
"initial21",
Expand All @@ -58,9 +61,11 @@ it("module manager tests", () => {

// Test final actions are dispatched for module1
expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial2",
"initial21",
Expand All @@ -74,9 +79,11 @@ it("module manager tests", () => {

// Test no additional actions are dispatched
expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial2",
"initial21",
Expand All @@ -90,9 +97,11 @@ it("module manager tests", () => {

// Test no additional actions are dispatched
expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial2",
"initial21",
Expand All @@ -105,6 +114,32 @@ it("module manager tests", () => {
]);
});

it("When adding a module, the initial state should be seeded immediately", () => {
const middlewareManager = getMiddlewareManager();
const moduleManager = getModuleManager(middlewareManager, []);

let actionsDispatched = [];

moduleManager.setDispatch(action => {
// Push the action type to array so we can track it
actionsDispatched.push(action.type);
return action.payload || null;
});

const reducer = jest.fn(() => state => state || null);

const module1 = {
id: "module1",
reducerMap: { duplicateReducer: reducer, key11: reducer },
};

middlewareManager.add = jest.fn(() => {
expect(reducer).toBeCalled();
});

moduleManager.add([module1]);
});

it("Dispose disposes all modules in reverse order they are added", () => {
const middlewareManager = getMiddlewareManager();
const moduleManager = getModuleManager(middlewareManager, []);
Expand Down Expand Up @@ -139,9 +174,11 @@ it("Dispose disposes all modules in reverse order they are added", () => {
moduleManager.dispose();

expect(actionsDispatched).toEqual([
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial1",
"initial11",
"@@Internal/ModuleManager/SeedReducers",
"@@Internal/ModuleManager/ModuleAdded",
"initial2",
"initial21",
Expand Down