1 | import React from 'react';
|
2 | import { Popper } from 'react-popper';
|
3 | import '@testing-library/jest-dom';
|
4 | import { render, screen } from '@testing-library/react';
|
5 | import { PopperContent } from '..';
|
6 |
|
7 | describe('PopperContent', () => {
|
8 | let element;
|
9 |
|
10 | beforeEach(() => {
|
11 | element = document.createElement('div');
|
12 | element.innerHTML =
|
13 | '<p id="outerTarget">This is the popover <span id="target">target</span>.</p>';
|
14 | document.body.appendChild(element);
|
15 |
|
16 | jest.useFakeTimers();
|
17 | });
|
18 |
|
19 | afterEach(() => {
|
20 | jest.clearAllTimers();
|
21 | document.body.removeChild(element);
|
22 | element = null;
|
23 | });
|
24 |
|
25 | it('should NOT render children when isOpen is false', () => {
|
26 | render(<PopperContent target="target">Yo!</PopperContent>);
|
27 |
|
28 | expect(screen.queryByText('Yo!')).not.toBeInTheDocument();
|
29 | });
|
30 |
|
31 | it('should render children when isOpen is true and container is inline', () => {
|
32 | render(
|
33 | <PopperContent target="target" isOpen>
|
34 | Yo!
|
35 | </PopperContent>,
|
36 | );
|
37 |
|
38 | expect(screen.queryByText('Yo!')).toBeInTheDocument();
|
39 | });
|
40 |
|
41 | it('should render children when isOpen is true and container is inline and DOM node passed directly for target', () => {
|
42 | const targetElement = element.querySelector('#target');
|
43 |
|
44 | render(
|
45 | <PopperContent target={targetElement} isOpen container="inline">
|
46 | Yo!
|
47 | </PopperContent>,
|
48 | );
|
49 |
|
50 | expect(screen.queryByText('Yo!')).toBeInTheDocument();
|
51 | });
|
52 |
|
53 | it('should render an Arrow in the Popper when isOpen is true and container is inline', () => {
|
54 | const { container } = render(
|
55 | <PopperContent
|
56 | target="target"
|
57 | isOpen
|
58 | container="inline"
|
59 | arrowClassName="custom-arrow"
|
60 | >
|
61 | Yo!
|
62 | </PopperContent>,
|
63 | );
|
64 |
|
65 | expect(container.querySelector('.arrow.custom-arrow')).toBeInTheDocument();
|
66 | });
|
67 |
|
68 | it('should NOT render an Arrow in the Popper when "hideArrow" is truthy', () => {
|
69 | const { container } = render(
|
70 | <PopperContent
|
71 | target="target"
|
72 | isOpen
|
73 | container="inline"
|
74 | arrowClassName="custom-arrow"
|
75 | hideArrow
|
76 | >
|
77 | Yo!
|
78 | </PopperContent>,
|
79 | );
|
80 |
|
81 | expect(
|
82 | container.querySelector('.arrow.custom-arrow'),
|
83 | ).not.toBeInTheDocument();
|
84 | });
|
85 |
|
86 | it('should pass additional classNames to the popper', () => {
|
87 | render(
|
88 | <PopperContent
|
89 | className="extra"
|
90 | target="target"
|
91 | isOpen
|
92 | container="inline"
|
93 | data-testid="rick"
|
94 | >
|
95 | Yo!
|
96 | </PopperContent>,
|
97 | );
|
98 |
|
99 | expect(screen.getByTestId('rick')).toHaveClass('extra');
|
100 | });
|
101 |
|
102 | it('should allow custom modifiers and even allow overriding of default modifiers', () => {
|
103 | render(
|
104 | <PopperContent
|
105 | className="extra"
|
106 | target="target"
|
107 | isOpen
|
108 | container="inline"
|
109 | modifiers={[
|
110 | {
|
111 | name: 'offset',
|
112 | options: {
|
113 | offset: [2, 2],
|
114 | },
|
115 | },
|
116 | {
|
117 | name: 'preventOverflow',
|
118 | options: {
|
119 | boundary: 'viewport',
|
120 | },
|
121 | },
|
122 | ]}
|
123 | >
|
124 | Yo!
|
125 | </PopperContent>,
|
126 | );
|
127 |
|
128 | expect(Popper).toHaveBeenCalledWith(
|
129 | expect.objectContaining({
|
130 | modifiers: expect.arrayContaining([
|
131 | expect.objectContaining({
|
132 | name: 'offset',
|
133 | options: {
|
134 | offset: [2, 2],
|
135 | },
|
136 | }),
|
137 | expect.objectContaining({
|
138 | name: 'preventOverflow',
|
139 | options: {
|
140 | boundary: 'viewport',
|
141 | },
|
142 | }),
|
143 | ]),
|
144 | }),
|
145 | {},
|
146 | );
|
147 | });
|
148 |
|
149 | it('should have data-popper-placement of auto by default', () => {
|
150 | const { container } = render(
|
151 | <PopperContent target="target" isOpen container="inline">
|
152 | Yo!
|
153 | </PopperContent>,
|
154 | );
|
155 |
|
156 | expect(
|
157 | container.querySelector('div[data-popper-placement="auto"]'),
|
158 | ).toBeInTheDocument();
|
159 | });
|
160 |
|
161 | it('should override data-popper-placement', () => {
|
162 | const { container } = render(
|
163 | <PopperContent placement="top" target="target" isOpen container="inline">
|
164 | Yo!
|
165 | </PopperContent>,
|
166 | );
|
167 |
|
168 | expect(
|
169 | container.querySelector('div[data-popper-placement="auto"]'),
|
170 | ).not.toBeInTheDocument();
|
171 |
|
172 | expect(
|
173 | container.querySelector('div[data-popper-placement="top"]'),
|
174 | ).toBeInTheDocument();
|
175 | });
|
176 |
|
177 | it('should allow for a placement prefix', () => {
|
178 | render(
|
179 | <PopperContent
|
180 | placementPrefix="dropdown"
|
181 | target="target"
|
182 | isOpen
|
183 | container="inline"
|
184 | >
|
185 | Yo!
|
186 | </PopperContent>,
|
187 | );
|
188 |
|
189 | expect(screen.getByText('Yo!')).toHaveClass('dropdown-auto');
|
190 | });
|
191 |
|
192 | it('should allow for a placement prefix with custom placement', () => {
|
193 | const { container } = render(
|
194 | <PopperContent
|
195 | placementPrefix="dropdown"
|
196 | placement="top"
|
197 | target="target"
|
198 | isOpen
|
199 | container="inline"
|
200 | >
|
201 | Yo!
|
202 | </PopperContent>,
|
203 | );
|
204 |
|
205 | expect(screen.getByText('Yo!')).toHaveClass('dropdown-auto');
|
206 | expect(
|
207 | container.querySelector('div[data-popper-placement="top"]'),
|
208 | ).toBeInTheDocument();
|
209 | });
|
210 |
|
211 | it('should render custom tag for the popper', () => {
|
212 | render(
|
213 | <PopperContent
|
214 | tag="main"
|
215 | target="target"
|
216 | isOpen
|
217 | container="inline"
|
218 | data-testid="morty"
|
219 | >
|
220 | Yo!
|
221 | </PopperContent>,
|
222 | );
|
223 |
|
224 | expect(screen.getByTestId('morty').tagName.toLowerCase()).toBe('main');
|
225 | });
|
226 |
|
227 | it('should allow a function to be used as children', () => {
|
228 | const renderChildren = jest.fn();
|
229 | render(
|
230 | <PopperContent target="target" isOpen>
|
231 | {renderChildren}
|
232 | </PopperContent>,
|
233 | );
|
234 | expect(renderChildren).toHaveBeenCalled();
|
235 | });
|
236 |
|
237 | it('should render children properly when children is a function', () => {
|
238 | render(
|
239 | <PopperContent target="target" isOpen>
|
240 | {() => 'Yo!'}
|
241 | </PopperContent>,
|
242 | );
|
243 |
|
244 | expect(screen.queryByText('Yo!')).toBeInTheDocument();
|
245 | });
|
246 | });
|