/
api.test.js
340 lines (260 loc) · 12.7 KB
/
api.test.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"use strict";
const namer = require("@modular-css/test-utils/namer.js");
const relative = require("@modular-css/test-utils/relative.js");
const Processor = require("../processor.js");
describe("/processor.js", () => {
describe("API", () => {
let processor;
beforeEach(() => {
processor = new Processor({
namer,
});
});
it("should be a function", () => {
expect(typeof Processor).toBe("function");
});
describe(".string()", () => {
it("should process a string", async () => {
const result = await processor.string(
"./simple.css", ".wooga { }"
);
expect(result.exports).toMatchSnapshot();
expect(result.details.exports).toMatchSnapshot();
expect(result.details.text).toMatchSnapshot();
expect(result.details.processed.root.toResult().css).toMatchSnapshot();
});
});
describe(".file()", () => {
it("should process a relative file", async () => {
const result = await processor.file("./packages/processor/test/specimens/simple.css");
expect(result.exports).toMatchSnapshot();
expect(result.details.exports).toMatchSnapshot();
expect(result.details.text).toMatchSnapshot();
expect(result.details.processed.root.toResult().css).toMatchSnapshot();
});
it("should process an absolute file", async () => {
const result = await processor.file(
require.resolve("./specimens/simple.css")
);
expect(result.exports).toMatchSnapshot();
expect(result.details.exports).toMatchSnapshot();
expect(result.details.text).toMatchSnapshot();
expect(result.details.processed.root.toResult().css).toMatchSnapshot();
});
});
describe(".remove()", () => {
it("should remove a relative file", async () => {
await processor.string(
"./simple.css",
".wooga { }"
);
processor.remove("./simple.css");
expect(relative(processor.dependencies())).toMatchSnapshot();
});
it("should remove an absolute file", async () => {
await processor.string(
"./packages/processor/test/specimens/simple.css",
".wooga { }"
);
processor.remove(require.resolve("./specimens/simple.css"));
expect(relative(processor.dependencies())).toMatchSnapshot();
});
it("should remove multiple files", async () => {
await processor.string("./a.css", ".a { }");
await processor.string("./b.css", ".b { }");
await processor.string("./c.css", ".c { }");
processor.remove([
"./a.css",
"./b.css",
]);
expect(relative(processor.dependencies())).toMatchSnapshot();
});
it("should return an array of removed files", async () => {
await processor.string("./a.css", ".a { }");
await processor.string("./b.css", ".b { }");
await processor.string("./c.css", ".c { }");
expect(
relative(
processor.remove([
"./a.css",
"./b.css",
])
)
).toMatchSnapshot();
});
});
describe(".invalidate()", () => {
const status = (source) =>
Object.entries(source).map(([ key, value ]) =>
([ relative([ key ])[0], value.valid ])
);
it("should invalidate a relative file", async () => {
await processor.string(
"./simple.css",
".wooga { }"
);
processor.invalidate("./simple.css");
expect(status(processor.files)).toMatchSnapshot();
});
it("should invalidate an absolute file", async () => {
await processor.string(
"./packages/processor/test/specimens/simple.css",
".wooga { }"
);
processor.invalidate(require.resolve("./specimens/simple.css"));
expect(status(processor.files)).toMatchSnapshot();
});
it("should throw if no file is passed", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(() => processor.invalidate()).toThrowErrorMatchingSnapshot();
});
it("should throw if an invalid file is passed", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(() => processor.invalidate("nope.css")).toThrowErrorMatchingSnapshot();
});
it("should invalidate all dependents as well", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
processor.invalidate("./packages/processor/test/specimens/folder/folder.css");
expect(status(processor.files)).toMatchSnapshot();
});
it("should reprocess invalidated files", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
processor.invalidate("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/start.css");
expect(status(processor.files)).toMatchSnapshot();
});
});
describe(".dependencies()", () => {
it("should return the dependencies of the specified file", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(
relative(processor.dependencies(require.resolve("./specimens/start.css")))
)
.toMatchSnapshot();
});
it("should return the overall order of dependencies if no file is specified", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(relative(processor.dependencies())).toMatchSnapshot();
});
});
describe(".dependents()", () => {
it("should return the dependents of the specified file", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(
relative(processor.dependents(require.resolve("./specimens/local.css")))
)
.toMatchSnapshot();
});
it("should throw if no file is passed", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
expect(() => processor.dependents()).toThrowErrorMatchingSnapshot();
});
});
describe(".output()", () => {
it("should reject unknown files", async () => {
await expect(processor.output({
files : [
"./not/a/real/file",
],
})).rejects.toThrow("Unknown file requested");
});
it("should return a postcss result", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
const result = await processor.output();
expect(result.css).toMatchSnapshot();
});
it("should generate css representing the output from all added files", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/simple.css");
const result = await processor.output();
expect(result.css).toMatchSnapshot();
});
it("should avoid duplicating files in the output", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/local.css");
const result = await processor.output();
expect(result.css).toMatchSnapshot();
});
it("should generate a JSON structure of all the compositions", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
const result = await processor.output();
expect(result.compositions).toMatchSnapshot();
});
it("should order output by dependencies, then alphabetically", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/local.css");
await processor.file("./packages/processor/test/specimens/composes.css");
await processor.file("./packages/processor/test/specimens/deep.css");
const result = await processor.output();
expect(result.css).toMatchSnapshot();
});
it("should support returning output for specified relative files", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/local.css");
const result = await processor.output({
files : [
"./packages/processor/test/specimens/start.css",
],
});
expect(result.css).toMatchSnapshot();
});
it("should support returning output for specified absolute files", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
await processor.file("./packages/processor/test/specimens/local.css");
const result = await processor.output({
files : [
require.resolve("./specimens/start.css"),
],
});
expect(result.css).toMatchSnapshot();
});
it("should allow for seperate source map output", async () => {
await processor.file("./packages/processor/test/specimens/start.css");
const result = await processor.output({
map : {
inline : false,
},
});
expect(result.map).toMatchSnapshot();
});
});
describe("._resolve()", () => {
it("should run resolvers until a match is found", () => {
let ran = false;
processor = new Processor({
resolvers : [
() => {
ran = true;
},
],
});
expect(
relative([
processor._resolve(
require.resolve("./specimens/start.css"),
"./local.css"
),
])
)
.toMatchSnapshot();
expect(ran).toBeTruthy();
});
it("should fall back to a default resolver", () => {
processor = new Processor({
resolvers : [
() => undefined,
],
});
expect(
relative([
processor._resolve(
require.resolve("./specimens/start.css"),
"./local.css"
),
])
)
.toMatchSnapshot();
});
});
});
});