fleet/frontend/components/forms/UserSettingsForm/UserSettingsForm.tests.jsx

53 lines
1.9 KiB
React
Raw Normal View History

import React from 'react';
import expect, { createSpy, restoreSpies } from 'expect';
import { mount } from 'enzyme';
import { noop } from 'lodash';
import UserSettingsForm from 'components/forms/UserSettingsForm';
import helpers from 'test/helpers';
const { fillInFormInput, itBehavesLikeAFormInputElement } = helpers;
describe('UserSettingsForm - component', () => {
afterEach(restoreSpies);
2017-02-13 23:19:00 +00:00
const defaultProps = {
handleSubmit: noop,
onCancel: noop,
};
it('has the correct fields', () => {
2017-02-13 23:19:00 +00:00
const form = mount(<UserSettingsForm {...defaultProps} />);
itBehavesLikeAFormInputElement(form, 'email');
itBehavesLikeAFormInputElement(form, 'name');
itBehavesLikeAFormInputElement(form, 'username');
});
it('calls the handleSubmit props with form data', () => {
const handleSubmitSpy = createSpy();
2017-02-13 23:19:00 +00:00
const props = { ...defaultProps, handleSubmit: handleSubmitSpy };
const form = mount(<UserSettingsForm {...props} />);
const expectedFormData = { email: 'email@example.com', name: 'Jim Example', username: 'jimmyexamples' };
const emailInput = form.find({ name: 'email' }).find('input');
const nameInput = form.find({ name: 'name' }).find('input');
const usernameInput = form.find({ name: 'username' }).find('input');
fillInFormInput(emailInput, expectedFormData.email);
fillInFormInput(nameInput, expectedFormData.name);
fillInFormInput(usernameInput, expectedFormData.username);
form.find('form').simulate('submit');
expect(handleSubmitSpy).toHaveBeenCalledWith(expectedFormData);
});
it('initializes the form with the users data', () => {
const user = { email: 'email@example.com', name: 'Jim Example', username: 'jimmyexamples' };
2017-02-13 23:19:00 +00:00
const props = { ...defaultProps, formData: user };
const form = mount(<UserSettingsForm {...props} />);
expect(form.state().formData).toEqual(user);
});
});