UNPKG

5.05 kBJavaScriptView Raw
1'use strict';
2
3const lib = require('./compilation-helpers'),
4 amphoraFs = require('amphora-fs'),
5 configFile = require('./config-file-helpers'),
6 mockConsole = require('jest-mock-console').default;
7
8// Mocks
9amphoraFs.tryRequire = jest.fn();
10configFile.getConfigValue = jest.fn();
11
12describe('compilation helpers', () => {
13 describe('time', () => {
14 const fn = lib.time;
15
16 it('returns minutes and seconds when time is longer than a minute', () => {
17 const t1 = new Date(0),
18 t2 = new Date(1000 * 62); // 1m 2s
19
20 expect(fn(t2, t1)).toBe('1m 2.00s');
21 });
22
23 it('returns seconds when time is less than a minute', () => {
24 const t1 = new Date(0),
25 t2 = new Date(1000 * 48.5); // 48.50s
26
27 expect(fn(t2, t1)).toBe('48.50s');
28 });
29 });
30
31 describe('watcher', () => {
32 const fn = lib.watcher;
33
34 it('does not log .DS_Store files', () => {
35 const restoreConsole = mockConsole();
36
37 fn(null, '/some/path/with/.DS_Store');
38 expect(console.log).not.toHaveBeenCalled();
39 restoreConsole();
40 });
41
42 it('logs when called with another file', () => {
43 const restoreConsole = mockConsole();
44
45 fn(null, '/some/path/with/a.file');
46 expect(console.log).toHaveBeenCalled();
47 restoreConsole();
48 });
49 });
50
51 describe('bucket', () => {
52 const fn = lib.bucket;
53
54 it('returns first bucket', () => {
55 expect(fn('alpha')).toBe('a-d');
56 });
57
58 it('returns second bucket', () => {
59 expect(fn('foxtrot')).toBe('e-h');
60 });
61
62 it('returns third bucket', () => {
63 expect(fn('kilo')).toBe('i-l');
64 });
65
66 it('returns fourth bucket', () => {
67 expect(fn('papa')).toBe('m-p');
68 });
69
70 it('returns fifth bucket', () => {
71 expect(fn('quebec')).toBe('q-t');
72 });
73
74 it('returns sixth bucket', () => {
75 expect(fn('victor')).toBe('u-z');
76 });
77
78 it('puts numbers in the sixth bucket', () => {
79 expect(fn('0451')).toBe('u-z');
80 });
81
82 it('puts non-alphanumeric characters in the sixth bucket', () => {
83 expect(fn('_someFile')).toBe('u-z');
84 });
85 });
86
87 describe('unbucket', () => {
88 const fn = lib.unbucket;
89
90 it('returns matcher for bucket', () => {
91 expect(fn('_deps-a-d.js')).toBe('a-d');
92 });
93 });
94
95 describe('generateBundles', () => {
96 const fn = lib.generateBundles;
97
98 it('generates template bundles', () => {
99 expect(fn('_templates', 'js')['_templates-a-d.js']).toEqual('**/[a-d]*.js');
100 });
101 });
102
103 describe('transformPath', () => {
104 const fn = lib.transformPath;
105
106 it('does not transform unminified paths', () => {
107 expect(fn('_templates', 'public/js', false)('/path/to/file.js')).toBe('/path/to/file.js');
108 });
109
110 it('transforms minified paths', () => {
111 expect(fn('_templates', 'public/js', true)('/path/to/file.js')).toBe('public/js/_templates-e-h.js');
112 });
113 });
114
115 describe('determinePostCSSPlugins', () => {
116 const fn = lib.determinePostCSSPlugins,
117 pluginMock = jest.fn();
118
119 beforeEach(() => {
120 pluginMock.mockReset();
121 amphoraFs.tryRequire.mockReset();
122 });
123
124 it('uses the values passed in from the command if no config file is set', () => {
125 amphoraFs.tryRequire.mockReturnValue(pluginMock);
126
127 fn({ plugins: [ 'some-val' ]});
128 expect(pluginMock).toHaveBeenCalled();
129 });
130
131 it('throws an error if the plugin cannot be found when required', () => {
132 amphoraFs.tryRequire.mockReturnValue(undefined);
133
134 expect(() => fn({ plugins: [ 'some-val' ]})).toThrowError();
135 });
136
137 it('logs if the required plugin\'s invocation fails', () => {
138 const restoreConsole = mockConsole();
139
140 amphoraFs.tryRequire.mockReturnValue(() => { throw new Error('foo'); });
141
142 fn({ plugins: [ 'some-val' ]});
143 expect(console.error).toHaveBeenCalled();
144 restoreConsole();
145 });
146
147 it ('returns the plugin array from the config file if it exists', () => {
148 const restoreConsole = mockConsole();
149
150 configFile.getConfigValue.mockReturnValue([]);
151 amphoraFs.tryRequire.mockReturnValue(pluginMock);
152
153 fn({ plugins: [ 'some-val' ]});
154 expect(amphoraFs.tryRequire).toHaveBeenCalledTimes(0);
155 expect(pluginMock).not.toHaveBeenCalled();
156 restoreConsole();
157 });
158
159 it ('throws an error if the config file plugins property is not an array', () => {
160 const restoreConsole = mockConsole();
161
162 configFile.getConfigValue.mockReturnValue({});
163 fn({ plugins: [ 'some-val' ]});
164 expect(console.error).toHaveBeenCalled();
165 restoreConsole();
166 });
167 });
168
169 describe('getConfigFileOrBrowsersList', () => {
170 const fn = lib.getConfigFileOrBrowsersList;
171
172 it('returns a value from the config if one is found', () => {
173 configFile.getConfigValue.mockReturnValue({});
174 expect(fn('foo')).toEqual({});
175 });
176
177 it('returns a value defined in the file no config value is found', () => {
178 configFile.getConfigValue.mockReturnValue(undefined);
179 expect(fn('foo')).toEqual(lib.browserslist);
180 });
181 });
182});