|
1 |
| -import * as uglifyjs from './uglifyjs'; |
2 |
| -import * as configUtil from './util/config'; |
3 |
| -import * as workerClient from './worker-client'; |
4 |
| - |
5 |
| -describe('uglifyjs function', () => { |
6 |
| - beforeEach(() => { |
7 |
| - spyOn(configUtil, 'getUserConfigFile').and.returnValue('fileContents'); |
8 |
| - spyOn(workerClient, 'runWorker').and.returnValue(Promise.resolve()); |
9 |
| - }); |
| 1 | +import * as fs from 'fs'; |
| 2 | +import { join } from 'path'; |
10 | 3 |
|
11 |
| - it('should call workerClient function', () => { |
12 |
| - const context = {}; |
13 |
| - const configFile = 'configFileContents'; |
| 4 | +import * as uglifyLib from 'uglify-js'; |
14 | 5 |
|
15 |
| - return uglifyjs.uglifyjs(context, configFile).then(() => { |
16 |
| - expect(configUtil.getUserConfigFile).toHaveBeenCalledWith(context, uglifyjs.taskInfo, configFile); |
17 |
| - expect(workerClient.runWorker).toHaveBeenCalledWith('uglifyjs', 'uglifyjsWorker', context, 'fileContents'); |
18 |
| - }); |
19 |
| - }); |
| 6 | +import * as helpers from './util/helpers'; |
| 7 | +import * as uglifyTask from './uglifyjs'; |
20 | 8 |
|
21 |
| - it('should fail because it does not have a valid build context', () => { |
22 |
| - const context: null = null; |
23 |
| - const configFile = 'configFileContents'; |
24 | 9 |
|
25 |
| - expect(uglifyjs.uglifyjs(context, configFile)).toThrow(); |
26 |
| - }); |
| 10 | +describe('uglifyjs', () => { |
| 11 | + describe('uglifyjsWorkerImpl', () => { |
| 12 | + it('should call uglify for the appropriate files', () => { |
| 13 | + const buildDir = join('some', 'fake', 'dir', 'myApp', 'www', 'build'); |
| 14 | + const context = { |
| 15 | + buildDir: buildDir |
| 16 | + }; |
| 17 | + const fileNames = ['polyfills.js', 'sw-toolbox.js', '0.main.js', '0.main.js.map', '1.main.js', '1.main.js.map', 'main.js', 'main.js.map']; |
| 18 | + const mockMinfiedResponse = { |
| 19 | + code: 'code', |
| 20 | + map: 'map' |
| 21 | + }; |
| 22 | + const mockUglifyConfig = { |
| 23 | + mangle: true, |
| 24 | + compress: true |
| 25 | + }; |
27 | 26 |
|
28 |
| - it('should fail because it does not have a valid config file', () => { |
29 |
| - const context = {}; |
30 |
| - const configFile: null = null; |
| 27 | + spyOn(fs, 'readdirSync').and.returnValue(fileNames); |
| 28 | + const uglifySpy = spyOn(uglifyLib, 'minify').and.returnValue(mockMinfiedResponse); |
| 29 | + const writeFileSpy = spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve()); |
31 | 30 |
|
32 |
| - expect(uglifyjs.uglifyjs(context, configFile)).toThrow(); |
33 |
| - }); |
| 31 | + const promise = uglifyTask.uglifyjsWorkerImpl(context, mockUglifyConfig); |
| 32 | + |
| 33 | + return promise.then(() => { |
| 34 | + expect(uglifyLib.minify).toHaveBeenCalledTimes(3); |
| 35 | + expect(uglifySpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js')); |
| 36 | + expect(uglifySpy.calls.all()[0].args[1].compress).toEqual(true); |
| 37 | + expect(uglifySpy.calls.all()[0].args[1].mangle).toEqual(true); |
| 38 | + expect(uglifySpy.calls.all()[0].args[1].inSourceMap).toEqual(join(buildDir, '0.main.js.map')); |
| 39 | + expect(uglifySpy.calls.all()[0].args[1].outSourceMap).toEqual(join(buildDir, '0.main.js.map')); |
| 40 | + |
| 41 | + expect(uglifySpy.calls.all()[1].args[0]).toEqual(join(buildDir, '1.main.js')); |
| 42 | + expect(uglifySpy.calls.all()[1].args[1].compress).toEqual(true); |
| 43 | + expect(uglifySpy.calls.all()[1].args[1].mangle).toEqual(true); |
| 44 | + expect(uglifySpy.calls.all()[1].args[1].inSourceMap).toEqual(join(buildDir, '1.main.js.map')); |
| 45 | + expect(uglifySpy.calls.all()[1].args[1].outSourceMap).toEqual(join(buildDir, '1.main.js.map')); |
| 46 | + |
| 47 | + expect(uglifySpy.calls.all()[2].args[0]).toEqual(join(buildDir, 'main.js')); |
| 48 | + expect(uglifySpy.calls.all()[2].args[1].compress).toEqual(true); |
| 49 | + expect(uglifySpy.calls.all()[2].args[1].mangle).toEqual(true); |
| 50 | + expect(uglifySpy.calls.all()[2].args[1].inSourceMap).toEqual(join(buildDir, 'main.js.map')); |
| 51 | + expect(uglifySpy.calls.all()[2].args[1].outSourceMap).toEqual(join(buildDir, 'main.js.map')); |
| 52 | + |
| 53 | + expect(writeFileSpy).toHaveBeenCalledTimes(6); |
| 54 | + expect(writeFileSpy.calls.all()[0].args[0]).toEqual(join(buildDir, '0.main.js')); |
| 55 | + expect(writeFileSpy.calls.all()[0].args[1]).toEqual(mockMinfiedResponse.code); |
| 56 | + expect(writeFileSpy.calls.all()[1].args[0]).toEqual(join(buildDir, '0.main.js.map')); |
| 57 | + expect(writeFileSpy.calls.all()[1].args[1]).toEqual(mockMinfiedResponse.map); |
34 | 58 |
|
35 |
| - it('should not fail if a config is not passed', () => { |
36 |
| - const context = {}; |
37 |
| - let configFile: any; |
| 59 | + expect(writeFileSpy.calls.all()[2].args[0]).toEqual(join(buildDir, '1.main.js')); |
| 60 | + expect(writeFileSpy.calls.all()[2].args[1]).toEqual(mockMinfiedResponse.code); |
| 61 | + expect(writeFileSpy.calls.all()[3].args[0]).toEqual(join(buildDir, '1.main.js.map')); |
| 62 | + expect(writeFileSpy.calls.all()[3].args[1]).toEqual(mockMinfiedResponse.map); |
38 | 63 |
|
39 |
| - return uglifyjs.uglifyjs(context).then(() => { |
40 |
| - expect(configUtil.getUserConfigFile).toHaveBeenCalledWith(context, uglifyjs.taskInfo, configFile); |
41 |
| - expect(workerClient.runWorker).toHaveBeenCalledWith('uglifyjs', 'uglifyjsWorker', context, 'fileContents'); |
| 64 | + expect(writeFileSpy.calls.all()[4].args[0]).toEqual(join(buildDir, 'main.js')); |
| 65 | + expect(writeFileSpy.calls.all()[4].args[1]).toEqual(mockMinfiedResponse.code); |
| 66 | + expect(writeFileSpy.calls.all()[5].args[0]).toEqual(join(buildDir, 'main.js.map')); |
| 67 | + expect(writeFileSpy.calls.all()[5].args[1]).toEqual(mockMinfiedResponse.map); |
| 68 | + }); |
42 | 69 | });
|
43 | 70 | });
|
44 | 71 | });
|
0 commit comments