From 8015a79f1511c36bf243f0a5fe94106537331d8c Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 16:04:08 +0100 Subject: [PATCH 01/11] Made module and compliant --- .gitignore | 1 + lib/ng-json2js.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0f27ec2..6e9bd3a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /node_modules /libpeerconnection.log +.idea diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index 6988ee2..6564033 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -2,7 +2,19 @@ module.exports = (function () { 'use strict'; var util = require('util'), - TEMPLATE = 'angular.module(\'%s\', []).constant(\'%s\', %s);\n'; + + TEMPLATE = + '\'use strict\'\n' + + 'function %sFunction(angular) {\n' + + 'angular.module(\'%s\', []).constant(\'%s\', %s);\n' + + '}\n' + + 'if (typeof define === \'function\' && define.amd) {\n' + + 'define([\'angular\'], function (angular) {\n' + + '%sFunction(angular);' + + '});\n' + + '} else {\n' + + '%sFunction(angular);\n' + + '}'; function createNgJson2jsPreprocessor(logger, basePath, config) { config = config || {}; @@ -26,7 +38,7 @@ module.exports = (function () { }); file.path = file.path + '.js'; - done(util.format(TEMPLATE, jsonPath, componentName, content)); + done(util.format(TEMPLATE, componentName, jsonPath, componentName, content, componentName, componentName)); }; } From 6202ad63a0f6521e64fc6cd82b466590ae246475 Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 16:15:46 +0100 Subject: [PATCH 02/11] Added spaces for readability --- lib/ng-json2js.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index 6564033..8189e93 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -6,14 +6,14 @@ module.exports = (function () { TEMPLATE = '\'use strict\'\n' + 'function %sFunction(angular) {\n' + - 'angular.module(\'%s\', []).constant(\'%s\', %s);\n' + + ' angular.module(\'%s\', []).constant(\'%s\', %s);\n' + '}\n' + 'if (typeof define === \'function\' && define.amd) {\n' + - 'define([\'angular\'], function (angular) {\n' + - '%sFunction(angular);' + - '});\n' + + ' define([\'angular\'], function (angular) {\n' + + ' %sFunction(angular);' + + ' });\n' + '} else {\n' + - '%sFunction(angular);\n' + + ' %sFunction(angular);\n' + '}'; function createNgJson2jsPreprocessor(logger, basePath, config) { From 6b9f168d614f9510e707a33f9c4b03e9d840d2a2 Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 17:32:45 +0100 Subject: [PATCH 03/11] Added spec to test requires setup --- Gruntfile.js | 7 ++- karma.conf.js | 2 +- karma.requirejs.conf.js | 71 +++++++++++++++++++++++++++++++ package.json | 2 + test/main-test.js | 31 ++++++++++++++ test/ng-json2js.requirejs.spec.js | 56 ++++++++++++++++++++++++ 6 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 karma.requirejs.conf.js create mode 100644 test/main-test.js create mode 100644 test/ng-json2js.requirejs.spec.js diff --git a/Gruntfile.js b/Gruntfile.js index df7c44f..3e3ad8e 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -5,7 +5,7 @@ module.exports = function (grunt) { grunt.initConfig({ 'ddescribe-iit': { - files: ['test/ng-json2js.spec.js'], + files: ['test/*.spec.js'], }, eslint: { @@ -45,6 +45,10 @@ module.exports = function (grunt) { configFile: 'karma.conf.js', singleRun: true, }, + requirejs: { + configFile: 'karma.requirejs.conf.js', + singleRun: true, + }, }, 'merge-conflict': { @@ -73,6 +77,7 @@ module.exports = function (grunt) { 'lint', 'commitChecks', 'karma:unit', + 'karma:requirejs', ]); grunt.registerTask('default', ['test']); diff --git a/karma.conf.js b/karma.conf.js index dc52353..76745b8 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -33,7 +33,7 @@ module.exports = function (config) { reporters: ['progress'], // web server port - port: 8080, + port: 9876, // cli runner port runnerPort: 9100, diff --git a/karma.requirejs.conf.js b/karma.requirejs.conf.js new file mode 100644 index 0000000..3443270 --- /dev/null +++ b/karma.requirejs.conf.js @@ -0,0 +1,71 @@ +'use strict'; + +module.exports = function (config) { + var preprocessors = config.preprocessors; + // put JSON data into a mock + preprocessors['**/*.json'] = 'ng-json2js'; + + config.set({ + frameworks: ['jasmine', 'requirejs'], + + // base path, that will be used to resolve files and exclude + basePath: '', + + // list of files / patterns to load in the browser + files: [ + 'test/main-test.js', + {pattern: 'test/vendor/angular.js', included: false}, + {pattern: 'test/vendor/angular-mocks.js', included: false}, + {pattern: 'test/fixtures/*.json', included: false}, + {pattern: 'test/ng-json2js.requirejs.spec.js', included: false}, + {pattern: 'karma.requirejs.conf.js', included: false, served: false}, + ], + + // list of files to exclude + exclude: [], + + preprocessors: preprocessors, + + plugins: config.plugins.concat([ + require('./lib/index.js'), + 'karma-requirejs', + ]), + + // test results reporter to use + // possible values: dots || progress || growl + reporters: ['progress'], + + // web server port + port: 9876, + + // cli runner port + runnerPort: 9100, + + // enable / disable colors in the output (reporters and logs) + colors: true, + + // level of logging + // possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG + logLevel: config.LOG_INFO, + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + // Start these browsers, currently available: + // - Chrome + // - ChromeCanary + // - Firefox + // - Opera + // - Safari (only Mac) + // - PhantomJS + // - IE (only Windows) + browsers: ['PhantomJS'], + + // If browser does not capture in given timeout [ms], kill it + captureTimeout: 5000, + + // Continuous Integration mode + // if true, it capture browsers, run tests and exit + singleRun: false, + }); +}; diff --git a/package.json b/package.json index ea67122..27eb708 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "karma-preprocessor", "angularjs", "angular", + "requirejs", "json2js" ], "author": { @@ -35,6 +36,7 @@ "karma": "0.12.24", "karma-jasmine": "0.1.5", "karma-phantomjs-launcher": "0.1.4", + "karma-requirejs": "^0.2.2", "load-grunt-tasks": "0.6.0", "time-grunt": "1.0.0" }, diff --git a/test/main-test.js b/test/main-test.js new file mode 100644 index 0000000..589a1ae --- /dev/null +++ b/test/main-test.js @@ -0,0 +1,31 @@ +/* global window */ + +var file, testFiles = []; + +for (file in window.__karma__.files) { + if (/spec.*\.js$/.test(file)) { + testFiles.push(file); + } + if (/.*\.json\.js$/.test(file)) { + testFiles.push(file); + } +} + +require({ + baseUrl: 'http://localhost:9876/base/', + paths:{ + angular: 'test/vendor/angular', + angularmocks: 'test/vendor/angular-mocks', + }, + shim: { + angular: { + exports: 'angular', + }, + angularmocks: { + deps: ['angular'], + }, + }, + deps: testFiles, + + callback: window.__karma__.start, +}); diff --git a/test/ng-json2js.requirejs.spec.js b/test/ng-json2js.requirejs.spec.js new file mode 100644 index 0000000..c8f9c32 --- /dev/null +++ b/test/ng-json2js.requirejs.spec.js @@ -0,0 +1,56 @@ +/* global define */ + +'use strict'; + +define(['angular', 'angularmocks'], function (angular) { + describe('json2j preprocessor', function () { + + beforeEach(module('test/fixtures/empty.json')); + beforeEach(module('test/fixtures/complex.json')); + + it('should work on an empty object', function () { + var testFixturesEmpty; + inject(function (_testFixturesEmpty_) { + testFixturesEmpty = _testFixturesEmpty_; + }); + + expect(testFixturesEmpty).toEqual({}); + }); + + var checkComplexObject = function (testFixturesComplex) { + expect(testFixturesComplex).toEqual({ + field: 'property', + subObject: [ + 'arrayElem1', + 'arrayElem2', + ], + anotherSubObject: { + subSubObject: { + field: 'property', + }, + }, + }); + }; + + it('should work on a complex object', function () { + var testFixturesComplex; + inject(function (_testFixturesComplex_) { + testFixturesComplex = _testFixturesComplex_; + }); + + checkComplexObject(testFixturesComplex); + }); + + it('should allow accessing the json during configuration phase', function () { + var injectedDuringConfig; + angular.module('testModule', ['test/fixtures/complex.json']).config(function (_testFixturesComplex_) { + injectedDuringConfig = _testFixturesComplex_; + }); + + inject(module('testModule')); + + checkComplexObject(injectedDuringConfig); + }); + }); + +}); From dba1523ceee495e36820d4eaec2e934f3cea557c Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 18:05:28 +0100 Subject: [PATCH 04/11] Added missing ";" --- lib/ng-json2js.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index 8189e93..736815a 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -4,8 +4,8 @@ module.exports = (function () { var util = require('util'), TEMPLATE = - '\'use strict\'\n' + - 'function %sFunction(angular) {\n' + + '\'use strict\';\n' + + 'function %sFunction (angular) {\n' + ' angular.module(\'%s\', []).constant(\'%s\', %s);\n' + '}\n' + 'if (typeof define === \'function\' && define.amd) {\n' + From 3bac37e2cf3dee852cdb49e8eedc57bdf71cda6e Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 18:33:25 +0100 Subject: [PATCH 05/11] Removed unneeded plugin from plugins list (will be loaded automatically by karma) --- karma.requirejs.conf.js | 1 - 1 file changed, 1 deletion(-) diff --git a/karma.requirejs.conf.js b/karma.requirejs.conf.js index 3443270..54b752f 100644 --- a/karma.requirejs.conf.js +++ b/karma.requirejs.conf.js @@ -28,7 +28,6 @@ module.exports = function (config) { plugins: config.plugins.concat([ require('./lib/index.js'), - 'karma-requirejs', ]), // test results reporter to use From b2cb0347ac9a1a631672c312b978e10f755c881b Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 18:39:41 +0100 Subject: [PATCH 06/11] Added missing new lines --- lib/ng-json2js.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index 736815a..bfaca8c 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -10,11 +10,11 @@ module.exports = (function () { '}\n' + 'if (typeof define === \'function\' && define.amd) {\n' + ' define([\'angular\'], function (angular) {\n' + - ' %sFunction(angular);' + + ' %sFunction(angular);\n' + ' });\n' + '} else {\n' + ' %sFunction(angular);\n' + - '}'; + '}\n'; function createNgJson2jsPreprocessor(logger, basePath, config) { config = config || {}; From 06b96fc797c94962bf28e0f639ef95c53f8b869d Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 18:41:07 +0100 Subject: [PATCH 07/11] Small code beautification --- lib/ng-json2js.js | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index bfaca8c..15526b6 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -2,19 +2,18 @@ module.exports = (function () { 'use strict'; var util = require('util'), - - TEMPLATE = - '\'use strict\';\n' + - 'function %sFunction (angular) {\n' + - ' angular.module(\'%s\', []).constant(\'%s\', %s);\n' + - '}\n' + - 'if (typeof define === \'function\' && define.amd) {\n' + - ' define([\'angular\'], function (angular) {\n' + - ' %sFunction(angular);\n' + - ' });\n' + - '} else {\n' + - ' %sFunction(angular);\n' + - '}\n'; + TEMPLATE = + '\'use strict\';\n' + + 'function %sFunction (angular) {\n' + + ' angular.module(\'%s\', []).constant(\'%s\', %s);\n' + + '}\n' + + 'if (typeof define === \'function\' && define.amd) {\n' + + ' define([\'angular\'], function (angular) {\n' + + ' %sFunction(angular);\n' + + ' });\n' + + '} else {\n' + + ' %sFunction(angular);\n' + + '}\n'; function createNgJson2jsPreprocessor(logger, basePath, config) { config = config || {}; From 47de4f5c7a8dfcace902b216ec484853b422ef72 Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 20:29:05 +0100 Subject: [PATCH 08/11] Updated README.md --- README.md | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 88aa56f..4da745b 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,21 @@ For instance this `test/fixture/data.json` ... } ``` ... with the configuration given above will be converted into: -```js -angular.module('served/data.json', []).constant('servedData', { - prop: 'val' -}); -``` + + 'use strict' + function servedDataFunction (angular) { + angular.module('served/data.json', []).constant('servedData', { + prop: 'val' + }); + } + if (typeof define === 'function' && define.amd) { + define(['angular'], function (angular) { + servedDataFunction(angular); + }); + } else { + servedDataFunction(angular); + } + Inject json fixture into your test case: ```js describe('me', function(){ From 38d3f2edc36e5483621474b9e8d7eab12e267adf Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 20:31:19 +0100 Subject: [PATCH 09/11] Use better github formatting --- README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4da745b..1262370 100644 --- a/README.md +++ b/README.md @@ -55,21 +55,21 @@ For instance this `test/fixture/data.json` ... } ``` ... with the configuration given above will be converted into: - - 'use strict' - function servedDataFunction (angular) { - angular.module('served/data.json', []).constant('servedData', { - prop: 'val' - }); - } - if (typeof define === 'function' && define.amd) { - define(['angular'], function (angular) { - servedDataFunction(angular); - }); - } else { - servedDataFunction(angular); - } - +```js +'use strict' +function servedDataFunction (angular) { + angular.module('served/data.json', []).constant('servedData', { + prop: 'val' + }); +} +if (typeof define === 'function' && define.amd) { + define(['angular'], function (angular) { + servedDataFunction(angular); + }); +} else { + servedDataFunction(angular); +} +``` Inject json fixture into your test case: ```js describe('me', function(){ From 8778a36ba7c431ea8a45a2d5bc51e048992af61e Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 20:50:11 +0100 Subject: [PATCH 10/11] Use require instead of define (no module creation, just loading angular module) --- README.md | 2 +- lib/ng-json2js.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1262370..9099507 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ function servedDataFunction (angular) { }); } if (typeof define === 'function' && define.amd) { - define(['angular'], function (angular) { + require(['angular'], function (angular) { servedDataFunction(angular); }); } else { diff --git a/lib/ng-json2js.js b/lib/ng-json2js.js index 15526b6..5eb483f 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -8,7 +8,7 @@ module.exports = (function () { ' angular.module(\'%s\', []).constant(\'%s\', %s);\n' + '}\n' + 'if (typeof define === \'function\' && define.amd) {\n' + - ' define([\'angular\'], function (angular) {\n' + + ' require([\'angular\'], function (angular) {\n' + ' %sFunction(angular);\n' + ' });\n' + '} else {\n' + From dc9a7d09b4ffd95efcfd67a07bc160fb00affc69 Mon Sep 17 00:00:00 2001 From: Louis De Bevere Date: Mon, 12 Jan 2015 21:01:19 +0100 Subject: [PATCH 11/11] Bump version to 1.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 27eb708..903cbd3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "karma-ng-json2js-preprocessor", - "version": "1.0.1-pre", + "version": "1.0.1", "description": "A Karma plugin. Compile JSON files to Angular constants on the fly.", "main": "./lib/index.js", "repository": {