|
| 1 | +import { join } from 'path'; |
| 2 | +import * as rewire from 'rewire'; |
| 3 | + |
| 4 | +const cleanCss = rewire('./cleancss'); |
| 5 | + |
| 6 | +import * as cleanCssFactory from './util/clean-css-factory'; |
| 7 | +import * as config from './util/config'; |
| 8 | +import * as helpers from './util/helpers'; |
| 9 | +import * as workerClient from './worker-client'; |
| 10 | + |
| 11 | + |
| 12 | +describe('clean css task', () => { |
| 13 | + |
| 14 | + describe('cleancss', () => { |
| 15 | + it('should return when the worker returns', async () => { |
| 16 | + // arrange |
| 17 | + const context = { }; |
| 18 | + const configFile: any = null; |
| 19 | + const spy = spyOn(workerClient, workerClient.runWorker.name).and.returnValue(Promise.resolve()); |
| 20 | + // act |
| 21 | + await (cleanCss as any).cleancss(context, null); |
| 22 | + // assert |
| 23 | + expect(spy).toHaveBeenCalledWith('cleancss', 'cleancssWorker', context, configFile); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should throw when the worker throws', async () => { |
| 27 | + // arrange |
| 28 | + const context = { }; |
| 29 | + const errorMessage = 'Simulating an error'; |
| 30 | + spyOn(workerClient, workerClient.runWorker.name).and.throwError(errorMessage); |
| 31 | + try { |
| 32 | + // act |
| 33 | + await (cleanCss as any).cleancss(context, null); |
| 34 | + throw new Error('Should never get here'); |
| 35 | + } catch (ex) { |
| 36 | + // assert |
| 37 | + expect(ex.message).toEqual(errorMessage, `Expected ex.message ${ex.message} to equal ${errorMessage}`); |
| 38 | + } |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('cleancssworker', () => { |
| 43 | + it('should throw when reading the file throws', async (done) => { |
| 44 | + const errorMessage = 'simulating an error'; |
| 45 | + try { |
| 46 | + // arrange |
| 47 | + const context = { buildDir: 'www'}; |
| 48 | + const cleanCssConfig = { sourceFileName: 'sourceFileName', destFileName: 'destFileName'}; |
| 49 | + spyOn(config, config.generateContext.name).and.returnValue(context); |
| 50 | + spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig); |
| 51 | + spyOn(helpers, helpers.readFileAsync.name).and.throwError(errorMessage); |
| 52 | + |
| 53 | + // act |
| 54 | + await (cleanCss as any).cleancssWorker(context, null); |
| 55 | + |
| 56 | + throw new Error('Should never get here'); |
| 57 | + } catch (ex) { |
| 58 | + expect(ex.message).toEqual(errorMessage); |
| 59 | + done(); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return what writeFileAsync returns', async (done) => { |
| 64 | + // arrange |
| 65 | + const context = { buildDir: 'www'}; |
| 66 | + const cleanCssConfig = { sourceFileName: 'sourceFileName', destFileName: 'destFileName'}; |
| 67 | + const fileContent = 'content'; |
| 68 | + const minifiedContent = 'someContent'; |
| 69 | + spyOn(config, config.generateContext.name).and.returnValue(context); |
| 70 | + spyOn(config, config.fillConfigDefaults.name).and.returnValue(cleanCssConfig); |
| 71 | + spyOn(helpers, helpers.readFileAsync.name).and.returnValue(Promise.resolve(fileContent)); |
| 72 | + spyOn(helpers, helpers.writeFileAsync.name).and.returnValue(Promise.resolve()); |
| 73 | + |
| 74 | + // use rewire to stub this since jasmine is insufficient |
| 75 | + const spy = jasmine.createSpy('mySpy').and.returnValue(Promise.resolve(minifiedContent)); |
| 76 | + cleanCss.__set__('runCleanCss', spy); |
| 77 | + |
| 78 | + // act |
| 79 | + await (cleanCss as any).cleancssWorker(context, null); |
| 80 | + |
| 81 | + // assert |
| 82 | + expect(config.generateContext).toHaveBeenCalledWith(context); |
| 83 | + expect(config.fillConfigDefaults).toHaveBeenCalledWith(null, (cleanCss as any).taskInfo.defaultConfigFile); |
| 84 | + expect(helpers.readFileAsync).toHaveBeenCalledWith(join(context.buildDir, cleanCssConfig.sourceFileName)); |
| 85 | + expect(helpers.writeFileAsync).toHaveBeenCalledWith(join(context.buildDir, cleanCssConfig.destFileName), minifiedContent); |
| 86 | + expect(spy).toHaveBeenCalledWith(cleanCssConfig, fileContent); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('runCleanCss', () => { |
| 92 | + it('should reject when minification errors out', async (done) => { |
| 93 | + // arrange |
| 94 | + const errorMessage = 'simulating an error'; |
| 95 | + const configFile = { options: {} }; |
| 96 | + const fileContent = 'fileContent'; |
| 97 | + let minifySpy: jasmine.Spy = null; |
| 98 | + try { |
| 99 | + const destinationFilePath = 'filePath'; |
| 100 | + const mockMinifier = { |
| 101 | + minify: () => {} |
| 102 | + }; |
| 103 | + minifySpy = spyOn(mockMinifier, mockMinifier.minify.name); |
| 104 | + spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier); |
| 105 | + |
| 106 | + // act |
| 107 | + const promise = (cleanCss as any).runCleanCss(configFile, fileContent, destinationFilePath); |
| 108 | + // call the callback from the spy's args |
| 109 | + const callback = minifySpy.calls.mostRecent().args[1]; |
| 110 | + callback(new Error(errorMessage), null); |
| 111 | + |
| 112 | + await promise; |
| 113 | + |
| 114 | + throw new Error('Should never get here'); |
| 115 | + } catch (ex) { |
| 116 | + // assert |
| 117 | + expect(ex.message).toEqual(errorMessage); |
| 118 | + done(); |
| 119 | + } |
| 120 | + }); |
| 121 | + |
| 122 | + it('should reject when minification has one or more errors', async (done) => { |
| 123 | + // arrange |
| 124 | + const configFile = { options: {} }; |
| 125 | + const fileContent = 'fileContent'; |
| 126 | + let minifySpy: jasmine.Spy = null; |
| 127 | + const minificationResponse = { |
| 128 | + errors: ['some error'] |
| 129 | + }; |
| 130 | + try { |
| 131 | + const destinationFilePath = 'filePath'; |
| 132 | + const mockMinifier = { |
| 133 | + minify: () => {} |
| 134 | + }; |
| 135 | + minifySpy = spyOn(mockMinifier, mockMinifier.minify.name); |
| 136 | + spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier); |
| 137 | + |
| 138 | + // act |
| 139 | + const promise = (cleanCss as any).runCleanCss(configFile, fileContent, destinationFilePath); |
| 140 | + // call the callback from the spy's args |
| 141 | + const callback = minifySpy.calls.mostRecent().args[1]; |
| 142 | + callback(null, minificationResponse); |
| 143 | + |
| 144 | + await promise; |
| 145 | + |
| 146 | + throw new Error('Should never get here'); |
| 147 | + } catch (ex) { |
| 148 | + // assert |
| 149 | + expect(ex.message).toEqual(minificationResponse.errors[0]); |
| 150 | + done(); |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return minified content', async (done) => { |
| 155 | + const configFile = { options: {} }; |
| 156 | + const fileContent = 'fileContent'; |
| 157 | + let minifySpy: jasmine.Spy = null; |
| 158 | + const minificationResponse = { |
| 159 | + styles: 'minifiedContent' |
| 160 | + }; |
| 161 | + const destinationFilePath = 'filePath'; |
| 162 | + const mockMinifier = { |
| 163 | + minify: () => {} |
| 164 | + }; |
| 165 | + minifySpy = spyOn(mockMinifier, mockMinifier.minify.name); |
| 166 | + spyOn(cleanCssFactory, cleanCssFactory.getCleanCssInstance.name).and.returnValue(mockMinifier); |
| 167 | + |
| 168 | + // act |
| 169 | + const promise = (cleanCss as any).runCleanCss(configFile, fileContent, destinationFilePath); |
| 170 | + // call the callback from the spy's args |
| 171 | + const callback = minifySpy.calls.mostRecent().args[1]; |
| 172 | + callback(null, minificationResponse); |
| 173 | + |
| 174 | + const result = await promise; |
| 175 | + expect(result).toEqual(minificationResponse.styles); |
| 176 | + expect(cleanCssFactory.getCleanCssInstance).toHaveBeenCalledWith(configFile.options); |
| 177 | + expect(minifySpy.calls.mostRecent().args[0]).toEqual(fileContent); |
| 178 | + done(); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
0 commit comments