UNPKG

2.08 kBJavaScriptView Raw
1import path from 'path';
2import fs from 'fs';
3import compiler from './helpers/compiler';
4
5describe('slate-cssvar-loader ', () => {
6 beforeEach(() => {
7 jest.resetModules();
8 });
9
10 test('replaces CSS custom properties with liquid variables', async () => {
11 jest.mock('../config');
12 const stats = await compiler('../fixtures/test.css');
13 const output = stats.toJson().modules[0].source;
14
15 const expected = fs.readFileSync(
16 path.resolve(__dirname, './fixtures/expected.js'),
17 'utf8',
18 );
19 expect(output).toBe(expected);
20 });
21
22 test('missing liquid variable', async () => {
23 jest.mock('../config');
24 const stats = await compiler('../fixtures/invalid.css');
25 const output = stats.toJson().modules[0].source;
26
27 expect(output).toBe(
28 'throw new Error("Module build failed: Liquid variable not found for CSS variable INVALID");',
29 );
30 });
31
32 test('loader does not run when disabled', async () => {
33 jest.mock('../config', () => {
34 return {
35 cssVarLoaderEnable: false,
36 };
37 });
38 const stats = await compiler('../fixtures/test.css');
39 const output = stats.toJson().modules[0].source;
40
41 const expected = fs.readFileSync(
42 path.resolve(__dirname, './fixtures/expected-disabled.js'),
43 'utf8',
44 );
45 expect(output).toBe(expected);
46 });
47
48 test('loads from multiple css variable liquid files', async () => {
49 jest.mock('../config', () => {
50 const _path = require('path');
51 const currentDir = _path.dirname(require.resolve('./index.test.js'));
52 return {
53 cssVarLoaderEnable: true,
54 cssVarLoaderLiquidPath: [
55 _path.resolve(currentDir, 'fixtures/css-variables.liquid'),
56 _path.resolve(currentDir, 'fixtures/morevars.liquid'),
57 ],
58 };
59 });
60 const stats = await compiler('../fixtures/morevars.css');
61 const output = stats.toJson().modules[0].source;
62
63 const expected = fs.readFileSync(
64 path.resolve(__dirname, './fixtures/expected-morevars.js'),
65 'utf8',
66 );
67 expect(output).toBe(expected);
68 });
69});