Farmbot-Web-App/frontend/account/components/__tests__/test_change_password.tsx

159 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import * as React from "react";
2019-04-26 10:32:03 -06:00
import { ChangePassword } from "../change_password";
2017-06-29 12:54:02 -06:00
import { mount } from "enzyme";
import { SpecialStatus } from "farmbot";
2017-08-17 12:36:33 -06:00
import * as moxios from "moxios";
2019-04-26 10:32:03 -06:00
import { API } from "../../../api/api";
2019-06-24 15:39:49 -06:00
import { error } from "../../../toast/toast";
2018-04-05 16:41:33 -06:00
2017-06-29 12:54:02 -06:00
describe("<ChangePassword/>", function () {
2017-08-17 12:36:33 -06:00
function testCase() {
2018-07-03 12:21:05 -06:00
const el = mount(<ChangePassword />);
2017-08-17 12:36:33 -06:00
return {
el,
instance(): ChangePassword { return el.instance() as ChangePassword; }
};
2017-08-17 12:36:33 -06:00
}
2018-04-05 16:41:33 -06:00
it("doesn't fire maybeClearForm() if form is filled", () => {
2017-08-28 05:49:13 -06:00
const { el, instance } = testCase();
2017-08-17 12:36:33 -06:00
el.setState({
status: SpecialStatus.DIRTY,
form: { ...instance().state.form, password: "X" }
});
el.update();
2018-07-02 13:41:58 -06:00
expect(instance().state.form.password).toEqual("X");
2017-08-17 12:36:33 -06:00
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
instance().maybeClearForm();
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
2018-07-02 13:41:58 -06:00
expect(instance().state.form.password).toEqual("X");
2017-08-17 12:36:33 -06:00
});
it("it does fire maybeClearForm() when form is empty.", () => {
2017-08-28 05:49:13 -06:00
const { el, instance } = testCase();
2017-08-17 12:36:33 -06:00
el.setState({
status: SpecialStatus.DIRTY,
form: {
new_password: "",
new_password_confirmation: "",
password: ""
}
});
instance().maybeClearForm();
el.update();
expect(instance().state.status).toBe(SpecialStatus.SAVED);
2017-08-17 12:36:33 -06:00
});
it("sets a field", () => {
2017-08-28 05:49:13 -06:00
const { el, instance } = testCase();
2017-08-28 06:23:53 -06:00
// tslint:disable-next-line:no-any
2017-08-17 12:36:33 -06:00
instance().set("password")({ currentTarget: { value: "foo" } } as any);
el.update();
expect(instance().state.form.password).toBe("foo");
});
2017-08-28 06:23:53 -06:00
2018-04-05 16:41:33 -06:00
it("rejects new == old password case", () => {
const { instance } = testCase();
instance().state.form = {
password: "a",
new_password: "a",
new_password_confirmation: "a"
};
instance().save();
2018-05-12 09:09:40 -06:00
const expectation = expect.stringContaining("Password not changed");
expect(error).toHaveBeenCalledWith(expectation, "Error");
2018-04-05 16:41:33 -06:00
expect(instance().state.status).toBe(SpecialStatus.SAVED);
});
it("rejects new != password confirmation case", () => {
const { instance } = testCase();
instance().state.form = {
password: "a",
new_password: "b",
new_password_confirmation: "c"
};
instance().save();
2018-05-12 09:09:40 -06:00
const expectation = expect.stringContaining("do not match");
expect(error).toHaveBeenCalledWith(expectation, "Error");
2018-04-05 16:41:33 -06:00
expect(instance().state.status).toBe(SpecialStatus.SAVED);
});
it("throws a form error", () => {
const { instance } = testCase();
// tslint:disable-next-line:no-any
instance().state.form = {} as any;
expect(instance().save).toThrowError("form error");
expect(instance().state.status).toBe(SpecialStatus.SAVED);
});
it("cancels password change", () => {
const { instance } = testCase();
instance().state.form = {
password: "a",
new_password: "b",
new_password_confirmation: "b"
};
window.confirm = () => false;
instance().save();
expect(instance().state.status).toBe(SpecialStatus.SAVED);
});
2017-08-17 12:36:33 -06:00
describe("AJAX", () => {
beforeEach(function () {
// import and pass your custom axios instance to this method
moxios.install();
2018-04-05 16:41:33 -06:00
API.setBaseUrl("localhost");
window.confirm = () => true;
2017-08-17 12:36:33 -06:00
});
afterEach(function () {
moxios.uninstall();
});
it("saves (KO)", (done) => {
2017-08-28 05:49:13 -06:00
const { instance } = testCase();
2018-04-05 16:41:33 -06:00
instance().state.form = {
password: "x",
new_password: "b",
new_password_confirmation: "b"
};
2017-08-17 12:36:33 -06:00
instance().save();
moxios.wait(function () {
2017-08-28 05:49:13 -06:00
const request = moxios.requests.mostRecent();
2017-08-17 12:36:33 -06:00
request.respondWith({
status: 422,
response: { bad: "data" }
}).then(function (resp) {
expect(resp.config.url).toContain("api/users");
expect(resp.config.method).toEqual("patch");
2018-04-05 16:41:33 -06:00
expect(instance().state.status).toBe(SpecialStatus.SAVED);
2017-08-17 12:36:33 -06:00
done();
});
});
});
it("saves (OK)", (done) => {
2017-08-28 05:49:13 -06:00
const { instance } = testCase();
2018-04-05 16:41:33 -06:00
instance().state.form = {
password: "a",
new_password: "b",
new_password_confirmation: "b"
};
2017-08-17 12:36:33 -06:00
instance().save();
moxios.wait(function () {
2017-08-28 05:49:13 -06:00
const request = moxios.requests.mostRecent();
2017-08-17 12:36:33 -06:00
request.respondWith({
status: 200,
response: {}
}).then(function (resp) {
expect(resp.config.url).toContain("api/users");
expect(resp.config.method).toEqual("patch");
2018-04-05 16:41:33 -06:00
expect(instance().state.status).toBe(SpecialStatus.SAVED);
2017-08-17 12:36:33 -06:00
done();
});
});
});
2018-04-05 16:41:33 -06:00
});
2017-06-29 12:54:02 -06:00
});