-
Notifications
You must be signed in to change notification settings - Fork 205
/
index.test.js
173 lines (155 loc) · 6.46 KB
/
index.test.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from "react";
import { shallow, mount } from "enzyme";
import Enzyme from "enzyme";
import Popup from "../src/";
import { shallowToJson } from "enzyme-to-json";
import { cleanup, fireEvent, render, waitForElement } from "react-testing-library";
import Adapter from "enzyme-adapter-react-16";
Enzyme.configure({ adapter: new Adapter() });
const PopupTest = props => (
<Popup {...props} trigger={<button> Trigger</button>}>
popup content
</Popup>
);
const PopupTriggerFunction = props => (
<Popup
{...props}
trigger={open => <button>Button nested {open ? "open" : "close"}</button>}
>
popup content
</Popup>
);
const PopupContentAsFunction = props => (
<Popup {...props} trigger={<button> Trigger</button>}>
{(close, open) => <div> Popup content {open ? "open" : "close"} </div>}
</Popup>
);
afterEach(() => cleanup());
test("it should render correctly ", () => {
const popup = shallow(<PopupTest on="click" />);
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should render correctly on click ", () => {
const { getByText } = render(<PopupTest />);
fireEvent.click(getByText("Trigger"));
expect(getByText("popup content")).toBeDefined();
});
test("it should not render when disabled", () => {
const { getByText, queryByText } = render(<PopupTest disabled />);
fireEvent.click(getByText("Trigger"));
expect(queryByText("popup content")).toBeNull();
});
test("it should render correctly on click (on = 'click') ", () => {
const { getByText } = render(<PopupTest on="click" />);
fireEvent.click(getByText("Trigger"));
expect(getByText("popup content")).toBeDefined();
});
test("it should render correctly on hover (on = 'hover') ", async () => {
const { getByText } = render(<PopupTest on="hover" />);
fireEvent.mouseOver(getByText("Trigger"));
await waitForElement(() => getByText("popup content"));
});
test("it should not render on hover (on = 'focus') ", async () => {
const { getByText, queryByText } = render(<PopupTest on="focus" />);
fireEvent.mouseOver(getByText("Trigger"));
expect(queryByText("popup content")).toBeNull();
});
test("it should not render on focus (on = 'hover') ", () => {
const { getByText, queryByText } = render(<PopupTest on="hover" />);
fireEvent.focus(getByText("Trigger"));
expect(queryByText("popup content")).toBeNull();
});
// trigger as function
test("it should render correctly on click and will update the trigger text ", () => {
const popup = mount(<PopupTriggerFunction />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
// content as function
test("it should render correctly on click and will update the the popup content text ", () => {
const popup = mount(<PopupContentAsFunction />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
// closeOnDocumentClick Tests PopupContentAsFunction
test("it shouldn't close on click outside popup ", () => {
const popup = mount(<PopupTest on="click" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
popup.find("div.popup-overlay").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should close on click outside popup (closeOnDocumentClick = true )", () => {
const popup = mount(
<PopupTest on="click" closeOnDocumentClick={true} />
);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
popup.find("div.popup-overlay").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
// position Tests
test("it should rendered in the top left position ", () => {
const popup = mount(<PopupTest on="click" position="top left" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the top center position ", () => {
const popup = mount(<PopupTest on="click" position="top center" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the top right position ", () => {
const popup = mount(<PopupTest on="click" position="top right" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the bottom left position ", () => {
const popup = mount(<PopupTest on="click" position="bottom left" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the bottom center position ", () => {
const popup = mount(<PopupTest on="click" position="bottom center" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the bottom right position ", () => {
const popup = mount(<PopupTest on="click" position="bottom right" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the right top position ", () => {
const popup = mount(<PopupTest on="click" position="right top" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the right center position ", () => {
const popup = mount(<PopupTest on="click" position="right center" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the right bottom position ", () => {
const popup = mount(<PopupTest on="click" position="right bottom" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the left top position ", () => {
const popup = mount(<PopupTest on="click" position="left top" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the left center position ", () => {
const popup = mount(<PopupTest on="click" position="left center" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});
test("it should rendered in the left bottom position ", () => {
const popup = mount(<PopupTest on="click" position="left bottom" />);
popup.find("button").simulate("click");
expect(shallowToJson(popup)).toMatchSnapshot();
});