Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 2c6f87b

Browse files
committed
test(test.ts*): add unit tests and style integration test, pass.
1 parent 3059ffe commit 2c6f87b

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed

test/compiler.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { exec } from 'child_process';
2+
import { promisify } from 'util';
3+
import path from 'path';
4+
5+
const execPromise = promisify(exec);
6+
7+
test('CSS compilation logs contain expected output', async () => {
8+
const { stdout } = await execPromise('npm run compile', {
9+
cwd: path.join(__dirname, '../e2e/site'),
10+
});
11+
12+
console.log('stdout:', stdout);
13+
expect(stdout).toContain('Generating module static css');
14+
expect(stdout).toContain('.e2e_');
15+
expect(stdout).toContain('color: pink;');
16+
expect(stdout).toContain('@media (max-width: 1024px)');
17+
expect(stdout).toContain('color: aqua;');
18+
});

test/create.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { create } from '../src/core/method/create';
2+
3+
test('Style.create returns an object with string values', () => {
4+
const styleObject = {
5+
test: {
6+
color: 'red',
7+
fontSize: '16px',
8+
},
9+
};
10+
11+
const styles = create(styleObject);
12+
expect(typeof styles).toBe('object');
13+
expect(typeof styles.test).toBe('string');
14+
15+
const styleElement = document.createElement('style');
16+
styleElement.textContent = `.${styles.test} { color: ${styleObject.test.color}; font-size: ${styleObject.test.fontSize}; }`;
17+
document.head.appendChild(styleElement);
18+
19+
document.body.innerHTML = `<div class="${styles.test}">Test Element</div>`;
20+
const element = document.querySelector(`.${styles.test}`) as HTMLElement;
21+
const computedStyle = window.getComputedStyle(element);
22+
expect(computedStyle.color).toBe('red');
23+
expect(computedStyle.fontSize).toBe('16px');
24+
});

test/global.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { global } from '../src/core/method/global';
2+
3+
test('Style.global insert style into the head with data-scope="global"', () => {
4+
global({
5+
h1: {
6+
fontSize: 24,
7+
},
8+
});
9+
10+
const styleElements = document.head.querySelectorAll('style[data-scope="global"]');
11+
expect(styleElements.length).toBeGreaterThan(0);
12+
13+
const styleContent = styleElements[0].textContent;
14+
expect(styleContent).toContain('h1');
15+
expect(styleContent).toContain('font-size: 24px');
16+
});

test/root.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { root } from '../src/core/method/root';
2+
3+
test('Style.root insert style into the head with data-scope="root"', () => {
4+
root({
5+
'--color-heading': '#333',
6+
});
7+
8+
const styleElements = document.head.querySelectorAll('style[data-scope="root"]');
9+
expect(styleElements.length).toBeGreaterThan(0);
10+
11+
const styleContent = styleElements[0].textContent;
12+
expect(styleContent).toContain('--color-heading: #333');
13+
});

test/set.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { set } from '../src/core/method/set';
2+
3+
test('Style.set creates a class with correct styles', () => {
4+
const styleObject = {
5+
color: 'blue',
6+
margin: '10px',
7+
};
8+
9+
const style = set(styleObject);
10+
expect(typeof style).toBe('string');
11+
12+
const styleElement = document.createElement('style');
13+
styleElement.textContent = `.${style} { color: ${styleObject.color}; margin: ${styleObject.margin}; }`;
14+
document.head.appendChild(styleElement);
15+
16+
document.body.innerHTML = `<div class="${style}">Test Element</div>`;
17+
const element = document.querySelector(`.${style}`) as HTMLElement;
18+
const computedStyle = window.getComputedStyle(element);
19+
expect(computedStyle.color).toBe('blue');
20+
expect(computedStyle.margin).toBe('10px');
21+
});

test/style-integration.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Style from '../src/core';
2+
3+
test('Style.create returns an object with string values', () => {
4+
const styles = Style.create({ test: { color: 'aqua' } });
5+
expect(typeof styles).toBe('object');
6+
expect(typeof styles.test).toBe('string');
7+
});
8+
9+
test('Style.set returns a string', () => {
10+
const styleClass = Style.set({ color: 'aqua' });
11+
expect(typeof styleClass).toBe('string');
12+
});
13+
14+
test('Style.global returns undefined', () => {
15+
const result = Style.global({});
16+
expect(result).toBeUndefined();
17+
});
18+
19+
test('Style.root returns undefined', () => {
20+
const result = Style.root({});
21+
expect(result).toBeUndefined();
22+
});

0 commit comments

Comments
 (0)