Skip to content

Instantly share code, notes, and snippets.

@charisschomba
Created February 20, 2019 10:05
Show Gist options
  • Save charisschomba/76b9e8d6bac44a05391574ae67b59b4a to your computer and use it in GitHub Desktop.
Save charisschomba/76b9e8d6bac44a05391574ae67b59b4a to your computer and use it in GitHub Desktop.
import React from 'react';
import { mount } from 'enzyme';
import { AddUser, mapStateToProps } from '../../../containers/Users/AddUser';
import fetchedGroups from '../../../__mocks__/fetchGroups';
const state = {
register: jest.fn(),
groups: jest.fn(),
};
const props = {
registerUser: jest.fn(),
allGroups: jest.fn(),
onChange: jest.fn(),
onSubmit: jest.fn(),
groups: {
groups: fetchedGroups,
},
register: {
isRegistering: false,
status: false,
success: false,
},
history: {
push: {
name: 'push',
},
},
};
const wrapper = mount(<AddUser {...props} />);
describe('<AddUser />', () => {
it('Maps state to props', () => {
expect(mapStateToProps(state)).toEqual(state);
});
it('renders form component as expected', () => {
expect(wrapper.find('Form').length).toBe(1);
});
it('test componentWillReceiveProps', () => {
const spy = jest.spyOn(AddUser.prototype, 'componentWillReceiveProps');
wrapper.state({
email: '',
status: false,
success: false,
selectedOption: null,
selectedGroups: null,
});
wrapper.instance().componentWillReceiveProps(props);
expect(spy.mock.calls.length).toEqual(1);
});
it('calls the onSubmit function', () => {
const onSubmitSpy = jest.spyOn(wrapper.instance(), 'onSubmit');
const e = { preventDefault: () => {} };
const data = {};
wrapper.instance().onSubmit(e, data);
expect(onSubmitSpy.mock.calls.length).toEqual(1);
});
it('calls the onChange function', () => {
const onChangeSpy = jest.spyOn(wrapper.instance(), 'onChange');
const e = { target: { name: '', value: '' } };
const data = {};
wrapper.instance().onChange(e, data);
expect(onChangeSpy.mock.calls.length).toEqual(1);
});
it('calls the handleChange function', () => {
const onChangeSpy = jest.spyOn(wrapper.instance(), 'handleChange');
const data = [{ value: '5c52bf9dfed75d1ec044c7fb', label: 'Cordinator' }];
wrapper.instance().handleChange(data);
expect(onChangeSpy.mock.calls.length).toEqual(1);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment