UNPKG

7.51 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path_1 = require("path");
4var cleanCss = require("./cleancss");
5var cleanCssFactory = require("./util/clean-css-factory");
6var config = require("./util/config");
7var helpers = require("./util/helpers");
8var workerClient = require("./worker-client");
9describe('clean css task', function () {
10 describe('cleancss', function () {
11 it('should return when the worker returns', function () {
12 // arrange
13 var context = {};
14 var configFile = null;
15 var spy = spyOn(workerClient, workerClient.runWorker.name).and.returnValue(Promise.resolve());
16 // act
17 return cleanCss.cleancss(context, null).then(function () {
18 // assert
19 expect(spy).toHaveBeenCalledWith('cleancss', 'cleancssWorker', context, configFile);
20 });
21 });
22 it('should throw when the worker throws', function () {
23 // arrange
24 var context = {};
25 var errorMessage = 'Simulating an error';
26 spyOn(workerClient, workerClient.runWorker.name).and.returnValue(Promise.reject(new Error(errorMessage)));
27 // act
28 return cleanCss.cleancss(context, null).then(function () {
29 throw new Error('Should never get here');
30 }).catch(function (err) {
31 // assert
32 expect(err.message).toEqual(errorMessage);
33 });
34 });
35 });
36 describe('cleancssworker', function () {
37 it('should throw when reading the file throws', function () {
38 var errorMessage = 'simulating an error';
39 // arrange
40 var context = { buildDir: 'www' };
41 var cleanCssConfig = { sourceFileName: 'sourceFileName', destFileName: 'destFileName' };
42 spyOn(config, config.generateContext.name).and.returnValue(context);
43 spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig);
44 spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.reject(new Error(errorMessage)));
45 // act
46 return cleanCss.cleancssWorker(context, null).then(function () {
47 throw new Error('Should never get here');
48 }).catch(function (err) {
49 expect(err.message).toEqual(errorMessage);
50 });
51 });
52 it('should return what writeFileAsync returns', function () {
53 // arrange
54 var context = { buildDir: 'www' };
55 var cleanCssConfig = { sourceFileName: 'sourceFileName', destFileName: 'destFileName' };
56 var fileContent = 'content';
57 var minifiedContent = 'someContent';
58 spyOn(config, config.generateContext.name).and.returnValue(context);
59 spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig);
60 spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve(fileContent));
61 spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve());
62 spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue({
63 minify: function (content, cb) {
64 cb(null, { styles: minifiedContent });
65 }
66 });
67 // act
68 return cleanCss.cleancssWorker(context, null).then(function () {
69 // assert
70 expect(config.generateContext).toHaveBeenCalledWith(context);
71 expect(config.fillConfigDefaults).toHaveBeenCalledWith(null, cleanCss.taskInfo.defaultConfigFile);
72 expect(helpers.readFileAsync).toHaveBeenCalledWith(path_1.join(context.buildDir, cleanCssConfig.sourceFileName));
73 expect(helpers.writeFileAsync).toHaveBeenCalledWith(path_1.join(context.buildDir, cleanCssConfig.destFileName), minifiedContent);
74 });
75 });
76 });
77 describe('runCleanCss', function () {
78 it('should reject when minification errors out', function () {
79 // arrange
80 var errorMessage = 'simulating an error';
81 var configFile = { options: {} };
82 var fileContent = 'fileContent';
83 var destinationFilePath = 'filePath';
84 var mockMinifier = {
85 minify: function () { }
86 };
87 var minifySpy = spyOn(mockMinifier, mockMinifier.minify.name);
88 spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier);
89 // act
90 var promise = cleanCss.runCleanCss(configFile, fileContent, destinationFilePath);
91 // call the callback from the spy's args
92 var callback = minifySpy.calls.mostRecent().args[1];
93 callback(new Error(errorMessage), null);
94 return promise.then(function () {
95 throw new Error('Should never get here');
96 }).catch(function (err) {
97 // assert
98 expect(err.message).toEqual(errorMessage);
99 });
100 });
101 it('should reject when minification has one or more errors', function () {
102 // arrange
103 var configFile = { options: {} };
104 var fileContent = 'fileContent';
105 var minificationResponse = {
106 errors: ['some error']
107 };
108 var destinationFilePath = 'filePath';
109 var mockMinifier = {
110 minify: function () { }
111 };
112 var minifySpy = spyOn(mockMinifier, mockMinifier.minify.name);
113 spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier);
114 // act
115 var promise = cleanCss.runCleanCss(configFile, fileContent, destinationFilePath);
116 // call the callback from the spy's args
117 var callback = minifySpy.calls.mostRecent().args[1];
118 callback(null, minificationResponse);
119 return promise.then(function () {
120 throw new Error('Should never get here');
121 }).catch(function (err) {
122 // assert
123 expect(err.message).toEqual(minificationResponse.errors[0]);
124 });
125 });
126 it('should return minified content', function () {
127 var configFile = { options: {} };
128 var fileContent = 'fileContent';
129 var minifySpy = null;
130 var minificationResponse = {
131 styles: 'minifiedContent'
132 };
133 var destinationFilePath = 'filePath';
134 var mockMinifier = {
135 minify: function () { }
136 };
137 minifySpy = spyOn(mockMinifier, mockMinifier.minify.name);
138 spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier);
139 // act
140 var promise = cleanCss.runCleanCss(configFile, fileContent, destinationFilePath);
141 // call the callback from the spy's args
142 var callback = minifySpy.calls.mostRecent().args[1];
143 callback(null, minificationResponse);
144 return promise.then(function (result) {
145 expect(result).toEqual(minificationResponse.styles);
146 expect(cleanCssFactory.getCleanCssInstance).toHaveBeenCalledWith(configFile.options);
147 expect(minifySpy.calls.mostRecent().args[0]).toEqual(fileContent);
148 });
149 });
150 });
151});