Skip to content

Commit

Permalink
Refactor test setup steps
Browse files Browse the repository at this point in the history
  • Loading branch information
hotblac committed Jul 29, 2018
1 parent f7236ec commit 24a2665
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions src/UserRegistration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,27 @@ describe ('<UserRegistration/>', () => {
onSubmit.mockReset();
});


it('renders', () => {
const wrapper = shallow(<UserRegistration/>);
expect(wrapper).toBeDefined();
});


it('notifies caller of username and password', () => {
const wrapper = shallow(<UserRegistration onSubmit={onSubmit}/>);
const usernameField = wrapper.find('input.username');
const passwordField = wrapper.find('input.password');
const confirmField = wrapper.find('input.confirm');
const submitButton = wrapper.find('button');
setFieldValues(wrapper, username, password, password);

usernameField.simulate('change', stubEvent(usernameField, username));
passwordField.simulate('change', stubEvent(passwordField, password));
confirmField.simulate('change', stubEvent(confirmField, password));
const submitButton = wrapper.find('button');
submitButton.simulate('click');

expect(onSubmit).toBeCalledWith(username, password);
});


it('disables submit when password does not match confirmation', () => {
const wrapper = shallow(<UserRegistration onSubmit={onSubmit}/>);
const usernameField = wrapper.find('input.username');
const passwordField = wrapper.find('input.password');
const confirmField = wrapper.find('input.confirm');

usernameField.simulate('change', stubEvent(usernameField, username));
passwordField.simulate('change', stubEvent(passwordField, password));
confirmField.simulate('change', stubEvent(confirmField, 'mismatch'));
setFieldValues(wrapper, username, password, 'mismatch');

const submitButton = wrapper.find('button');
expect(submitButton.prop('disabled')).toBeTruthy();
Expand All @@ -50,15 +42,10 @@ describe ('<UserRegistration/>', () => {
expect(onSubmit).not.toBeCalled();
});


it('enables submit when password matches confirmation', () => {
const wrapper = shallow(<UserRegistration onSubmit={onSubmit}/>);
const usernameField = wrapper.find('input.username');
const passwordField = wrapper.find('input.password');
const confirmField = wrapper.find('input.confirm');

usernameField.simulate('change', stubEvent(usernameField, username));
passwordField.simulate('change', stubEvent(passwordField, password));
confirmField.simulate('change', stubEvent(confirmField, password));
setFieldValues(wrapper, username, password, password);

const submitButton = wrapper.find('button');
expect(submitButton.prop('disabled')).toBeFalsy();
Expand All @@ -67,15 +54,10 @@ describe ('<UserRegistration/>', () => {
expect(onSubmit).toBeCalled();
});


it('disables submit when password and confirmation are blank', () => {
const wrapper = shallow(<UserRegistration onSubmit={onSubmit}/>);
const usernameField = wrapper.find('input.username');
const passwordField = wrapper.find('input.password');
const confirmField = wrapper.find('input.confirm');

usernameField.simulate('change', stubEvent(usernameField, username));
passwordField.simulate('change', stubEvent(passwordField, ''));
confirmField.simulate('change', stubEvent(confirmField, ''));
setFieldValues(wrapper, username, '', '');

const submitButton = wrapper.find('button');
expect(submitButton.prop('disabled')).toBeTruthy();
Expand All @@ -84,6 +66,17 @@ describe ('<UserRegistration/>', () => {
submitButton.simulate('click');
expect(onSubmit).not.toBeCalled();
});


function setFieldValues(wrapper, username, password, confirm) {
const usernameField = wrapper.find('input.username');
const passwordField = wrapper.find('input.password');
const confirmField = wrapper.find('input.confirm');

usernameField.simulate('change', stubEvent(usernameField, username));
passwordField.simulate('change', stubEvent(passwordField, password));
confirmField.simulate('change', stubEvent(confirmField, confirm));
}
});

/**
Expand Down

0 comments on commit 24a2665

Please sign in to comment.