forked from akanix42/meteor-css-modules
-
Notifications
You must be signed in to change notification settings - Fork 1
/
css-modules-processor.tests.js
204 lines (178 loc) · 8.35 KB
/
css-modules-processor.tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* eslint-env node, mocha */
import './test-helpers/global-variables.stub';
import chai from 'chai';
import CssModulesProcessor from './css-modules-processor';
import { reloadOptions } from './options';
import { generatePreprocessedFileObject } from './test-helpers/generate-file-object';
const expect = chai.expect;
describe('CssModulesProcessor', function() {
describe('#process()', function() {
describe('file.contents', function() {
it('should transpile the passed in file', async function z() {
const file = {
importPath: './test.css',
contents: '.test { color: red; } .test2 { color: blue; }',
referencedImportPaths: [],
getPathInPackage() {
return './test.css';
}
};
const processor = new CssModulesProcessor({ ...reloadOptions() });
await processor.process(file);
expect(file.contents).to.equal('._test__test { color: red; } ._test__test2 { color: blue; }\n/*# sourceMappingURL=test.css.map */');
});
it('should not transpile passthrough files', async function z() {
const file = {
importPath: './test.css',
contents: '.test { color: red; } .test2 { color: blue; }',
referencedImportPaths: [],
getPathInPackage() {
return './test.css';
}
};
const pluginOptions = { ...reloadOptions() };
pluginOptions.passthroughPaths.push(/test/);
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file);
expect(file.contents).to.equal('.test { color: red; } .test2 { color: blue; }');
});
});
describe('file.tokens', function() {
it('should export the class names as an object', async function z() {
const file = {
importPath: './test.css',
contents: '.test { color: red; } .test-two { color: blue; }',
referencedImportPaths: [],
getPathInPackage() {
return './test.css';
}
};
const pluginOptions = { ...reloadOptions() };
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file);
expect(file.tokens).to.eql({
'test': '_test__test',
'test-two': '_test__test-two'
});
});
it('should camelcase the JS class names when the camelcase option is enabled', async function z() {
const file = {
importPath: './test.css',
contents: '.test { color: red; } .test-two { color: blue; }',
referencedImportPaths: [],
getPathInPackage() {
return './test.css';
}
};
const pluginOptions = { ...reloadOptions() };
pluginOptions.jsClassNamingConvention.camelCase = true;
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file);
expect(file.tokens).to.eql({
'test': '_test__test',
'testTwo': '_test__test-two'
});
});
it('should pull in tokens from imported files', async function z() {
const allFiles = new Map();
addFile(generatePreprocessedFileObject('./direct-import1.css', '.test { color: red; }'));
addFile(generatePreprocessedFileObject('./direct-import2.css', '.test { composes: test from "./indirect-import.css"; }'));
addFile(generatePreprocessedFileObject('./indirect-import.css', '.test { color: red; }'));
const file = generatePreprocessedFileObject('./test.css', '.test { composes: test from "./direct-import1.css"; } .test-two { composes: test from "./direct-import2.css"; }');
const pluginOptions = { ...reloadOptions() };
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file, allFiles);
expect(file.tokens).to.eql({
'test': '_test__test _direct_import1__test',
'test-two': '_test__test-two _direct_import2__test _indirect_import__test'
});
function addFile(file) {
allFiles.set(file.importPath, file);
}
});
});
describe('file.referencedImportPaths', function() {
it('should list all of the files that the current file imports', async function z() {
const allFiles = new Map();
addFile(generatePreprocessedFileObject('./direct-import1.css', '.test { color: red; }'));
addFile(generatePreprocessedFileObject('./direct-import2.css', '.test { composes: test from "./indirect-import.css"; }'));
addFile(generatePreprocessedFileObject('./indirect-import.css', '.test { color: red; }'));
const file = generatePreprocessedFileObject('./test.css', '.test { composes: test from "./direct-import1.css"; } .test-two { composes: test from "./direct-import2.css"; }');
const pluginOptions = { ...reloadOptions() };
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file, allFiles);
expect(file.referencedImportPaths).to.eql([
'D:/projects/meteor-css-modules/direct-import1.css',
'D:/projects/meteor-css-modules/direct-import2.css',
'D:/projects/meteor-css-modules/indirect-import.css'
]);
function addFile(file) {
allFiles.set(file.importPath, file);
}
});
it('should build a deduplicated list of all the files that the current file imports directly or indirectly', async function z() {
const allFiles = new Map();
addFile(generatePreprocessedFileObject('./direct-import1.css', '.test { color: red; }'));
addFile(generatePreprocessedFileObject('./direct-import2.css', '.test { composes: test from "./indirect-import.css"; }'));
addFile(generatePreprocessedFileObject('./indirect-import.css', '.test { composes: test from "./direct-import1.css"; }'));
const file = generatePreprocessedFileObject('./test.css', '.test { composes: test from "./direct-import1.css"; } .test-two { composes: test from "./direct-import2.css"; }');
const pluginOptions = { ...reloadOptions() };
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file, allFiles);
expect(file.referencedImportPaths).to.eql([
'D:/projects/meteor-css-modules/direct-import1.css',
'D:/projects/meteor-css-modules/direct-import2.css',
'D:/projects/meteor-css-modules/indirect-import.css'
]);
function addFile(file) {
allFiles.set(file.importPath, file);
}
});
});
describe('file.imports', function() {
it('should list the files that the current file imports directly', async function z() {
const allFiles = new Map();
addFile(generatePreprocessedFileObject('./direct-import1.css', '.test { color: red; }'));
addFile(generatePreprocessedFileObject('./direct-import2.css', '.test { composes: test from "./indirect-import.css"; }'));
addFile(generatePreprocessedFileObject('./indirect-import.css', '.test { color: red; }'));
const file = generatePreprocessedFileObject('./test.css', '.test { composes: test from "./direct-import1.css"; } .test-two { composes: test from "./direct-import2.css"; }');
const pluginOptions = { ...reloadOptions() };
const processor = new CssModulesProcessor(pluginOptions);
await processor.process(file, allFiles);
expect(file.imports).to.eql([
'./direct-import1.css',
'./direct-import2.css'
]);
function addFile(file) {
allFiles.set(file.importPath, file);
}
});
});
describe('file.sourcemap', function() {
it('should generate a sourcemap', async function z() {
const file = {
importPath: './test.css',
contents: '.test { color: red; } .test2 { color: blue; }',
referencedImportPaths: [],
getPathInPackage() {
return './test.css';
}
};
const processor = new CssModulesProcessor({ ...reloadOptions() });
await processor.process(file);
expect(file.sourceMap).to.eql({
'file': 'test.css',
'mappings': 'AAAA,eAAQ,WAAW,EAAE,CAAC,gBAAS,YAAY,EAAE',
'names': [],
'sources': [
'test.css'
],
'sourcesContent': [
'.test { color: red; } .test2 { color: blue; }'
],
'version': 3
});
});
});
});
});