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

Commit

Permalink
move getFlowFromConfig into flow for testability
Browse files Browse the repository at this point in the history
Also add tests
  • Loading branch information
stephanebachelier committed Apr 15, 2015
1 parent 51c2455 commit 234510d
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 22 deletions.
25 changes: 25 additions & 0 deletions lib/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,28 @@ Flow.prototype.setPost = function (post) {
Flow.prototype.blockTypes = function () {
return _.union(_.keys(this._steps), _.keys(this._post));
};

// Default flow configuration for a target
Flow.defaultConfig = {
steps: {
js: ['concat', 'uglify'],
css: ['concat', 'cssmin']
},
post: {}
};

// Retrieve the flow config from the furnished configuration. It can be:
// - a dedicated one for the furnished target
// - a general one
// - the default one
Flow.getFlowFromConfig = function (config, target) {
var flow = new Flow(Flow.defaultConfig);

if (config.options && config.options.flow) {
var flowConfig = config.options.flow[target] ? config.options.flow[target] : config.options.flow;
flow.mergeSteps(flowConfig.steps);
flow.setPost(flowConfig.post);
}

return flow;
};
24 changes: 2 additions & 22 deletions tasks/usemin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,7 @@
var util = require('util');
var chalk = require('chalk');

// Retrieve the flow config from the furnished configuration. It can be:
// - a dedicated one for the furnished target
// - a general one
// - the default one
var getFlowFromConfig = function (config, target) {
var Flow = require('../lib/flow');
var flow = new Flow({
steps: {
js: ['concat', 'uglify'],
css: ['concat', 'cssmin']
},
post: {}
});

if (config.options && config.options.flow) {
var flowConfig = config.options.flow[target] ? config.options.flow[target] : config.options.flow;
flow.mergeSteps(flowConfig.steps);
flow.setPost(flowConfig.post);
}
return flow;
};
var Flow = require('../lib/flow');

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

var flow = getFlowFromConfig(grunt.config('useminPrepare'), this.target);
var flow = Flow.getFlowFromConfig(grunt.config('useminPrepare'), this.target);

var c = new ConfigWriter(flow, {
root: root,
Expand Down
75 changes: 75 additions & 0 deletions test/test-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,78 @@ describe('ConfigWriter', function () {
});

});

describe('Flow builder from configuration', function () {
describe('global flow', function () {
it('should enable removing a step for a type', function () {
var flow = Flow.getFlowFromConfig({
html: 'index.html',
options: {
flow: {
steps: {
js: ['uglify']
}
}
}
});

assert.deepEqual(flow.steps('js'), ['uglify']);
assert.deepEqual(flow.steps('css'), Flow.defaultConfig.steps.css);
});

it('should enable clearing all steps for a type', function () {
var flow = Flow.getFlowFromConfig({
html: 'index.html',
options: {
flow: {
steps: {
css: []
}
}
}
});

assert.deepEqual(flow.steps('js'), Flow.defaultConfig.steps.js);
assert.deepEqual(flow.steps('css'), []);
});
});

describe('flow per target', function () {
it('should enable removing a step for a type', function () {
var flow = Flow.getFlowFromConfig({
html: 'index.html',
options: {
flow: {
html: {
steps: {
js: ['uglify']
}
}
}
}
}, 'html');

assert.deepEqual(flow.steps('js'), ['uglify']);
assert.deepEqual(flow.steps('css'), Flow.defaultConfig.steps.css);
});

it('should enable clearing all steps for a type', function () {
var flow = Flow.getFlowFromConfig({
html: 'index.html',
options: {
flow: {
html: {
steps: {
js: ['uglify'],
css: []
}
}
}
}
}, 'html');

assert.deepEqual(flow.steps('js'), ['uglify']);
assert.deepEqual(flow.steps('css'), []);
});
});
});

0 comments on commit 234510d

Please sign in to comment.