-
Notifications
You must be signed in to change notification settings - Fork 6
/
popup.test.js
189 lines (153 loc) · 4.89 KB
/
popup.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import '../vwc-popup.js';
import '@vonage/vwc-button';
import 'chai-dom';
import {
waitNextTask,
textToDomToParent,
isolatedElementsCreation, waitInterval, noop,
} from '../../../test/test-helpers.js';
import {chaiDomDiff} from '@open-wc/semantic-dom-diff';
chai.use(chaiDomDiff);
const COMPONENT_NAME = 'vwc-popup';
describe('popup', () => {
let addElement = isolatedElementsCreation();
it(`${COMPONENT_NAME} is defined as a custom element`, async () => {
assert.exists(
customElements.get(COMPONENT_NAME)
);
});
it('should internal contents', async () => {
const addedElements = addElement(
textToDomToParent(`<${COMPONENT_NAME}></${COMPONENT_NAME}>`)
);
const actualElement = addedElements[0];
await waitNextTask();
expect(actualElement.shadowRoot.innerHTML).to.equalSnapshot();
});
describe('Popup default init', () => {
it('should reflect from attribute to property', async () => {
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME}></${COMPONENT_NAME}>`)
);
await actualElement.updateComplete;
expect(actualElement.anchor, 'anchor should be ""')
.to
.equal('');
expect(actualElement.open, 'open should be false')
.to
.equal(false);
expect(actualElement.arrow, 'arrow should be undefined')
.to
.equal(undefined);
expect(actualElement.corner, 'corner should be left')
.to
.equal('left');
expect(actualElement.dismissible, 'dismissible should be undefined')
.to
.equal(undefined);
expect(actualElement.strategy, 'strategy should be fixed')
.to
.equal('fixed');
});
});
describe(`show`, () => {
it(`should set "open" to true`, async () => {
addElement(
textToDomToParent(`<vwc-button id="anchor"></vwc-button>`)
);
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME}></${COMPONENT_NAME}>`)
);
actualElement.anchor = 'anchor';
await actualElement.updateComplete;
actualElement.show();
await actualElement.updateComplete;
expect(actualElement.open)
.to
.equal(true);
});
});
describe(`hide`, () => {
it(`should set "open" to false`, async () => {
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME} open></${COMPONENT_NAME}>`)
);
actualElement.hide();
await actualElement.updateComplete;
expect(actualElement.open)
.to
.equal(false);
});
});
describe(`anchor`, () => {
async function flushResizeEvents() {
await waitNextTask();
}
function triggerResize(element) {
element.style.height = '20px';
}
function expectASingleUpdatePositionCall(updatePositionCallCount, anchorElement) {
const observer = new ResizeObserver(async function () {
await waitNextTask();
expect(updatePositionCallCount).to.equal(1);
});
observer.observe(anchorElement);
}
async function setPopupAndAnchor() {
const [anchorElement] = addElement(
textToDomToParent(`<vwc-button style="position: absolute; left: 100px;" layout="outlined" id="anchor">Button</vwc-button>`)
);
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME} arrow anchor="anchor" open><div>This is my popup</div></${COMPONENT_NAME}>`)
);
actualElement.updatePosition = noop;
await actualElement.updateComplete;
return {anchorElement, actualElement};
}
it(`should not set popup open if anchor element does not exist`, async () => {
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME}></${COMPONENT_NAME}>`)
);
actualElement.anchor = 'anchor';
await actualElement.updateComplete;
actualElement.show();
await actualElement.updateComplete;
expect(actualElement.open)
.to
.equal(false);
});
it(`should init the popup as open if anchor element does not exist`, async () => {
const [actualElement] = addElement(
textToDomToParent(`<${COMPONENT_NAME} open></${COMPONENT_NAME}>`)
);
actualElement.anchor = 'anchor';
await actualElement.updateComplete;
expect(actualElement.open)
.to
.equal(false);
});
it(`should reposition when the anchor changes its size`, async function () {
const {anchorElement, actualElement} = await setPopupAndAnchor();
await flushResizeEvents();
let updatePositionCallCount = 0;
actualElement.updatePosition = function () {
updatePositionCallCount++;
};
triggerResize(anchorElement);
expectASingleUpdatePositionCall(updatePositionCallCount, anchorElement);
});
it(`should stop observing the anchor when changing an anchor`, async function () {
const {anchorElement, actualElement} = await setPopupAndAnchor();
await flushResizeEvents();
actualElement.anchor = '';
await waitNextTask();
let updatePositionCallCount = 0;
actualElement.updatePosition = function () {
updatePositionCallCount++;
};
anchorElement.style.height = '20px';
await waitNextTask();
expect(updatePositionCallCount).to.equal(0);
});
});
});