Skip to content

Commit

Permalink
ref: Use context instead of extra for redux state.
Browse files Browse the repository at this point in the history
  • Loading branch information
jennmueng committed Jul 14, 2020
1 parent f1f8d1a commit 5220d6b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 6 additions & 5 deletions packages/react/src/redux.ts
Expand Up @@ -25,9 +25,9 @@ export interface SentryEnhancerOptions {
*/
actionBreadcrumbType: string;
/**
* The extra key to pass the state to. Default is 'redux.state'
* The context key to pass the state to. Default is 'redux.state'
*/
stateExtraKey: string;
stateContextKey: string;
/**
* Called on every state update, configure the Sentry Scope with the redux state.
*/
Expand All @@ -38,7 +38,7 @@ const defaultOptions: SentryEnhancerOptions = {
actionBreadcrumbCategory: 'redux.action',
actionBreadcrumbType: 'info',
actionTransformer: action => action,
stateExtraKey: 'redux.state',
stateContextKey: 'redux.state',
// tslint:disable-next-line: no-unsafe-any
stateTransformer: state => state,
};
Expand Down Expand Up @@ -71,9 +71,10 @@ function createReduxEnhancer(enhancerOptions?: Partial<SentryEnhancerOptions>):
/* Set latest state to scope */
const transformedState = options.stateTransformer ? options.stateTransformer(newState) : newState;
if (typeof transformedState !== 'undefined' && transformedState !== null) {
scope.setExtra(options.stateExtraKey, transformedState);
// tslint:disable-next-line: no-unsafe-any
scope.setContext(options.stateContextKey, transformedState);
} else {
scope.setExtra(options.stateExtraKey, undefined);
scope.setContext(options.stateContextKey, null);
}

/* Allow user to configure scope with latest state */
Expand Down
12 changes: 6 additions & 6 deletions packages/react/test/redux.test.ts
Expand Up @@ -6,19 +6,19 @@ import * as Redux from 'redux';
import { createReduxEnhancer } from '../src/redux';

const mockAddBreadcrumb = jest.fn();
const mockSetExtra = jest.fn();
const mockSetContext = jest.fn();

jest.mock('@sentry/minimal', () => ({
configureScope: (callback: (scope: any) => Partial<Scope>) =>
callback({
addBreadcrumb: mockAddBreadcrumb,
setExtra: mockSetExtra,
setContext: mockSetContext,
}),
}));

afterEach(() => {
mockAddBreadcrumb.mockReset();
mockSetExtra.mockReset();
mockSetContext.mockReset();
});

describe('createReduxEnhancer', () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('createReduxEnhancer', () => {
const updateAction = { type: ACTION_TYPE, newValue: 'updated' };
store.dispatch(updateAction);

expect(mockSetExtra).toBeCalledWith('redux.state', {
expect(mockSetContext).toBeCalledWith('redux.state', {
value: 'updated',
});
});
Expand All @@ -81,7 +81,7 @@ describe('createReduxEnhancer', () => {

Redux.createStore((state = initialState) => state, enhancer);

expect(mockSetExtra).toBeCalledWith('redux.state', {
expect(mockSetContext).toBeCalledWith('redux.state', {
superSecret: 'REDACTED',
value: 123,
});
Expand All @@ -100,7 +100,7 @@ describe('createReduxEnhancer', () => {
Redux.createStore((state = initialState) => state, enhancer);

// Check that state is cleared
expect(mockSetExtra).toBeCalledWith('redux.state', undefined);
expect(mockSetContext).toBeCalledWith('redux.state', null);
});

it('transforms actions', () => {
Expand Down

0 comments on commit 5220d6b

Please sign in to comment.