|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var td = require('testdouble'); |
| 4 | +var formatter = require('stylelint').formatters.string; |
| 5 | + |
| 6 | +var runCompilation = td.replace('../../lib/run-compilation'); |
| 7 | + |
| 8 | +var LintDirtyModulesPlugin = require('../../lib/lint-dirty-modules-plugin'); |
| 9 | + |
| 10 | +var configFilePath = getPath('./.stylelintrc'); |
| 11 | +var glob = '/**/*.s?(c|a)ss'; |
| 12 | + |
| 13 | +describe('lint-dirty-modules-plugin', function () { |
| 14 | + context('lintDirtyModulesOnly flag is enabled', function () { |
| 15 | + var LintDirtyModulesPluginCloned; |
| 16 | + var compilerMock; |
| 17 | + var optionsMock; |
| 18 | + |
| 19 | + beforeEach(function () { |
| 20 | + LintDirtyModulesPluginCloned = function () { |
| 21 | + LintDirtyModulesPlugin.apply(this, arguments); |
| 22 | + }; |
| 23 | + LintDirtyModulesPluginCloned.prototype = Object.create(LintDirtyModulesPlugin.prototype); |
| 24 | + LintDirtyModulesPluginCloned.prototype.constructor = LintDirtyModulesPlugin; |
| 25 | + |
| 26 | + compilerMock = { |
| 27 | + callback: null, |
| 28 | + plugin: function plugin (event, callback) { |
| 29 | + this.callback = callback; |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + optionsMock = { |
| 34 | + configFile: configFilePath, |
| 35 | + lintDirtyModulesOnly: true, |
| 36 | + fomatter: formatter, |
| 37 | + files: [glob] |
| 38 | + }; |
| 39 | + }); |
| 40 | + |
| 41 | + it('lint is called on \'emit\'', function () { |
| 42 | + var lintStub = td.function(); |
| 43 | + var doneStub = td.function(); |
| 44 | + LintDirtyModulesPluginCloned.prototype.lint = lintStub; |
| 45 | + var plugin = new LintDirtyModulesPluginCloned(compilerMock, optionsMock); |
| 46 | + |
| 47 | + var compilationMock = { |
| 48 | + fileTimestamps: { |
| 49 | + '/udpated.scss': 5 |
| 50 | + } |
| 51 | + }; |
| 52 | + compilerMock.callback(compilationMock, doneStub); |
| 53 | + |
| 54 | + expect(plugin.isFirstRun).to.eql(true); |
| 55 | + td.verify(lintStub(compilationMock, doneStub)); |
| 56 | + }); |
| 57 | + |
| 58 | + context('LintDirtyModulesPlugin.prototype.lint()', function () { |
| 59 | + var getChangedFilesStub; |
| 60 | + var doneStub; |
| 61 | + var compilationMock; |
| 62 | + var fileTimestamps = { |
| 63 | + '/test/changed.scss': 5, |
| 64 | + '/test/newly-created.scss': 5 |
| 65 | + }; |
| 66 | + var pluginMock; |
| 67 | + beforeEach(function () { |
| 68 | + getChangedFilesStub = td.function(); |
| 69 | + doneStub = td.function(); |
| 70 | + compilationMock = { |
| 71 | + fileTimestamps: {} |
| 72 | + }; |
| 73 | + td.when(getChangedFilesStub({}, glob)).thenReturn([]); |
| 74 | + td.when(getChangedFilesStub(fileTimestamps, glob)).thenReturn(Object.keys(fileTimestamps)); |
| 75 | + pluginMock = { |
| 76 | + getChangedFiles: getChangedFilesStub, |
| 77 | + compiler: compilerMock, |
| 78 | + options: optionsMock, |
| 79 | + isFirstRun: true |
| 80 | + }; |
| 81 | + }); |
| 82 | + |
| 83 | + it('skips compilation on first run', function () { |
| 84 | + expect(pluginMock.isFirstRun).to.eql(true); |
| 85 | + LintDirtyModulesPluginCloned.prototype.lint.call(pluginMock, compilationMock, doneStub); |
| 86 | + td.verify(doneStub()); |
| 87 | + expect(pluginMock.isFirstRun).to.eql(false); |
| 88 | + td.verify(getChangedFilesStub, {times: 0, ignoreExtraArgs: true}); |
| 89 | + td.verify(runCompilation, {times: 0, ignoreExtraArgs: true}); |
| 90 | + }); |
| 91 | + |
| 92 | + it('runCompilation is not called if files are not changed', function () { |
| 93 | + pluginMock.isFirstRun = false; |
| 94 | + LintDirtyModulesPluginCloned.prototype.lint.call(pluginMock, compilationMock, doneStub); |
| 95 | + td.verify(doneStub()); |
| 96 | + td.verify(runCompilation, {times: 0, ignoreExtraArgs: true}); |
| 97 | + }); |
| 98 | + |
| 99 | + it('runCompilation is called if styles are changed', function () { |
| 100 | + pluginMock.isFirstRun = false; |
| 101 | + compilationMock = { |
| 102 | + fileTimestamps: fileTimestamps |
| 103 | + }; |
| 104 | + LintDirtyModulesPluginCloned.prototype.lint.call(pluginMock, compilationMock, doneStub); |
| 105 | + optionsMock.files = Object.keys(fileTimestamps); |
| 106 | + td.verify(runCompilation(optionsMock, compilerMock, doneStub)); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + context('LintDirtyModulesPlugin.prototype.getChangedFiles()', function () { |
| 111 | + var pluginMock; |
| 112 | + before(function () { |
| 113 | + pluginMock = { |
| 114 | + compiler: compilerMock, |
| 115 | + options: optionsMock, |
| 116 | + isFirstRun: true, |
| 117 | + startTime: 10, |
| 118 | + prevTimestamps: { |
| 119 | + '/test/changed.scss': 5, |
| 120 | + '/test/removed.scss': 5, |
| 121 | + '/test/changed.js': 5 |
| 122 | + } |
| 123 | + }; |
| 124 | + }); |
| 125 | + it('returns changed style files', function () { |
| 126 | + var fileTimestamps = { |
| 127 | + '/test/changed.scss': 20, |
| 128 | + '/test/changed.js': 20, |
| 129 | + '/test/newly-created.scss': 15 |
| 130 | + }; |
| 131 | + |
| 132 | + expect( |
| 133 | + LintDirtyModulesPluginCloned.prototype.getChangedFiles.call(pluginMock, fileTimestamps, glob)).to.eql([ |
| 134 | + '/test/changed.scss', |
| 135 | + '/test/newly-created.scss' |
| 136 | + ] |
| 137 | + ); |
| 138 | + }); |
| 139 | + }); |
| 140 | + }); |
| 141 | +}); |
0 commit comments