/
Calculator.test.jsx
119 lines (103 loc) · 4.25 KB
/
Calculator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import React from "react";
import { shallow } from "enzyme";
import DilationForm from "../DilationForm";
import Result from "../Result";
import * as WindowContext from "../../WindowContext";
import Calculator from "../Calculator";
const createWindow = (
response = {
ok: true,
json: () => Promise.resolve({}),
},
fetch = jest.fn(() => Promise.resolve(response))
) => ({
fetch,
});
const render = (window = {}) => {
jest.spyOn(WindowContext, "useWindowContext").mockImplementation(
() => window
);
return shallow(<Calculator />);
};
describe("Calculator", () => {
it("should render", () => {
jest.spyOn(WindowContext, "useWindowContext").mockImplementation(
() => ({})
);
const component = render();
expect(component).toMatchSnapshot();
});
describe("on client", () => {
it("call fetch when onSubmit on DilationForm is called", async () => {
const window = createWindow();
const component = render(window);
await component.find(DilationForm).prop("onSubmit")();
expect(window.fetch.mock.calls.length).toEqual(1);
});
it("should set isLoading prop during fetch", async () => {
const response = {
ok: false,
json: () => Promise.resolve({}),
};
const window = createWindow(response);
const component = render(window);
response.json = () => {
expect(component.find(Result).prop("isLoading")).toEqual(true);
return Promise.resolve({});
};
expect(component.find(Result).prop("isLoading")).toEqual(false);
await component.find(DilationForm).prop("onSubmit")();
expect(component.find(Result).prop("isLoading")).toEqual(false);
});
it("should render the dilation after is has been fetched", async () => {
const dilation = [1, 2, 3, 4];
const response = {
ok: true,
json: () => Promise.resolve({ value: dilation }),
};
const component = render(createWindow(response));
await component.find(DilationForm).prop("onSubmit")();
expect(component.find(Result).prop("dilation")).toEqual(dilation);
});
describe("should handle errors", () => {
it("and not throw an exception if the fetch is not ok", async () => {
const responseBody = {
validationError: {
value: ["value must represent a real contraction"],
},
};
const response = {
ok: false,
json: () => Promise.resolve(responseBody),
};
const component = render(createWindow(response));
await component.find(DilationForm).prop("onSubmit")();
expect(component.find(Result).prop("errorDetails")).toEqual(
responseBody
);
});
it("and catch an exception thrown by fetch", async () => {
const error = new Error("Mock Error");
const fetch = jest.fn(() => Promise.reject(error));
const component = render(createWindow(null, fetch));
await component.find(DilationForm).prop("onSubmit")();
expect(component.find(Result).prop("errorDetails")).toEqual(
error
);
});
it("and not render dilation from a first fetch if a second fetch throws", async () => {
const window = createWindow();
const component = render(window);
await component.find(DilationForm).prop("onSubmit")();
const mockError = new Error("Mock Error");
window.fetch.mockImplementation(() =>
Promise.reject(mockError)
);
await component.find(DilationForm).prop("onSubmit")();
const result = component.find(Result);
expect(result.prop("dilation")).toBe(null);
expect(result.prop("errorDetails")).toEqual(mockError);
});
});
});
});