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

Test with extra dispatched action in middleware #170

Open
Cicko opened this issue Sep 22, 2019 · 0 comments
Open

Test with extra dispatched action in middleware #170

Cicko opened this issue Sep 22, 2019 · 0 comments

Comments

@Cicko
Copy link

Cicko commented Sep 22, 2019

Hi coders!
I would like to test my middleware and in my case I use it to check if an input field value has correct pattern to display an error in case of not.

My ValidationMiddleware.js:

import { FORM_SET_VALUE, FORM_SET_ERROR } from '../../../lib/components/containers/form/store/constants';

const validationMiddleware = store => next => action => {
	if (action.type !== FORM_SET_VALUE) {
		return next(action);
	}
	const { pattern, value, attr } = action;
	const state = store.getState();
	const newError = new RegExp(pattern).exec(value) === null;
	const oldError = state.form.form[attr] ? state.form.form[attr].error : false;
	if (newError !== oldError) {
		store.dispatch({
			type: FORM_SET_ERROR,
			error: newError,
			attr,
		});
	}
	next(action);
};

export default validationMiddleware; 

And my ValidationMiddleware.text.js:

const configureMockStore = require('redux-mock-store').default;
import validationMiddleware from './validationMiddleware';
import { FORM_SET_ERROR } from '../../../lib/components/containers/form/store/constants';
import { setFormValue } from '../../../lib/components/containers/form/store/actions';

const mockStore = configureMockStore([validationMiddleware]);

describe('Validation middleware', () => {
	it('should dispatch error when wrong value for specific pattern', () => {

		const store = mockStore({
			form: {
				form: {
					field: {
						error: false,
					},
				},
			},
		});
		store.dispatch(setFormValue('field', '123', '^[a-zA-Z\-\'. ]*$'));
		const expectedPayload = {
			type: FORM_SET_ERROR,
			attr: 'field',
			error: true,
		};
		const actions = store.getActions();
		expect(actions).toEqual([expectedPayload]);
	});
});

The point is that it dispatches another action in case of error so there are two different actions dispatched instead of one (FORM_SET_ERROR and FORM_SET_VALUE).

So I don't know how to check if the FORM_SET_ERROR was dispatched.
Any ideas?

Thank you in advance!

@Cicko Cicko changed the title Test with dispatch in middleware Test with extra dispatched action in middleware Sep 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant