Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

/node_modules
/libpeerconnection.log
.idea
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function (grunt) {

grunt.initConfig({
'ddescribe-iit': {
files: ['test/ng-json2js.spec.js'],
files: ['test/*.spec.js'],
},

eslint: {
Expand Down Expand Up @@ -45,6 +45,10 @@ module.exports = function (grunt) {
configFile: 'karma.conf.js',
singleRun: true,
},
requirejs: {
configFile: 'karma.requirejs.conf.js',
singleRun: true,
},
},

'merge-conflict': {
Expand Down Expand Up @@ -73,6 +77,7 @@ module.exports = function (grunt) {
'lint',
'commitChecks',
'karma:unit',
'karma:requirejs',
]);

grunt.registerTask('default', ['test']);
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function (config) {
reporters: ['progress'],

// web server port
port: 8080,
port: 9876,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the previous port causing issues?


// cli runner port
runnerPort: 9100,
Expand Down
70 changes: 70 additions & 0 deletions karma.requirejs.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

module.exports = function (config) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of duplication in this file as well. How about creating karma-shared.conf.js that will have options shared by both configs and then requiring it here and just extending by proper options? See e.g. how Angular does it:
https://github.com/angular/angular.js/blob/master/karma-jquery.conf.js
https://github.com/angular/angular.js/blob/master/karma-shared.conf.js
etc.

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,
});
};
15 changes: 13 additions & 2 deletions lib/ng-json2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand All @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting unreadable with this many parameters... How about turning TEMPLATE into a function and then just invoke it here with correct parameters? the function should be invoked in the following way:

template({
    jsonPath: jsonPath,
    componentName: componentName,
    content: content,
});

};
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "karma-ng-json2js-preprocessor",
"version": "1.0.1-pre",
"version": "1.0.1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't modify this.

"description": "A Karma plugin. Compile JSON files to Angular constants on the fly.",
"main": "./lib/index.js",
"repository": {
Expand All @@ -12,6 +12,7 @@
"karma-preprocessor",
"angularjs",
"angular",
"requirejs",
"json2js"
],
"author": {
Expand All @@ -35,6 +36,7 @@
"karma": "0.12.24",
"karma-jasmine": "0.1.5",
"karma-phantomjs-launcher": "0.1.4",
"karma-requirejs": "^0.2.2",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the caret; I prefer to have devDependencies declared strictly.

"load-grunt-tasks": "0.6.0",
"time-grunt": "1.0.0"
},
Expand Down
31 changes: 31 additions & 0 deletions test/main-test.js
Original file line number Diff line number Diff line change
@@ -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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this all could be handled with one regex, sth like: /(?:spec.*\.js|.*\.json\.js)$/.

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,
});
56 changes: 56 additions & 0 deletions test/ng-json2js.requirejs.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

});