Skip to content

Commit

Permalink
chore(): create tests for npmscripts util.
Browse files Browse the repository at this point in the history
  • Loading branch information
jthoms1 authored Oct 21, 2016
1 parent 5544cc8 commit 9c128e0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/utils/npmScripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ var fs = require('fs');
var IonicAppLib = require('ionic-app-lib');
var log = IonicAppLib.logging.logger;

function getScriptName(name) {
return 'ionic:' + name;
}


function hasIonicScript(name) {
return getPackageJsonContents().then(function(packageJsonContents) {
return packageJsonContents.hasOwnProperty('scripts') &&
packageJsonContents.scripts.hasOwnProperty('ionic:' + name);
packageJsonContents.scripts.hasOwnProperty(getScriptName(name));
});
}

function runIonicScript(name, argv) {
var spawn = require('cross-spawn-async');
var scriptName = 'ionic:' + name;
var scriptName = getScriptName(name);
var q = Q.defer();

var scriptSpawn = spawn('npm', ['run', scriptName].concat(argv || []), { stdio: 'inherit' })
Expand Down Expand Up @@ -48,7 +52,7 @@ var getPackageJsonContents = (function() {
var q = Q.defer();

if (packageJson) {
Q.resolve(packageJson);
return Q.resolve(packageJson);
}

try {
Expand Down
53 changes: 53 additions & 0 deletions spec/fixtures/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"name": "ionic-hello-world",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"scripts": {
"ionic:build": "ionic-app-scripts build",
"watch": "ionic-app-scripts watch",
"ionic:test": "dir",
"serve:before": "watch",
"emulate:before": "build",
"deploy:before": "build",
"build:before": "build",
"run:before": "build"
},
"dependencies": {
"@angular/common": "^2.0.0",
"@angular/compiler": "^2.0.0",
"@angular/compiler-cli": "0.6.2",
"@angular/core": "^2.0.0",
"@angular/forms": "^2.0.0",
"@angular/http": "^2.0.0",
"@angular/platform-browser": "^2.0.0",
"@angular/platform-browser-dynamic": "^2.0.0",
"@angular/platform-server": "^2.0.0",
"@ionic/storage": "^1.0.3",
"ionic-angular": "^2.0.0-rc.1",
"ionic-native": "^2.2.3",
"ionicons": "^3.0.0",
"rxjs": "5.0.0-beta.12",
"zone.js": "^0.6.21"
},
"devDependencies": {
"@ionic/app-scripts": "^0.0.36",
"typescript": "^2.0.3"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-statusbar",
"cordova-plugin-console",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": [
"ios",
{
"platform": "ios",
"version": "",
"locator": "ios"
}
],
"description": "hi: An Ionic project"
}
73 changes: 73 additions & 0 deletions spec/utils/npmScripts.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';

var rewire = require('rewire');
var path = require('path');
var fs = require('fs');
var npmScripts = rewire('../../lib/utils/npmScripts');
var EventEmitter = require('events');
var Q = require('q');
var spawn = require('cross-spawn-async');

describe('hasIonicScript function', function() {

it('should return false if script does not exist', function(done) {
var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json'));
var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent));
var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy);
spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent));

npmScripts.hasIonicScript('stuff').then(function(results) {
expect(results).toEqual(false);
done();
revert();
});
});
it('should return true if script does not exist', function(done) {
var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json'));
var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent));
var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy);
spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent));

npmScripts.hasIonicScript('build').then(function(results) {
expect(results).toEqual(true);
done();
revert();
});
});
});
/*
describe('runIonicScript function', function() {
it('should call spawn', function(done) {
//'npm', ['run', scriptName].concat(argv || []), { stdio: 'inherit' }
var emitter = new EventEmitter();
var error = new Error();
spawn = jasmine.createSpy('spawnSpy', spawn).andCallFake(function() {
return emitter;
});
npmScripts.runIonicScript('test').catch(function(err) {
expect(err).toEqual(error);
done();
});
emitter.emit('error', error);
});
});
*/
describe('getPackageJsonContents method', function() {
it('getPackageJsonContents should return json contents of package.json file and should memoize', function(done) {
var dapath = path.join(__dirname, '../', 'fixtures/package.json');
spyOn(path, 'resolve').andReturn(dapath);
spyOn(fs, 'readFile').andCallThrough();

npmScripts.getPackageJsonContents().then(function(contents) {
expect(contents).toEqual(require(dapath));

npmScripts.getPackageJsonContents().then(function(secondContents) {
expect(secondContents).toEqual(require(dapath));
expect(fs.readFile.calls.length).toEqual(1);
done();
});
});
});
});

0 comments on commit 9c128e0

Please sign in to comment.