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

Add reduxDevtoolsConfig token to customize setup of Redux devtools extension #750

Closed
wants to merge 3 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 65 additions & 1 deletion fusion-plugin-react-redux/__tests__/index.browser.js
Expand Up @@ -21,12 +21,13 @@ import {
ReducerToken,
PreloadedStateToken,
EnhancerToken,
ReduxDevtoolsConfigToken,
} from '../src/tokens.js';

Enzyme.configure({adapter: new Adapter()});

/* Test fixtures */
const appCreator = (reducer, preloadedState, enhancer) => {
const appCreator = (reducer, preloadedState, enhancer, reduxDevToolsConfig) => {
const app = new App('test', el => el);
if (reducer) {
app.register(ReducerToken, reducer);
Expand All @@ -37,6 +38,9 @@ const appCreator = (reducer, preloadedState, enhancer) => {
if (enhancer) {
app.register(EnhancerToken, enhancer);
}
if (reduxDevToolsConfig !== undefined && reduxDevToolsConfig !== null) {
app.register(ReduxDevtoolsConfigToken, reduxDevToolsConfig);
}
return () => app;
};

Expand Down Expand Up @@ -173,6 +177,66 @@ test('browser with devtools enhancer', () => {
delete window.__REDUX_DEVTOOLS_EXTENSION__;
});

test('browser with devtools enhancer with custom devToolsConfig', () => {
const Redux = GetReduxPlugin();
const reducer = (state, action) => {
return {
...state,
test: action.payload || 1,
};
};
let enhancerCalls = 0;
let devToolsInitArg = null;
window.__REDUX_DEVTOOLS_EXTENSION__ = initArg => createStore => {
devToolsInitArg = initArg;
enhancerCalls++;
expect(typeof createStore).toBe('function');
return (...args) => {
expect(args[0]).toBe(reducer);
return createStore(...args);
};
};
// https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md#actionsanitizer--statesanitizer
const customReduxDevtoolsConfig = {
actionSanitizer: action => action,
};
const {store} = getService(
appCreator(reducer, null, null, customReduxDevtoolsConfig),
Redux
).from();
expect(store.getState()).toStrictEqual({test: 1});
store.dispatch({type: 'CHANGE', payload: 2});
expect(store.getState().test).toBe(2);
expect(devToolsInitArg).toEqual(
expect.objectContaining(customReduxDevtoolsConfig)
);
expect(enhancerCalls).toBe(1);
delete window.__REDUX_DEVTOOLS_EXTENSION__;
});

test('browser with devtools enhancer, and devToolsConfig set to false', () => {
const Redux = GetReduxPlugin();
const reducer = (state, action) => {
return {
...state,
test: action.payload || 1,
};
};
let enhancerCalls = 0;
window.__REDUX_DEVTOOLS_EXTENSION__ = () => createStore => {
enhancerCalls++;
};
const {store} = getService(
appCreator(reducer, null, null, false),
Redux
).from();
expect(store.getState()).toStrictEqual({test: 1});
store.dispatch({type: 'CHANGE', payload: 2});
expect(store.getState().test).toBe(2);
expect(enhancerCalls).toBe(0);
delete window.__REDUX_DEVTOOLS_EXTENSION__;
});

test('browser with devtools enhancer and normal enhancer', () => {
const Redux = GetReduxPlugin();
const reducer = (state, action) => {
Expand Down
19 changes: 16 additions & 3 deletions fusion-plugin-react-redux/src/browser.js
Expand Up @@ -18,7 +18,12 @@ import type {Context, FusionPlugin} from 'fusion-core';

import ctxEnhancer from './ctx-enhancer';
import {deserialize} from './codec.js';
import {ReducerToken, PreloadedStateToken, EnhancerToken} from './tokens.js';
import {
ReducerToken,
PreloadedStateToken,
EnhancerToken,
ReduxDevtoolsConfigToken,
} from './tokens.js';
import type {
StoreWithContextType,
ReactReduxDepsType,
Expand All @@ -32,8 +37,9 @@ const getPlugin = () => {
reducer: ReducerToken,
preloadedState: PreloadedStateToken.optional,
enhancer: EnhancerToken.optional,
reduxDevToolsConfig: ReduxDevtoolsConfigToken.optional,
},
provides({reducer, preloadedState, enhancer}) {
provides({reducer, preloadedState, enhancer, reduxDevToolsConfig}) {
class Redux {
store: StoreWithContextType<*, *, *>;

Expand All @@ -53,10 +59,17 @@ const getPlugin = () => {
}
}
const devTool =
reduxDevToolsConfig !== false &&
__DEV__ &&
window.__REDUX_DEVTOOLS_EXTENSION__ &&
// $FlowFixMe
__REDUX_DEVTOOLS_EXTENSION__({trace: true, traceLimit: 25});
__REDUX_DEVTOOLS_EXTENSION__({
trace: true,
traceLimit: 25,
...((typeof reduxDevToolsConfig === 'object' &&
reduxDevToolsConfig) ||
{}),
});
const enhancers = [enhancer, ctxEnhancer(ctx), devTool].filter(
Boolean
);
Expand Down
3 changes: 3 additions & 0 deletions fusion-plugin-react-redux/src/tokens.js
Expand Up @@ -23,6 +23,9 @@ export const PreloadedStateToken: Token<Object> = createToken(
export const EnhancerToken: Token<StoreEnhancer<*, *, *>> = createToken(
'EnhancerToken'
);
export const ReduxDevtoolsConfigToken: Token<{} | false> = createToken(
'ReduxDevtoolsConfigToken'
);
export const GetInitialStateToken: Token<GetInitialStateType<*>> = createToken(
'GetInitialStateToken'
);