Skip to content

Commit 6f35c8d

Browse files
author
Michael Vurchio
committed
Add test
1 parent fb6ee1f commit 6f35c8d

File tree

7 files changed

+76
-48
lines changed

7 files changed

+76
-48
lines changed

test/compiler.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const svelte = require('svelte/compiler');
22
const cssModules = require('../index.js');
33

4-
module.exports = async ({ source, localIdentName }) => {
5-
const { code } = await svelte.preprocess(
4+
module.exports = async ({ source }, options) => {
5+
const { code } = await svelte.preprocess(
66
source,
77
[
8-
cssModules({
9-
localIdentName,
10-
})
8+
cssModules(options)
119
],
1210
{ filename : 'src/App.svelte' }
1311
);

test/getlocalidentname.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source = '<style>.red { color: red; }</style>\n<span class="$style.red">Red</span>';
4+
const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.red">Red</span>';
5+
6+
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
7+
const output = await compiler({
8+
source,
9+
}, {
10+
localIdentName: '[local]-123456',
11+
});
12+
13+
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
14+
});
15+
test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
16+
const output = await compiler({
17+
source: sourceShorthand,
18+
}, {
19+
localIdentName: '[local]-123456',
20+
});
21+
22+
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
23+
});
24+
25+
test('Avoid generated class to start with a non character', async () => {
26+
const output = await compiler({
27+
source,
28+
}, {
29+
localIdentName: '1[local]',
30+
});
31+
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
32+
});
33+
test('[Shorthand] Avoid generated class to start with a non character', async () => {
34+
const output = await compiler({
35+
source: sourceShorthand,
36+
}, {
37+
localIdentName: '1[local]',
38+
});
39+
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
40+
});
41+
42+
test('Avoid generated class to end with a hyphen', async () => {
43+
const output = await compiler({
44+
source,
45+
}, {
46+
localIdentName: '[local]-',
47+
});
48+
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
49+
});
50+
test('[Shorthand] Avoid generated class to end with a hyphen', async () => {
51+
const output = await compiler({
52+
source: sourceShorthand,
53+
}, {
54+
localIdentName: '[local]-',
55+
});
56+
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
57+
});

test/path.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sourceReactiveClass = '<style>.red { color: red; }</style>\n<span class:$.
66
test('Replace path on regular class', async () => {
77
const output = await compiler({
88
source,
9+
}, {
910
localIdentName: '[path][name]__[local]',
1011
});
1112

@@ -15,6 +16,7 @@ test('Replace path on regular class', async () => {
1516
test('Replace path on reactive class', async () => {
1617
const output = await compiler({
1718
source: sourceReactiveClass,
19+
}, {
1820
localIdentName: '[path][name]__[local]',
1921
});
2022

test/remove.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const expectedOutput = '<style>.red { color: red; }</style>\n<span class="">Blue
77
test('Remove unused CSS Modules from HTML attribute', async () => {
88
const output = await compiler({
99
source,
10+
}, {
1011
localIdentName: '[local]-123456',
1112
});
1213

@@ -15,6 +16,7 @@ test('Remove unused CSS Modules from HTML attribute', async () => {
1516
test('[Shorthand] Remove unused CSS Modules from HTML attribute', async () => {
1617
const output = await compiler({
1718
source: sourceShorthand,
19+
}, {
1820
localIdentName: '[local]-123456',
1921
});
2022

test/replace.test.js

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,16 @@
11
const compiler = require('./compiler.js');
22

33
const source = '<style>.red { color: red; }</style>\n<span class="$style.red">Red</span>';
4-
const sourceShorthand = '<style>.red { color: red; }</style>\n<span class="$.red">Red</span>';
54

6-
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
5+
test('Customize generated classname from getLocalIdentName', async () => {
76
const output = await compiler({
87
source,
9-
localIdentName: '[local]-123456',
8+
}, {
9+
localIdentName: '[local]-123456MC',
10+
getLocalIdentName: (context, localIdentName) => {
11+
return localIdentName.interpolated.toLowerCase();
12+
}
1013
});
1114

12-
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
13-
});
14-
test('[Shorthand] Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
15-
const output = await compiler({
16-
source: sourceShorthand,
17-
localIdentName: '[local]-123456',
18-
});
19-
20-
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
21-
});
22-
23-
test('Avoid generated class to start with a non character', async () => {
24-
const output = await compiler({
25-
source,
26-
localIdentName: '1[local]',
27-
});
28-
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
29-
});
30-
test('[Shorthand] Avoid generated class to start with a non character', async () => {
31-
const output = await compiler({
32-
source: sourceShorthand,
33-
localIdentName: '1[local]',
34-
});
35-
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
36-
});
37-
38-
test('Avoid generated class to end with a hyphen', async () => {
39-
const output = await compiler({
40-
source,
41-
localIdentName: '[local]-',
42-
});
43-
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
44-
});
45-
test('[Shorthand] Avoid generated class to end with a hyphen', async () => {
46-
const output = await compiler({
47-
source: sourceShorthand,
48-
localIdentName: '[local]-',
49-
});
50-
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
51-
});
15+
expect(output).toBe('<style>:global(.red-123456mc) { color: red; }</style>\n<span class="red-123456mc">Red</span>');
16+
});

test/target.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const expectedOutput =
3333
test('Target proper className from lookalike classNames', async () => {
3434
const output = await compiler({
3535
source,
36+
}, {
3637
localIdentName: '[local]-123',
3738
});
3839

@@ -42,6 +43,7 @@ test('Target proper className from lookalike classNames', async () => {
4243
test('[Shorthand] Target proper className from lookalike classNames', async () => {
4344
const output = await compiler({
4445
source: sourceShorthand,
46+
}, {
4547
localIdentName: '[local]-123',
4648
});
4749

test/toggle.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const expectedOutput =
2323
test('Generate CSS Modules className on class binding', async () => {
2424
const output = await compiler({
2525
source,
26+
}, {
2627
localIdentName: '[local]-123456',
2728
});
2829

@@ -32,6 +33,7 @@ test('Generate CSS Modules className on class binding', async () => {
3233
test('[Shorthand] Generate CSS Modules className on class binding', async () => {
3334
const output = await compiler({
3435
source: sourceShorthand,
36+
}, {
3537
localIdentName: '[local]-123456',
3638
});
3739

0 commit comments

Comments
 (0)