Skip to content

Commit c9c6bc5

Browse files
committed
Update unit tests with a more verbose testing style
1 parent 84c913d commit c9c6bc5

File tree

9 files changed

+181
-93
lines changed

9 files changed

+181
-93
lines changed

test-server/public/import1.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import url('./import2.css');
2+
@import url('./import3.css');
3+
4+
.third p {
5+
content: 'C';
6+
}

test-server/public/import2.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.first .inner > p span {
2+
content: 'A';
3+
}

test-server/public/import3.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import url('./import1.css');
2+
3+
.second {
4+
content: 'B';
5+
}

tests/cascading.spec.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Verify that styles and content will cascade.
2+
* Verify that content will cascade.
33
*/
44

55
import { test, expect } from '@playwright/test';
@@ -8,32 +8,54 @@ import { cssToHtml } from '../src/index';
88
const css = `
99
a {
1010
content: 'https://example.com/';
11-
border-radius: 5px;
1211
}
1312
a {
1413
content: 'https://example.com/page';
15-
border-radius: 10px;
14+
}
15+
.more p {}
16+
.more p span {
17+
content: 'A';
18+
}
19+
.more p span {
20+
content: 'B';
1621
}
1722
`;
1823

1924
test('Cascading', async ({ page }) => {
20-
await page.addScriptTag({ path: './dist/Generator.script.js' });
21-
22-
const result = await page.evaluate(async (css) => {
23-
document.body = await cssToHtml(css);
24-
const styleElement = document.createElement('style');
25-
styleElement.innerText = css;
26-
document.head.append(styleElement);
27-
28-
const element = document.body.querySelector('a');
29-
return element
30-
&& element.href === 'https://example.com/page'
31-
&& element.innerHTML === ''
32-
&& window.getComputedStyle(element).borderRadius === '10px'
33-
&& element.previousSibling === null
34-
&& element.nextSibling === null;
35-
}, css);
36-
37-
expect(result).toBeDefined();
38-
expect(result).toBe(true);
25+
await page.goto('http://localhost:5173/');
26+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css); return document.body.outerHTML; }, css);
27+
28+
// The body should have exactly two direct children.
29+
const bodyDirectChildren = page.locator('body > *');
30+
expect(await bodyDirectChildren.count()).toBe(2);
31+
32+
// There should be exactly one anchor element.
33+
const anchor = page.locator('a');
34+
expect(await anchor.count()).toBe(1);
35+
const anchorElement = await anchor.elementHandle();
36+
expect(anchorElement).toBeTruthy();
37+
38+
// The anchor's href attribute should be populated.
39+
const anchorHref = await anchorElement?.getAttribute('href');
40+
expect(anchorHref).toBe('https://example.com/page');
41+
42+
// The anchor should not have any text content.
43+
const anchorContent = await anchorElement?.innerHTML();
44+
expect(anchorContent).toBe('');
45+
46+
// The element with class `.more` should follow the anchor.
47+
const more = page.locator('a + .more');
48+
expect(await more.count()).toBe(1);
49+
const moreElement = await more.elementHandle();
50+
expect(moreElement).toBeTruthy();
51+
52+
// There should be exactly one span element.
53+
const span = page.locator('span');
54+
expect(await span.count()).toBe(1);
55+
const spanElement = await span.elementHandle();
56+
expect(spanElement).toBeTruthy();
57+
58+
// The span should have specific text content.
59+
const spanContent = await spanElement?.innerHTML();
60+
expect(spanContent).toBe('B');
3961
});

tests/comma.spec.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,45 @@ const css = `
99
h1,
1010
p.subtitle,
1111
p.content {
12-
content: 'a';
12+
content: 'A';
1313
}
1414
`;
1515

1616
test('Comma', async ({ page }) => {
17-
await page.addScriptTag({ path: './dist/Generator.script.js' });
17+
await page.goto('http://localhost:5173/');
18+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css); return document.body.outerHTML; }, css);
1819

19-
const result = await page.evaluate(async (css) => {
20-
document.body = await cssToHtml(css);
20+
// The body should have exactly three direct children.
21+
const bodyDirectChildren = page.locator('body > *');
22+
expect(await bodyDirectChildren.count()).toBe(3);
2123

22-
return document.body.querySelector('h1')?.innerHTML === 'a'
23-
&& document.body.querySelector('p.subtitle')?.innerHTML === 'a'
24-
&& document.body.querySelector('p.content')?.innerHTML === 'a';
25-
}, css);
24+
// There should be exactly one heading element.
25+
const heading = page.locator('h1');
26+
expect(await heading.count()).toBe(1);
27+
const headingElement = await heading.elementHandle();
28+
expect(headingElement).toBeTruthy();
2629

