From 234510de48495922686ca5f310a4493d6a1c4af4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bachelier?= Date: Tue, 14 Apr 2015 02:15:53 +0200 Subject: [PATCH] move getFlowFromConfig into flow for testability Also add tests --- lib/flow.js | 25 ++++++++++++++++ tasks/usemin.js | 24 ++------------- test/test-flow.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 22 deletions(-) diff --git a/lib/flow.js b/lib/flow.js index a1ccab9..c2c5b2b 100644 --- a/lib/flow.js +++ b/lib/flow.js @@ -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; +}; diff --git a/tasks/usemin.js b/tasks/usemin.js index 483ceb5..09a3b4c 100644 --- a/tasks/usemin.js +++ b/tasks/usemin.js @@ -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 @@ -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, diff --git a/test/test-flow.js b/test/test-flow.js index f6f6ad9..5d8d016 100644 --- a/test/test-flow.js +++ b/test/test-flow.js @@ -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'), []); + }); + }); +});