2021-04-12 13:32:25 +00:00
|
|
|
import React from "react";
|
|
|
|
|
import { mount } from "enzyme";
|
|
|
|
|
import { noop } from "lodash";
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import UserSettingsForm from "components/forms/UserSettingsForm";
|
|
|
|
|
import helpers from "test/helpers";
|
2016-12-13 15:24:58 +00:00
|
|
|
|
|
|
|
|
const { fillInFormInput, itBehavesLikeAFormInputElement } = helpers;
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("UserSettingsForm - component", () => {
|
2017-02-13 23:19:00 +00:00
|
|
|
const defaultProps = {
|
|
|
|
|
handleSubmit: noop,
|
|
|
|
|
onCancel: noop,
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("has the correct fields", () => {
|
2017-02-13 23:19:00 +00:00
|
|
|
const form = mount(<UserSettingsForm {...defaultProps} />);
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
itBehavesLikeAFormInputElement(form, "email");
|
|
|
|
|
itBehavesLikeAFormInputElement(form, "name");
|
|
|
|
|
itBehavesLikeAFormInputElement(form, "username");
|
2016-12-13 15:24:58 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("calls the handleSubmit props with form data", () => {
|
2020-12-01 18:15:12 +00:00
|
|
|
const handleSubmitSpy = jest.fn();
|
2017-02-13 23:19:00 +00:00
|
|
|
const props = { ...defaultProps, handleSubmit: handleSubmitSpy };
|
|
|
|
|
const form = mount(<UserSettingsForm {...props} />);
|
2021-04-12 13:32:25 +00:00
|
|
|
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");
|
2016-12-13 15:24:58 +00:00
|
|
|
|
|
|
|
|
fillInFormInput(emailInput, expectedFormData.email);
|
|
|
|
|
fillInFormInput(nameInput, expectedFormData.name);
|
|
|
|
|
fillInFormInput(usernameInput, expectedFormData.username);
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
form.find("form").simulate("submit");
|
2016-12-13 15:24:58 +00:00
|
|
|
|
|
|
|
|
expect(handleSubmitSpy).toHaveBeenCalledWith(expectedFormData);
|
|
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
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} />);
|
2016-12-13 15:24:58 +00:00
|
|
|
|
|
|
|
|
expect(form.state().formData).toEqual(user);
|
|
|
|
|
});
|
|
|
|
|
});
|