submit() tests
This commit is contained in:
parent
5d631ce748
commit
bc4469b5bd
|
@ -10,13 +10,41 @@ jest.mock("../../i18n", () => {
|
|||
};
|
||||
});
|
||||
|
||||
jest.mock("axios", () => {
|
||||
return {
|
||||
default: {
|
||||
post: jest.fn(() => {
|
||||
return Promise.resolve({
|
||||
data: { token: { unencoded: {}, encoded: "========" } }
|
||||
});
|
||||
})
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
import * as React from "react";
|
||||
import { TosUpdate } from "../index";
|
||||
import { shallow } from "enzyme";
|
||||
import axios from "axios";
|
||||
import { API } from "../../api/index";
|
||||
|
||||
type E = React.FormEvent<HTMLInputElement>;
|
||||
|
||||
describe("<TosUpdate/>", () => {
|
||||
const instance = () => shallow(<TosUpdate />).instance() as TosUpdate;
|
||||
it("renders correctly when envs are set", () => {
|
||||
const oldTos = globalConfig.TOS_URL;
|
||||
const oldPriv = globalConfig.PRIV_URL;
|
||||
globalConfig.TOS_URL = "";
|
||||
globalConfig.PRIV_URL = "";
|
||||
const el = shallow(<TosUpdate />);
|
||||
expect(el.text().toLocaleLowerCase()).toContain("something went wrong");
|
||||
globalConfig.TOS_URL = oldTos;
|
||||
globalConfig.PRIV_URL = oldPriv;
|
||||
});
|
||||
|
||||
it("toggles server options", () => {
|
||||
const x = shallow(<TosUpdate />);
|
||||
const wow = x.instance() as TosUpdate;
|
||||
const wow = instance();
|
||||
wow.toggleServerOpts();
|
||||
expect(wow.state.hideServerSettings).toBeFalsy();
|
||||
wow.toggleServerOpts();
|
||||
|
@ -24,9 +52,7 @@ describe("<TosUpdate/>", () => {
|
|||
});
|
||||
|
||||
it("has a setter", () => {
|
||||
type E = React.FormEvent<HTMLInputElement>;
|
||||
const x = shallow(<TosUpdate />);
|
||||
const wow = x.instance() as TosUpdate;
|
||||
const wow = instance();
|
||||
wow.setState = jest.fn();
|
||||
const fn = wow.set("email");
|
||||
fn({ currentTarget: { value: "foo@bar.com" } } as E);
|
||||
|
@ -34,6 +60,25 @@ describe("<TosUpdate/>", () => {
|
|||
});
|
||||
|
||||
it("submits a form", () => {
|
||||
|
||||
type FormEvent = React.FormEvent<HTMLFormElement>;
|
||||
const fakeEvent: Partial<FormEvent> = { preventDefault: jest.fn() };
|
||||
const i = instance();
|
||||
i.setState({
|
||||
email: "foo@bar.com",
|
||||
password: "password123",
|
||||
agree_to_terms: true
|
||||
});
|
||||
i.forceUpdate();
|
||||
i.submit(fakeEvent as FormEvent);
|
||||
expect(fakeEvent.preventDefault).toHaveBeenCalled();
|
||||
const expectation = {
|
||||
user: {
|
||||
agree_to_terms: true,
|
||||
email: "foo@bar.com",
|
||||
password: "password123"
|
||||
}
|
||||
};
|
||||
expect(axios.post)
|
||||
.toHaveBeenCalledWith(API.current.tokensPath, expectation);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue