Skip to content
New issue

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

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

Already on GitHub? # to your account

(Do not merge!) Remove getReducer and replaceReducer from public API #624

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 1 addition & 31 deletions src/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,34 +109,6 @@ export default function createStore(reducer, initialState) {
return action;
}

/**
* Returns the reducer currently used by the store to calculate the state.
*
* It is likely that you will only need this function if you implement a hot
* reloading mechanism for Redux.
*
* @returns {Function} The reducer used by the current store.
*/
function getReducer() {
return currentReducer;
}

/**
* Replaces the reducer currently used by the store to calculate the state.
*
* You might need this if your app implements code splitting and you want to
* load some of the reducers dynamically. You might also need this if you
* implement a hot reloading mechanism for Redux.
*
* @param {Function} nextReducer The reducer for the store to use instead.
* @returns {void}
*/
function replaceReducer(nextReducer) {
currentReducer = nextReducer;
dispatch({ type: ActionTypes.INIT });
}


// When a store is created, an "INIT" action is dispatched so that every
// reducer returns their initial state. This effectively populates
// the initial state tree.
Expand All @@ -145,8 +117,6 @@ export default function createStore(reducer, initialState) {
return {
dispatch,
subscribe,
getState,
getReducer,
replaceReducer
getState
};
}
67 changes: 1 addition & 66 deletions test/createStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ describe('createStore', () => {
const store = createStore(combineReducers(reducers));
const methods = Object.keys(store);

expect(methods.length).toBe(5);
expect(methods.length).toBe(3);
expect(methods).toContain('subscribe');
expect(methods).toContain('dispatch');
expect(methods).toContain('getState');
expect(methods).toContain('getReducer');
expect(methods).toContain('replaceReducer');
});

it('should require a reducer function', () => {
Expand Down Expand Up @@ -94,69 +92,6 @@ describe('createStore', () => {
}]);
});

it('should preserve the state when replacing a reducer', () => {
const store = createStore(reducers.todos);
store.dispatch(addTodo('Hello'));
store.dispatch(addTodo('World'));
expect(store.getState()).toEqual([{
id: 1,
text: 'Hello'
}, {
id: 2,
text: 'World'
}]);

let nextStore = createStore(reducers.todosReverse);
store.replaceReducer(nextStore.getReducer());
expect(store.getState()).toEqual([{
id: 1,
text: 'Hello'
}, {
id: 2,
text: 'World'
}]);

store.dispatch(addTodo('Perhaps'));
expect(store.getState()).toEqual([{
id: 3,
text: 'Perhaps'
}, {
id: 1,
text: 'Hello'
}, {
id: 2,
text: 'World'
}]);

nextStore = createStore(reducers.todos);
store.replaceReducer(nextStore.getReducer());
expect(store.getState()).toEqual([{
id: 3,
text: 'Perhaps'
}, {
id: 1,
text: 'Hello'
}, {
id: 2,
text: 'World'
}]);

store.dispatch(addTodo('Surely'));
expect(store.getState()).toEqual([{
id: 3,
text: 'Perhaps'
}, {
id: 1,
text: 'Hello'
}, {
id: 2,
text: 'World'
}, {
id: 4,
text: 'Surely'
}]);
});

it('should support multiple subscriptions', () => {
const store = createStore(reducers.todos);
const listenerA = expect.createSpy(() => {});
Expand Down