Skip to content

Commit b0e739b

Browse files
authored
test: more tests on TailwindCSS configuration (#74)
1 parent 0d36c81 commit b0e739b

File tree

10 files changed

+225
-0
lines changed

10 files changed

+225
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import plugin from 'tailwindcss/plugin';
2+
3+
/** @type {import('tailwindcss').Config} */
4+
export default {
5+
// Intentionally empty to ensure the plugin overrides it.
6+
content: [],
7+
plugins: [
8+
plugin(({ addUtilities }) => {
9+
addUtilities({
10+
'.from-plugin': {
11+
position: 'fixed',
12+
top: '0px',
13+
},
14+
});
15+
}),
16+
],
17+
};

test/plugin/index.test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import { expect, test } from '@playwright/test';
5+
import { createRsbuild } from '@rsbuild/core';
6+
7+
import { pluginTailwindCSS } from '../../src';
8+
import { getRandomPort, supportESM } from '../helper';
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
12+
test('plugin', async ({ page }) => {
13+
test.skip(
14+
!supportESM(),
15+
'Skip since the tailwindcss version does not support ESM configuration',
16+
);
17+
18+
const rsbuild = await createRsbuild({
19+
cwd: __dirname,
20+
rsbuildConfig: {
21+
plugins: [
22+
pluginTailwindCSS({
23+
config: './config/tailwind.config.js',
24+
}),
25+
],
26+
server: {
27+
port: getRandomPort(),
28+
},
29+
},
30+
});
31+
32+
await rsbuild.build();
33+
const { server, urls } = await rsbuild.preview();
34+
35+
try {
36+
await page.goto(urls[0]);
37+
await page.waitForSelector('#test', { state: 'attached' });
38+
39+
const style = await page.evaluate(() => {
40+
const el = document.getElementById('test');
41+
42+
if (!el) {
43+
throw new Error('#test not found');
44+
}
45+
46+
const computed = window.getComputedStyle(el);
47+
return {
48+
position: computed.getPropertyValue('position'),
49+
top: computed.getPropertyValue('top'),
50+
};
51+
});
52+
53+
expect(style.position).toBe('fixed');
54+
expect(style.top).toBe('0px');
55+
} finally {
56+
await server.close();
57+
}
58+
});

test/plugin/src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
const root = document.getElementById('root');
4+
const element = document.createElement('div');
5+
element.id = 'test';
6+
element.className = 'from-plugin';
7+
element.textContent = 'plugin';
8+
root.appendChild(element);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
// Intentionally empty to ensure the plugin overrides it.
4+
content: [],
5+
prefix: 'tw-',
6+
};

test/prefix/index.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import { expect, test } from '@playwright/test';
5+
import { createRsbuild } from '@rsbuild/core';
6+
7+
import { pluginTailwindCSS } from '../../src';
8+
import { getRandomPort, supportESM } from '../helper';
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
12+
test('prefix', async ({ page }) => {
13+
test.skip(
14+
!supportESM(),
15+
'Skip since the tailwindcss version does not support ESM configuration',
16+
);
17+
18+
const rsbuild = await createRsbuild({
19+
cwd: __dirname,
20+
rsbuildConfig: {
21+
plugins: [
22+
pluginTailwindCSS({
23+
config: './config/tailwind.config.js',
24+
}),
25+
],
26+
server: {
27+
port: getRandomPort(),
28+
},
29+
},
30+
});
31+
32+
await rsbuild.build();
33+
const { server, urls } = await rsbuild.preview();
34+
35+
try {
36+
await page.goto(urls[0]);
37+
await page.waitForSelector('#test', { state: 'attached' });
38+
39+
const display = await page.evaluate(() => {
40+
const el = document.getElementById('test');
41+
42+
if (!el) {
43+
throw new Error('#test not found');
44+
}
45+
46+
return window.getComputedStyle(el).getPropertyValue('display');
47+
});
48+
49+
expect(display).toBe('flex');
50+
} finally {
51+
await server.close();
52+
}
53+
});

test/prefix/src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
const root = document.getElementById('root');
4+
const element = document.createElement('div');
5+
element.id = 'test';
6+
element.className = 'tw-flex';
7+
element.textContent = 'prefix';
8+
root.appendChild(element);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
// Intentionally empty to ensure the plugin overrides it.
4+
content: [],
5+
theme: {
6+
extend: {
7+
colors: {
8+
brand: '#010203',
9+
},
10+
},
11+
},
12+
};

test/theme/index.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { dirname } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
4+
import { expect, test } from '@playwright/test';
5+
import { createRsbuild } from '@rsbuild/core';
6+
7+
import { pluginTailwindCSS } from '../../src';
8+
import { getRandomPort, supportESM } from '../helper';
9+
10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
12+
test('theme', async ({ page }) => {
13+
test.skip(
14+
!supportESM(),
15+
'Skip since the tailwindcss version does not support ESM configuration',
16+
);
17+
18+
const rsbuild = await createRsbuild({
19+
cwd: __dirname,
20+
rsbuildConfig: {
21+
plugins: [
22+
pluginTailwindCSS({
23+
config: './config/tailwind.config.js',
24+
}),
25+
],
26+
server: {
27+
port: getRandomPort(),
28+
},
29+
},
30+
});
31+
32+
await rsbuild.build();
33+
const { server, urls } = await rsbuild.preview();
34+
35+
try {
36+
await page.goto(urls[0]);
37+
await page.waitForSelector('#test', { state: 'attached' });
38+
39+
const color = await page.evaluate(() => {
40+
const el = document.getElementById('test');
41+
42+
if (!el) {
43+
throw new Error('#test not found');
44+
}
45+
46+
return window.getComputedStyle(el).getPropertyValue('color');
47+
});
48+
49+
expect(color).toBe('rgb(1, 2, 3)');
50+
} finally {
51+
await server.close();
52+
}
53+
});

test/theme/src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'tailwindcss/utilities.css';
2+
3+
const root = document.getElementById('root');
4+
const element = document.createElement('div');
5+
element.id = 'test';
6+
element.className = 'text-brand';
7+
element.textContent = 'theme';
8+
root.appendChild(element);

test/with-resource-query/index.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ test('should dev with resource query on rspack', async ({ page }) => {
2626
const { server, urls } = await rsbuild.startDevServer();
2727

2828
await page.goto(urls[0]);
29+
await page.waitForSelector('#test', { state: 'attached' });
2930

3031
const display = await page.evaluate(() => {
3132
const el = document.getElementById('test');
@@ -62,6 +63,7 @@ test('should build with resource query on rspack', async ({ page }) => {
6263
const { server, urls } = await rsbuild.preview();
6364

6465
await page.goto(urls[0]);
66+
await page.waitForSelector('#test', { state: 'attached' });
6567

6668
const display = await page.evaluate(() => {
6769
const el = document.getElementById('test');

0 commit comments

Comments
 (0)