27-
expect(result).toBeDefined();
28-
expect(result).toBe(true);
30+
// The heading should have specific text content.
31+
const headingContent = await headingElement?.innerHTML();
32+
expect(headingContent).toBe('A');
33+
34+
// There should be exactly one element with class `.subtitle`.
35+
const subtitle = page.locator('.subtitle');
36+
expect(await subtitle.count()).toBe(1);
37+
const subtitleElement = await subtitle.elementHandle();
38+
expect(subtitleElement).toBeTruthy();
39+
40+
// The element with class `.subtitle` should have specific text content.
41+
const subtitleContent = await subtitleElement?.innerHTML();
42+
expect(subtitleContent).toBe('A');
43+
44+
// There should be exactly one element with class `.content`.
45+
const content = page.locator('.content');
46+
expect(await content.count()).toBe(1);
47+
const contentElement = await content.elementHandle();
48+
expect(contentElement).toBeTruthy();
49+
50+
// The element with class `.content` should have specific text content.
51+
const contentContent = await contentElement?.innerHTML();
52+
expect(contentContent).toBe('A');
2953
});

tests/ignored.spec.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@ const css = `
1010
background: #000;
1111
}
1212
* {
13-
content: 'a';
13+
content: 'A';
1414
box-sizing: border-box;
1515
}
1616
@media screen and (max-width: 200px) {}
1717
div {
18-
content: 'a';
18+
content: 'B';
1919
}
2020
div:hover {
21-
content: 'b';
21+
content: 'C';
22+
}
23+
@media screen and (max-width: 800px) {}
24+
.ignored > section::-webkit-scrollbar {
25+
width: 0.5rem;
2226
}
2327
`;
2428

2529
test('Ignored', async ({ page }) => {
26-
await page.addScriptTag({ path: './dist/Generator.script.js' });
27-
28-
const result = await page.evaluate(async (css) => {
29-
document.body = await cssToHtml(css);
30+
await page.goto('http://localhost:5173/');
31+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css); return document.body.outerHTML; }, css);
3032

31-
const element = document.body.querySelector('div');
32-
return element
33-
&& element.innerHTML === 'a'
34-
&& element.previousSibling === null
35-
&& element.nextSibling === null;
36-
}, css);
33+
// The body should have exactly one child.
34+
const bodyDirectChildren = page.locator('body *');
35+
expect(await bodyDirectChildren.count()).toBe(1);
36+
const element = await bodyDirectChildren.elementHandle();
37+
expect(element).toBeTruthy();
3738

38-
expect(result).toBeDefined();
39-
expect(result).toBe(true);
39+
// That element should have specific text content.
40+
const content = await element?.innerHTML();
41+
expect(content).toBe('B');
4042
});

tests/import.spec.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,33 @@ import { test, expect } from '@playwright/test';
66
import { cssToHtml } from '../src/index';
77

88
const css = `
9-
@import url('https://cascades.app/project/wBcGlZUV.css');
9+
@import url('http://localhost:5173/import1.css');
1010
div.last {
11-
content: 'a';
11+
content: 'D';
1212
}
1313
`;
1414

1515
test('Import', async ({ page }) => {
16-
await page.addScriptTag({ path: './dist/Generator.script.js' });
16+
await page.goto('http://localhost:5173/');
17+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css, { imports: 'include' }); return document.body.outerHTML; }, css);
1718

18-
page.on('console', msg => console.log(msg.text()));
19+
// The body should have exactly four direct children.
20+
const bodyDirectChildren = page.locator('body > *');
21+
expect(await bodyDirectChildren.count()).toBe(4);
1922

20-
const result = await page.evaluate(async (css) => {
21-
document.body = await cssToHtml(css);
23+
// The body's direct children should be in a specific order.
24+
const last = page.locator('.first:first-child + .second + .third + .last:last-child');
25+
expect(await last.count()).toBe(1);
26+
const lastElement = await last.elementHandle();
27+
expect(lastElement).toBeTruthy();
2228

23-
const element = document.body.querySelector('div.last');
24-
return element
25-
&& element.innerHTML === 'a'
26-
&& element.previousElementSibling !== null
27-
&& element.previousElementSibling.innerHTML === 'Test'
28-
&& element.nextSibling === null;
29-
}, css);
29+
// There should be exactly one span element.
30+
const span = page.locator('span');
31+
expect(await span.count()).toBe(1);
32+
const spanElement = await span.elementHandle();
33+
expect(spanElement).toBeTruthy();
3034

31-
expect(result).toBeDefined();
32-
expect(result).toBe(true);
35+
// The span should have specific text content.
36+
const spanContent = await spanElement?.innerHTML();
37+
expect(spanContent).toBe('A');
3338
});

tests/nth-child.spec.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,62 @@ import { test, expect } from '@playwright/test';
66
import { cssToHtml } from '../src/index';
77

