Project

General

Profile

« Previous | Next » 

Revision c28d51eb

Added by Boaz Shuster about 6 years ago

Fixes #23468 - Add a test helper to test reducers with fixtures

The idea of testing a reducer using fixtures was introduced
in the BreadcrumBar tests.

Since, other components are going to be tested using this method,
it is moved to testHelpers.js to avoid duplication of code.

Signed-off-by: Boaz Shuster <>

View differences:

webpack/assets/javascripts/react_app/common/testHelpers.js
export const testActionSnapshotWithFixtures = fixtures =>
Object.entries(fixtures).forEach(([description, runAction]) =>
it(description, () => testActionSnapshot(runAction)));
/**
* Test a reducer with fixtures and snapshots
* @param {Function} reducer reducer to test
* @param {Object} fixtures key=fixture description, value=props to apply
*/
export const testReducerSnapshotWithFixtures = (reducer, fixtures) => {
const reduce = ({ state, action = {} } = {}) => reducer(state, action);
Object.entries(fixtures).forEach(([description, action]) =>
it(description, () => expect(reduce(action)).toMatchSnapshot()));
};
webpack/assets/javascripts/react_app/components/BreadcrumbBar/__tests__/BreadcrumbBarReducer.test.js
} from '../BreadcrumbBarConstants';
import reducer from '../BreadcrumbBarReducer';
import { testReducerSnapshotWithFixtures } from '../../../common/testHelpers';
import { resource, resourceList } from '../BreadcrumbBar.fixtures';
const fixtures = {
......
},
};
describe('BreadcrumbBar reducer', () => {
const reduce = ({ state, action = {} } = {}) => reducer(state, action);
Object.entries(fixtures).forEach(([description, action]) =>
it(description, () => expect(reduce(action)).toMatchSnapshot()));
});
describe('BreadcrumbBar reducer', () => testReducerSnapshotWithFixtures(reducer, fixtures));
webpack/assets/javascripts/react_app/components/PasswordStrength/__tests__/PasswordStrengthReducer.test.js
} from '../PasswordStrengthConstants';
import reducer from '../PasswordStrengthReducer';
import { testReducerSnapshotWithFixtures } from '../../../common/testHelpers';
describe('PasswordStrength reducer', () => {
const reduce = ({ state, action = {} } = {}) => reducer(state, action);
const fixtures = {
'should return the initial state': {},
it('should return the initial state', () => expect(reduce()).toMatchSnapshot());
'should handle PASSWORD_STRENGTH_PASSWORD_CHANGED': {
action: {
type: PASSWORD_STRENGTH_PASSWORD_CHANGED,
payload: 'some-password',
},
},
it('should handle PASSWORD_STRENGTH_PASSWORD_CHANGED', () =>
expect(reduce({
action: {
type: PASSWORD_STRENGTH_PASSWORD_CHANGED,
payload: 'some-password',
},
})).toMatchSnapshot());
'should handle PASSWORD_STRENGTH_PASSWROD_CONFIRMATION_CHANGED': {
action: {
type: PASSWORD_STRENGTH_PASSWROD_CONFIRMATION_CHANGED,
payload: 'some-password',
},
},
};
it('should handle PASSWORD_STRENGTH_PASSWROD_CONFIRMATION_CHANGED', () =>
expect(reduce({
action: {
type: PASSWORD_STRENGTH_PASSWROD_CONFIRMATION_CHANGED,
payload: 'some-password',
},
})).toMatchSnapshot());
});
describe('PasswordStrength reducer', () => testReducerSnapshotWithFixtures(reducer, fixtures));

Also available in: Unified diff