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

Fix reducer pattern match #23

Merged
merged 1 commit into from
Jul 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ handlers.onComplete = function (state, action) {
*/
export default function createReducer(apiName) {
const safeName = escapeRegExp(apiName);
const reApiAction = new RegExp(`^${safeName}_(RESET|START|SUCCESS|FAIL)$`);
const reApiAction = new RegExp(`^${safeName}_(?:.+)_(RESET|START|SUCCESS|FAIL)$`);
return function reducer(state = {}, action) {
const match = reApiAction.exec(action.type);
if (match) {
Expand Down
44 changes: 29 additions & 15 deletions tests/reducers.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createReducer, { handlers } from '../src/reducers';
import { mockApiName } from './fixtures/mockApi';

const mockStartActionType = 'mockApi_START';
const mockSuccessActionType = 'mockApi_SUCCESS';
const mockFailActionType = 'mockApi_FAIL';
const mockResetActionType = 'mockApi_RESET';
const mockStartActionType = 'mockApi_getItem_START';
const mockSuccessActionType = 'mockApi_getItem_SUCCESS';
const mockFailActionType = 'mockApi_getItem_FAIL';
const mockResetActionType = 'mockApi_getItem_RESET';
const mockKey = 'getSomething__var:one';
const mockValue = { someData: 'bogus data string' };
const mockError = { code: 500, message: 'Something terrible happened' };
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('Reducers', () => {
});
describe('reducer', () => {
let reducer;
beforeAll(() => {
beforeEach(() => {
reducer = createReducer(mockApiName);
jest.spyOn(handlers, 'onStart');
jest.spyOn(handlers, 'onComplete');
Expand Down Expand Up @@ -132,29 +132,43 @@ describe('Reducers', () => {
it('handles special characters in api names', () => {
const inState = { bogus: 'bogus' };
reducer = createReducer('a*b+c');
reducer(inState, { type: 'a*b+c_START' });
reducer(inState, { type: 'a*b+c_getItem_START' });
expect(handlers.onStart).toHaveBeenCalled();

reducer = createReducer('api(v2)');
reducer(inState, { type: 'api(v2)_SUCCESS' });
reducer(inState, { type: 'api(v2)_getItem_SUCCESS' });
expect(handlers.onComplete).toHaveBeenCalled();
});

it('expects resource name', () => {
const inState = { bogus: 'bogus' };
reducer(inState, { type: 'mockApi__START' });
expect(handlers.onStart).not.toHaveBeenCalled();
});

it('handles special characters in resource names', () => {
const inState = { bogus: 'bogus' };
reducer(inState, { type: 'mockApi_getItems*_START' });
reducer(inState, { type: 'mockApi_get-items_START' });
reducer(inState, { type: 'mockApi_@get%items_START' });
expect(handlers.onStart).toHaveBeenCalledTimes(3);
});

it('does not handle for similarly named apis', () => {
const inState = { bogus: 'bogus' };
reducer(inState, { type: '2mockApi_START' });
reducer(inState, { type: 'mockApi_2_START' });
reducer(inState, { type: 'mockApi2_START' });
reducer(inState, { type: 'MOCKAPI_START' });
reducer(inState, { type: '2mockApi_getItem_START' });
reducer(inState, { type: 'mockApi-2_getItem_START' });
reducer(inState, { type: 'mockApi2_getItem_START' });
reducer(inState, { type: 'MOCKAPI_getItem_START' });
expect(handlers.onStart).not.toHaveBeenCalled();
});

it('does not handle for unknown actions', () => {
const inState = { bogus: 'bogus' };
reducer(inState, { type: 'mockApi_2START' });
reducer(inState, { type: 'mockApi_START2' });
reducer(inState, { type: 'mockApi__START' });
reducer(inState, { type: 'mockApi_start' });
reducer(inState, { type: 'mockApi_getItem_2START' });
reducer(inState, { type: 'mockApi_getItem_START2' });
reducer(inState, { type: 'mockApi_getItem_DOSTART' });
reducer(inState, { type: 'mockApi_getItem_start' });
expect(handlers.onStart).not.toHaveBeenCalled();
});
});
Expand Down