Skip to content

Commit 8a51181

Browse files
author
Michael Vurchio
committed
Add test
1 parent 7553cb6 commit 8a51181

File tree

9 files changed

+4800
-1
lines changed

9 files changed

+4800
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# 1.0.0
44
- Ability to use the same class multiple time
5-
- use with svelte loader Ok
5+
- Use with svelte loader Ok
66

77
# 0.1.1
88
- Fix modules exports

example/webpack/Time.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { onDestroy } from 'svelte';
33
44
let date = new Date();
5+
const active = true;
56
$: time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
67
78
const interval = setInterval(() => date = new Date(), 1000);

package-lock.json

Lines changed: 4684 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"author": {
1515
"name": "micantoine"
1616
},
17+
"scripts": {
18+
"test": "jest"
19+
},
1720
"license": "MIT",
1821
"main": "index.js",
1922
"directories": {
@@ -26,6 +29,10 @@
2629
"dependencies": {
2730
"loader-utils": "^2.0.0"
2831
},
32+
"devDependencies": {
33+
"jest": "^26.0.1",
34+
"svelte": "^3.22.3"
35+
},
2936
"peerDependencies": {
3037
"svelte": ">1.44.0"
3138
},

test/compiler.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const svelte = require('svelte/compiler');
2+
const cssModules = require('../index.js');
3+
4+
module.exports = async ({ source, localIdentName }) => {
5+
const { code } = await svelte.preprocess(
6+
source,
7+
[
8+
cssModules({
9+
localIdentName,
10+
})
11+
],
12+
{ filename : 'App.svelte' }
13+
);
14+
15+
return code;
16+
};

test/remove.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source = '<style>.red { color: red; }</style>\n<span class="$style.blue">Blue</span>';
4+
5+
test('Remove unused CSS Modules from HTML attribute', async () => {
6+
const output = await compiler({
7+
source,
8+
localIdentName: '[local]-123456',
9+
});
10+
11+
expect(output).toBe('<style>.red { color: red; }</style>\n<span class="">Blue</span>');
12+
});

test/replace.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source = '<style>.red { color: red; }</style>\n<span class="$style.red">Red</span>';
4+
5+
test('Generate CSS Modules from HTML attributes, Replace CSS className', async () => {
6+
const output = await compiler({
7+
source,
8+
localIdentName: '[local]-123456',
9+
});
10+
11+
expect(output).toBe('<style>:global(.red-123456) { color: red; }</style>\n<span class="red-123456">Red</span>');
12+
});
13+
14+
test('Avoid generated class to start with a non character', async () => {
15+
const output = await compiler({
16+
source,
17+
localIdentName: '1[local]',
18+
});
19+
expect(output).toBe('<style>:global(._1red) { color: red; }</style>\n<span class="_1red">Red</span>');
20+
});
21+
22+
test('Avoid generated class to end with a hyphen', async () => {
23+
const output = await compiler({
24+
source,
25+
localIdentName: '[local]-',
26+
});
27+
expect(output).toBe('<style>:global(.red) { color: red; }</style>\n<span class="red">Red</span>');
28+
});

test/target.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source =
4+
`<style>
5+
.red { color: red; }
6+
.red-crimson { color: crimson; }
7+
.redMajenta { color: magenta; }
8+
</style>
9+
<span class="$style.red">Red</span>
10+
<span class="$style.red-crimson">Crimson</span>
11+
<span class="$style.redMajenta">Majenta</span>`;
12+
13+
test('Target proper className from lookalike classNames', async () => {
14+
const output = await compiler({
15+
source,
16+
localIdentName: '[local]-123',
17+
});
18+
19+
expect(output).toBe(
20+
`<style>
21+
:global(.red-123) { color: red; }
22+
:global(.red-crimson-123) { color: crimson; }
23+
:global(.redMajenta-123) { color: magenta; }
24+
</style>
25+
<span class="red-123">Red</span>
26+
<span class="red-crimson-123">Crimson</span>
27+
<span class="redMajenta-123">Majenta</span>`);
28+
});

test/toggle.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const compiler = require('./compiler.js');
2+
3+
const source =
4+
`<style>.red { color: red; }</style>
5+
<span
6+
class:$style.red={true}
7+
class="{true ? '$style.red' : ''}">Red</span>
8+
`;
9+
10+
test('Generate CSS Modules className on class binding', async () => {
11+
const output = await compiler({
12+
source,
13+
localIdentName: '[local]-123456',
14+
});
15+
16+
expect(output).toBe(
17+
`<style>:global(.red-123456) { color: red; }</style>
18+
<span
19+
class:red-123456={true}
20+
class="{true ? 'red-123456' : ''}">Red</span>
21+
`);
22+
});
23+

0 commit comments

Comments
 (0)