Skip to content

Commit 912c553

Browse files
committed
add test coverage for polyfilling multiple sets of elements
1 parent d00ab88 commit 912c553

File tree

3 files changed

+104
-11
lines changed

3 files changed

+104
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ lib-cov
2424
coverage
2525
*.lcov
2626
playwright-report
27+
test-results
2728

2829
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
2930
.grunt

index.html

+36
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@
129129
);
130130
}
131131

132+
// This event listener is for E2E testing only
133+
document
134+
.getElementById('prepare-manual-polyfill')
135+
?.addEventListener('click', () => prepareManualPolyfill(), {
136+
once: true,
137+
});
138+
document
139+
.getElementById('apply-polyfill-manually-set1')
140+
?.addEventListener('click', () => {
141+
polyfill({
142+
elements: [
143+
document.getElementById('my-style-manual-anchor'),
144+
document.getElementById('my-style-manual-style-el'),
145+
],
146+
});
147+
});
148+
document
149+
.getElementById('apply-polyfill-manually-set2')
150+
?.addEventListener('click', () => {
151+
polyfill({
152+
elements: [
153+
document.getElementById('my-style-manual-anchor'),
154+
document.getElementById('my-style-manual-link-el'),
155+
document.getElementById('my-target-manual-inline-style'),
156+
],
157+
});
158+
});
159+
132160
const manualBtn = document.getElementById('apply-polyfill-manually');
133161
if (SUPPORTS_ANCHOR_POSITIONING) {
134162
manualBtn.innerText = 'Load Anchor Positioning CSS';
@@ -1087,6 +1115,14 @@ <h2>
10871115
Manually apply polyfill
10881116
</h2>
10891117
<button id="apply-polyfill-manually">Polyfill these elements</button>
1118+
<!-- This button is for E2E testing only -->
1119+
<div hidden id="anchor-manual-test-buttons">
1120+
<button id="prepare-manual-polyfill">Prepare</button>
1121+
<button id="apply-polyfill-manually-set1">Polyfill target 1</button>
1122+
<button id="apply-polyfill-manually-set2">
1123+
Polyfill target 2 and 3
1124+
</button>
1125+
</div>
10901126
<div class="demo-elements">
10911127
<div id="my-anchor-manual" class="anchor">Anchor</div>
10921128
<div id="my-target-manual-style-el" class="target">

tests/e2e/polyfill.test.ts

+67-11
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,16 @@ test('applies polyfill for `@position-fallback`', async ({ page }) => {
129129

130130
test('applies manual polyfill', async ({ page }) => {
131131
await page.locator('#apply-polyfill-manually').click();
132-
const anchorBox = await page.locator('#my-anchor-manual').boundingBox();
133-
const target1Box = await page
132+
const anchorBox = (await page.locator('#my-anchor-manual').boundingBox())!;
133+
const target1Box = (await page
134134
.locator('#my-target-manual-style-el')
135-
.boundingBox();
136-
const target2Box = await page
135+
.boundingBox())!;
136+
const target2Box = (await page
137137
.locator('#my-target-manual-link-el')
138-
.boundingBox();
139-
const target3Box = await page
138+
.boundingBox())!;
139+
const target3Box = (await page
140140
.locator('#my-target-manual-inline-style')
141-
.boundingBox();
142-
143-
if (!anchorBox || !target1Box || !target2Box || !target3Box) {
144-
return;
145-
}
141+
.boundingBox())!;
146142

147143
expect(target1Box.x + target1Box.width).toBeCloseTo(anchorBox.x, 0);
148144
expect(target1Box.y + target1Box.height).toBeCloseTo(anchorBox.y, 0);
@@ -153,3 +149,63 @@ test('applies manual polyfill', async ({ page }) => {
153149
expect(target3Box.x).toBeCloseTo(anchorBox.x + anchorBox.width, 0);
154150
expect(target3Box.y).toBeCloseTo(anchorBox.y + anchorBox.height, 0);
155151
});
152+
153+
test('applies manual polyfill for multiple elements separately', async ({
154+
page,
155+
}) => {
156+
const buttonContainer = page.locator('#anchor-manual-test-buttons');
157+
await buttonContainer.evaluate((node: HTMLDivElement) => {
158+
node.hidden = false;
159+
});
160+
await buttonContainer.waitFor({ state: 'visible' });
161+
162+
const prepareButton = page.locator('#prepare-manual-polyfill');
163+
await prepareButton.click();
164+
165+
const anchorBox = (await page.locator('#my-anchor-manual').boundingBox())!;
166+
const target1Box = (await page
167+
.locator('#my-target-manual-style-el')
168+
.boundingBox())!;
169+
const target2Box = (await page
170+
.locator('#my-target-manual-link-el')
171+
.boundingBox())!;
172+
const target3Box = (await page
173+
.locator('#my-target-manual-inline-style')
174+
.boundingBox())!;
175+
176+
expect(target1Box.x + target1Box.width).not.toBeCloseTo(anchorBox.x, 0);
177+
expect(target1Box.y + target1Box.height).not.toBeCloseTo(anchorBox.y, 0);
178+
179+
expect(target2Box.x).not.toBeCloseTo(anchorBox.x + anchorBox.width, 0);
180+
expect(target2Box.y + target2Box.height).not.toBeCloseTo(anchorBox.y, 0);
181+
182+
expect(target3Box.x).not.toBeCloseTo(anchorBox.x + anchorBox.width, 0);
183+
expect(target3Box.y).not.toBeCloseTo(anchorBox.y + anchorBox.height, 0);
184+
185+
const set1Button = page.locator('#apply-polyfill-manually-set1');
186+
const set2Button = page.locator('#apply-polyfill-manually-set2');
187+
188+
await set1Button.click();
189+
190+
const newTarget1Box = (await page
191+
.locator('#my-target-manual-style-el')
192+
.boundingBox())!;
193+
194+
expect(newTarget1Box.x + newTarget1Box.width).toBeCloseTo(anchorBox.x, 0);
195+
expect(newTarget1Box.y + newTarget1Box.height).toBeCloseTo(anchorBox.y, 0);
196+
197+
await set2Button.click();
198+
199+
const newTarget2Box = (await page
200+
.locator('#my-target-manual-link-el')
201+
.boundingBox())!;
202+
const newTarget3Box = (await page
203+
.locator('#my-target-manual-inline-style')
204+
.boundingBox())!;
205+
206+
expect(newTarget2Box.x).toBeCloseTo(anchorBox.x + anchorBox.width, 0);
207+
expect(newTarget2Box.y + newTarget2Box.height).toBeCloseTo(anchorBox.y, 0);
208+
209+
expect(newTarget3Box.x).toBeCloseTo(anchorBox.x + anchorBox.width, 0);
210+
expect(newTarget3Box.y).toBeCloseTo(anchorBox.y + anchorBox.height, 0);
211+
});

0 commit comments

Comments
 (0)