88
const css = `
9-
span:nth-child(10) {
10-
color: red;
9+
span:nth-child(5) {
10+
content: 'A';
1111
}
1212
div.first:first-child {
13-
content: 'a';
13+
content: 'B';
1414
}
1515
span.last:last-child {
16-
content: 'b';
16+
content: 'C';
17+
}
18+
span:nth-child(8) {
19+
content: 'D';
1720
}
1821
span:last-child {
19-
content: 'c';
22+
content: 'E';
23+
}
24+
span.second-to-last:nth-last-child(2) {
25+
content: 'F';
2026
}
2127
`;
2228

2329
test('Nth-Child', async ({ page }) => {
24-
await page.addScriptTag({ path: './dist/Generator.script.js' });
30+
await page.goto('http://localhost:5173/');
31+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css); return document.body.outerHTML; }, css);
32+
33+
// The body should have exactly eight direct children.
34+
const bodyDirectChildren = page.locator('body > *');
35+
expect(await bodyDirectChildren.count()).toBe(8);
36+
37+
// The body's direct children should be in a specific order.
38+
const last = page.locator('div.first:first-child + span + span + span + span.last + span + span.second-to-last + span:last-child');
39+
expect(await last.count()).toBe(1);
40+
const lastElement = await last.elementHandle();
41+
expect(lastElement).toBeTruthy();
42+
43+
// The first element should have specific text content.
44+
const first = page.locator('div.first:first-child');
45+
expect(await first.count()).toBe(1);
46+
const firstElement = await first.elementHandle();
47+
const firstContent = await firstElement?.innerHTML();
48+
expect(firstContent).toBe('B');
2549

26-
const result = await page.evaluate(async (css) => {
27-
document.body = await cssToHtml(css);
50+
// The fifth element should have specific text content.
51+
const fifth = page.locator('span.last:nth-child(5)');
52+
expect(await fifth.count()).toBe(1);
53+
const fifthElement = await fifth.elementHandle();
54+
const fifthContent = await fifthElement?.innerHTML();
55+
expect(fifthContent).toBe('C');
2856

29-
return document.body.querySelectorAll('*').length === 10
30-
&& document.body.querySelector('div.first')?.previousElementSibling === null
31-
&& document.body.querySelector('div.first')?.innerHTML === 'a'
32-
&& document.body.querySelector('span.last')?.nextElementSibling === null
33-
&& document.body.querySelector('span.last')?.innerHTML === 'c';
34-
}, css);
57+
// The seventh element should have specific text content.
58+
const seventh = page.locator('span.second-to-last:nth-child(7)');
59+
expect(await seventh.count()).toBe(1);
60+
const seventhElement = await seventh.elementHandle();
61+
const seventhContent = await seventhElement?.innerHTML();
62+
expect(seventhContent).toBe('F');
3563

36-
expect(result).toBeDefined();
37-
expect(result).toBe(true);
64+
// The last element should have specific text content.
65+
const lastContent = await lastElement?.innerHTML();
66+
expect(lastContent).toBe('E');
3867
});

tests/selector.spec.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,16 @@ nav a#logo.icon> img {
1717
nav>a#logo.icon > img {
1818
content: 'https://example.com/image2';
1919
}
20+
nav input[type="text"].search[readonly] {
21+
content: 'Search';
22+
}
2023
`;
2124

22-
const html = `<body xmlns="http://www.w3.org/1999/xhtml"><div id="cat"></div><div class="mouse"><span class="flea"></span><i></i></div><nav><a class="icon" id="logo"><img src="https://example.com/image2" /></a></nav></body>`;
25+
const html = `<body><div id="cat"></div><div class="mouse"><span class="flea"></span><i></i></div><nav><a id="logo" class="icon" href=""><img src="https://example.com/image2"></a><input class="search" type="text" readonly="" placeholder="Search"></nav></body>`;
2326

2427
test('Selector', async ({ page }) => {
25-
await page.addScriptTag({ path: './dist/Generator.script.js' });
26-
27-
const result = await page.evaluate(async ([css, html]) => {
28-
document.body = await cssToHtml(css);
29-
const styleElement = document.createElement('style');
30-
styleElement.innerText = css;
31-
document.head.append(styleElement);
32-
33-
const xml = new XMLSerializer().serializeToString(document.body);
34-
35-
return xml.trim() === html.trim();
36-
}, [css, html]);
28+
await page.goto('http://localhost:5173/');
29+
const body = await page.evaluate(async (css) => { document.body = await cssToHtml(css); return document.body.outerHTML; }, css);
3730

38-
expect(result).toBeDefined();
39-
expect(result).toBe(true);
31+
expect(body).toBe(html);
4032
});

0 commit comments

Comments
 (0)