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/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/README.md b/README.md index 88aa56f..9099507 100644 --- a/README.md +++ b/README.md @@ -56,9 +56,19 @@ For instance this `test/fixture/data.json` ... ``` ... with the configuration given above will be converted into: ```js -angular.module('served/data.json', []).constant('servedData', { +'use strict' +function servedDataFunction (angular) { + angular.module('served/data.json', []).constant('servedData', { prop: 'val' -}); + }); +} +if (typeof define === 'function' && define.amd) { + require(['angular'], function (angular) { + servedDataFunction(angular); + }); +} else { + servedDataFunction(angular); +} ``` Inject json fixture into your test case: ```js 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..54b752f --- /dev/null +++ b/karma.requirejs.conf.js @@ -0,0 +1,70 @@ +'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'), + ]), + + // 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/lib/ng-json2js.js b/lib/ng-json2js.js index 6988ee2..5eb483f 100644 --- a/lib/ng-json2js.js +++ b/lib/ng-json2js.js @@ -2,7 +2,18 @@ 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' + + ' require([\'angular\'], function (angular) {\n' + + ' %sFunction(angular);\n' + + ' });\n' + + '} else {\n' + + ' %sFunction(angular);\n' + + '}\n'; function createNgJson2jsPreprocessor(logger, basePath, config) { config = config || {}; @@ -26,7 +37,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)); }; } diff --git a/package.json b/package.json index ea67122..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": { @@ -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); + }); + }); + +});