/
PopperContent.spec.js
168 lines (126 loc) · 5.78 KB
/
PopperContent.spec.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
import React from 'react';
import { shallow, mount } from 'enzyme';
import { Popper } from 'react-popper';
import { PopperContent } from '../';
describe('PopperContent', () => {
let element;
let container;
beforeEach(() => {
element = document.createElement('div');
container = document.createElement('div');
element.innerHTML = '<p id="outerTarget">This is the popover <span id="target">target</span>.</p>';
container.setAttribute('id', 'container');
element.appendChild(container);
document.body.appendChild(element);
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
document.body.removeChild(element);
element = null;
});
it('should render a null by default', () => {
const wrapper = shallow(<PopperContent target="target">Yo!</PopperContent>);
expect(wrapper.type()).toBe(null);
});
it('should NOT render children when isOpen is false', () => {
const wrapper = shallow(<PopperContent target="target">Yo!</PopperContent>);
expect(wrapper.type()).toBe(null);
});
it('should render children when isOpen is true and container is inline', () => {
const wrapper = mount(<PopperContent target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.text()).toBe('Yo!');
});
it('should render children when isOpen is true and container is inline and DOM node passed directly for target', () => {
const targetElement = element.querySelector('#target');
const wrapper = mount(<PopperContent target={targetElement} isOpen container="inline">Yo!</PopperContent>);
expect(targetElement).toBeDefined();
expect(wrapper.text()).toBe('Yo!');
});
it('should render an Arrow in the Popper when isOpen is true and container is inline', () => {
const wrapper = mount(<PopperContent target="target" isOpen container="inline" arrowClassName="custom-arrow">Yo!</PopperContent>);
expect(wrapper.containsMatchingElement(<span className="arrow custom-arrow" />)).toBe(true);
});
it('should NOT render an Arrow in the Popper when "hideArrow" is truthy', () => {
const wrapper = mount(<PopperContent target="target" isOpen container="inline" arrowClassName="custom-arrow" hideArrow>Yo!</PopperContent>);
expect(wrapper.containsMatchingElement(<span className="arrow custom-arrow" />)).toBe(false);
});
it('should render with "hideArrow" false by default', () => {
const wrapper = mount(<PopperContent target="target">Yo!</PopperContent>);
expect(wrapper.prop('hideArrow')).toBe(false);
});
it('should render with "hideArrow" true when "hideArrow" prop is truthy', () => {
const wrapper = mount(<PopperContent target="target" hideArrow>Yo!</PopperContent>);
expect(wrapper.prop('hideArrow')).toBe(true);
});
it('should not render children', () => {
const wrapper = shallow(<PopperContent target="target">Yo!</PopperContent>);
expect(wrapper.type()).toBe(null);
});
it('should pass additional classNames to the popper', () => {
const wrapper = shallow(<PopperContent className="extra" target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.hasClass('extra')).toBe(true);
});
it('should allow custom modifiers and even allow overriding of default modifiers', () => {
const wrapper = mount(
<PopperContent
className="extra"
target="target"
isOpen
container="inline"
modifiers={{
preventOverflow: { boundariesElement: 'viewport' },
offset: { offset: 2 },
}}
>Yo!</PopperContent>
);
expect(wrapper.find(Popper).props().modifiers).toMatchObject({
// remaining default modifiers
flip: { enabled: true, behavior: 'flip' },
// additional modifiers
preventOverflow: { boundariesElement: 'viewport' },
// override modifiers
offset: { offset: 2 },
});
wrapper.unmount();
});
it('should have x-placement of auto by default', () => {
const wrapper = mount(<PopperContent target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.find('div[x-placement="auto"]').exists()).toBe(true);
});
it('should override x-placement', () => {
const wrapper = mount(<PopperContent placement="top" target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.find('div[x-placement="auto"]').exists()).toBe(false);
expect(wrapper.find('div[x-placement="top"]').exists()).toBe(true);
});
it('should allow for a placement prefix', () => {
const wrapper = mount(<PopperContent placementPrefix="dropdown" target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.find('.dropdown-auto').exists()).toBe(true);
});
it('should allow for a placement prefix with custom placement', () => {
const wrapper = mount(<PopperContent placementPrefix="dropdown" placement="top" target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.find('.dropdown-auto').exists()).toBe(true);
expect(wrapper.find('div[x-placement="top"]').exists()).toBe(true);
});
it('should render custom tag for the popper', () => {
const wrapper = mount(<PopperContent tag="main" target="target" isOpen container="inline">Yo!</PopperContent>);
expect(wrapper.getDOMNode().tagName.toLowerCase()).toBe('main');
});
it('should allow a function to be used as children', () => {
const renderChildren = jest.fn();
const wrapper = mount(
<PopperContent target="target" isOpen>
{renderChildren}
</PopperContent>
);
expect(renderChildren).toHaveBeenCalled();
});
it('should render children properly when children is a function', () => {
const wrapper = mount(
<PopperContent target="target" isOpen>
{() => 'Yo!'}
</PopperContent>
);
expect(wrapper.text()).toBe('Yo!');
});
});