Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 4db334f

Browse files
move getFlowFromConfig into flow for testability
Also add tests
1 parent 0c8b687 commit 4db334f

File tree

3 files changed

+101
-22
lines changed

3 files changed

+101
-22
lines changed

lib/flow.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,27 @@ Flow.prototype.setPost = function (post) {
6262
Flow.prototype.blockTypes = function () {
6363
return _.union(_.keys(this._steps), _.keys(this._post));
6464
};
65+
66+
// Default flow configuration for a target
67+
Flow.defaultConfig = {
68+
steps: {
69+
js: ['concat', 'uglify'],
70+
css: ['concat', 'cssmin']
71+
},
72+
post: {}
73+
}
74+
75+
// Retrieve the flow config from the furnished configuration. It can be:
76+
// - a dedicated one for the furnished target
77+
// - a general one
78+
// - the default one
79+
Flow.getFlowFromConfig = function (config, target) {
80+
var flow = new Flow(Flow.defaultConfig);
81+
82+
if (config.options && config.options.flow) {
83+
var flowConfig = config.options.flow[target] ? config.options.flow[target] : config.options.flow;
84+
flow.mergeSteps(flowConfig.steps);
85+
flow.setPost(flowConfig.post);
86+
}
87+
return flow;
88+
};

tasks/usemin.js

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,7 @@
22
var util = require('util');
33
var chalk = require('chalk');
44

5-
// Retrieve the flow config from the furnished configuration. It can be:
6-
// - a dedicated one for the furnished target
7-
// - a general one
8-
// - the default one
9-
var getFlowFromConfig = function (config, target) {
10-
var Flow = require('../lib/flow');
11-
var flow = new Flow({
12-
steps: {
13-
js: ['concat', 'uglify'],
14-
css: ['concat', 'cssmin']
15-
},
16-
post: {}
17-
});
18-
19-
if (config.options && config.options.flow) {
20-
var flowConfig = config.options.flow[target] ? config.options.flow[target] : config.options.flow;
21-
flow.mergeSteps(flowConfig.steps);
22-
flow.setPost(flowConfig.post);
23-
}
24-
return flow;
25-
};
5+
var Flow = require('../lib/flow');
266

277
//
288
// Return which locator to use to get the revisioned version (revved) of the files, with, by order of
@@ -169,7 +149,7 @@ module.exports = function (grunt) {
169149
.writeln('Going through ' + grunt.log.wordlist(this.filesSrc) + ' to update the config')
170150
.writeln('Looking for build script HTML comment blocks');
171151

172-
var flow = getFlowFromConfig(grunt.config('useminPrepare'), this.target);
152+
var flow = Flow.getFlowFromConfig(grunt.config('useminPrepare'), this.target);
173153

174154
var c = new ConfigWriter(flow, {
175155
root: root,

test/test-flow.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,78 @@ describe('ConfigWriter', function () {
134134
});
135135

136136
});
137+
138+
describe('Flow builder from configuration', function () {
139+
describe('global flow', function () {
140+
it('should enable removing a step for a type', function () {
141+
var flow = Flow.getFlowFromConfig({
142+
html: 'index.html',
143+
options: {
144+
flow: {
145+
steps: {
146+
js: ['uglify']
147+
}
148+
}
149+
}
150+
});
151+
152+
assert.deepEqual(flow.steps('js'), ['uglify']);
153+
assert.deepEqual(flow.steps('css'), Flow.defaultConfig.steps.css);
154+
});
155+
156+
it('should enable clearing all steps for a type', function () {
157+
var flow = Flow.getFlowFromConfig({
158+
html: 'index.html',
159+
options: {
160+
flow: {
161+
steps: {
162+
css: []
163+
}
164+
}
165+
}
166+
});
167+
168+
assert.deepEqual(flow.steps('js'), Flow.defaultConfig.steps.js);
169+
assert.deepEqual(flow.steps('css'), []);
170+
});
171+
});
172+
173+
describe('flow per target', function () {
174+
it('should enable removing a step for a type', function () {
175+
var flow = Flow.getFlowFromConfig({
176+
html: 'index.html',
177+
options: {
178+
flow: {
179+
html: {
180+
steps: {
181+
js: ['uglify']
182+
}
183+
}
184+
}
185+
}
186+
}, 'html');
187+
188+
assert.deepEqual(flow.steps('js'), ['uglify']);
189+
assert.deepEqual(flow.steps('css'), Flow.defaultConfig.steps.css);
190+
});
191+
192+
it('should enable clearing all steps for a type', function () {
193+
var flow = Flow.getFlowFromConfig({
194+
html: 'index.html',
195+
options: {
196+
flow: {
197+
html: {
198+
steps: {
199+
js: ['uglify'],
200+
css: []
201+
}
202+
}
203+
}
204+
}
205+
}, 'html');
206+
207+
assert.deepEqual(flow.steps('js'), ['uglify']);
208+
assert.deepEqual(flow.steps('css'), []);
209+
});
210+
});
211+
});

0 commit comments

Comments
 (0)