/
DilationForm.test.jsx
101 lines (78 loc) · 2.92 KB
/
DilationForm.test.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React from "react";
import { shallow } from "enzyme";
import TextAreaAutosize from "react-textarea-autosize";
import DilationForm from "../DilationForm";
const defaultSubmitEvent = {
preventDefault: () => {},
};
const defaultProps = {
onSubmit: jest.fn(),
};
const render = () => shallow(<DilationForm {...defaultProps} />);
describe("DilationForm", () => {
it("should render a form", () => {
const component = render();
expect(component.exists("StyledForm")).toEqual(true);
});
it("should render a label for the textarea", () => {
const component = render();
expect(component.exists("StyledLabel[htmlFor='matrix-input']")).toEqual(
true
);
});
it("should render a textarea", () => {
const component = render();
expect(component.find(TextAreaAutosize).prop("name")).toEqual(
"matrix-input"
);
});
it("should not call onSubmit from props if input is not a matrix", () => {
const component = render();
component.find("StyledForm").simulate("submit", defaultSubmitEvent);
expect(defaultProps.onSubmit.mock.calls.length).toBe(0);
});
it("should not call onSubmit if input is not s square matrix", () => {
const component = render();
component
.find(TextAreaAutosize)
.simulate("change", { target: { value: "1,2,3" } });
component.find("StyledForm").simulate("submit", defaultSubmitEvent);
expect(defaultProps.onSubmit.mock.calls.length).toBe(0);
});
const inputValues = [
"1.5,-2,\n3,4",
"1.5 -2,\n3, 4",
"1.5 -2,\n3, 4",
"1,5 -2\n3\t4",
"1,5 -2, 3 4,",
];
inputValues.forEach(inputValue => {
it(`should pass value ${inputValue} from textarea and degree to onSubmit`, () => {
const component = render();
component
.find(TextAreaAutosize)
.simulate("change", { target: { value: inputValue } });
component
.find("input[type='number']")
.simulate("change", { target: { value: 3 } });
component.find("StyledForm").simulate("submit", defaultSubmitEvent);
expect(defaultProps.onSubmit.mock.calls.length).toBe(1);
expect(defaultProps.onSubmit.mock.calls[0][0]).toEqual([
1.5,
-2,
3,
4,
]);
expect(defaultProps.onSubmit.mock.calls[0][1]).toEqual(3);
});
});
it("should render a input for the degree", () => {
const component = render();
const input = component.find("input[type='number']");
expect(input.prop("name")).toEqual("degree");
});
it("should render a label for the degree input", () => {
const component = render();
expect(component.exists("StyledLabel[htmlFor='degree']")).toEqual(true);
});
});