Skip to content

Commit 9c128e0

Browse files
authored
chore(): create tests for npmscripts util.
1 parent 5544cc8 commit 9c128e0

File tree

3 files changed

+133
-3
lines changed

3 files changed

+133
-3
lines changed

lib/utils/npmScripts.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ var fs = require('fs');
66
var IonicAppLib = require('ionic-app-lib');
77
var log = IonicAppLib.logging.logger;
88

9+
function getScriptName(name) {
10+
return 'ionic:' + name;
11+
}
12+
913

1014
function hasIonicScript(name) {
1115
return getPackageJsonContents().then(function(packageJsonContents) {
1216
return packageJsonContents.hasOwnProperty('scripts') &&
13-
packageJsonContents.scripts.hasOwnProperty('ionic:' + name);
17+
packageJsonContents.scripts.hasOwnProperty(getScriptName(name));
1418
});
1519
}
1620

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

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

5054
if (packageJson) {
51-
Q.resolve(packageJson);
55+
return Q.resolve(packageJson);
5256
}
5357

5458
try {

spec/fixtures/package.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "ionic-hello-world",
3+
"author": "Ionic Framework",
4+
"homepage": "http://ionicframework.com/",
5+
"scripts": {
6+
"ionic:build": "ionic-app-scripts build",
7+
"watch": "ionic-app-scripts watch",
8+
"ionic:test": "dir",
9+
"serve:before": "watch",
10+
"emulate:before": "build",
11+
"deploy:before": "build",
12+
"build:before": "build",
13+
"run:before": "build"
14+
},
15+
"dependencies": {
16+
"@angular/common": "^2.0.0",
17+
"@angular/compiler": "^2.0.0",
18+
"@angular/compiler-cli": "0.6.2",
19+
"@angular/core": "^2.0.0",
20+
"@angular/forms": "^2.0.0",
21+
"@angular/http": "^2.0.0",
22+
"@angular/platform-browser": "^2.0.0",
23+
"@angular/platform-browser-dynamic": "^2.0.0",
24+
"@angular/platform-server": "^2.0.0",
25+
"@ionic/storage": "^1.0.3",
26+
"ionic-angular": "^2.0.0-rc.1",
27+
"ionic-native": "^2.2.3",
28+
"ionicons": "^3.0.0",
29+
"rxjs": "5.0.0-beta.12",
30+
"zone.js": "^0.6.21"
31+
},
32+
"devDependencies": {
33+
"@ionic/app-scripts": "^0.0.36",
34+
"typescript": "^2.0.3"
35+
},
36+
"cordovaPlugins": [
37+
"cordova-plugin-whitelist",
38+
"cordova-plugin-statusbar",
39+
"cordova-plugin-console",
40+
"cordova-plugin-device",
41+
"cordova-plugin-splashscreen",
42+
"ionic-plugin-keyboard"
43+
],
44+
"cordovaPlatforms": [
45+
"ios",
46+
{
47+
"platform": "ios",
48+
"version": "",
49+
"locator": "ios"
50+
}
51+
],
52+
"description": "hi: An Ionic project"
53+
}

spec/utils/npmScripts.spec.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
var rewire = require('rewire');
4+
var path = require('path');
5+
var fs = require('fs');
6+
var npmScripts = rewire('../../lib/utils/npmScripts');
7+
var EventEmitter = require('events');
8+
var Q = require('q');
9+
var spawn = require('cross-spawn-async');
10+
11+
describe('hasIonicScript function', function() {
12+
13+
it('should return false if script does not exist', function(done) {
14+
var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json'));
15+
var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent));
16+
var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy);
17+
spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent));
18+
19+
npmScripts.hasIonicScript('stuff').then(function(results) {
20+
expect(results).toEqual(false);
21+
done();
22+
revert();
23+
});
24+
});
25+
it('should return true if script does not exist', function(done) {
26+
var jsonContent = require(path.join(__dirname, '../', 'fixtures/', 'package.json'));
27+
var getPackageJsonContentsSpy = jasmine.createSpy('getPackageJsonContentsSpy').andReturn(Q(jsonContent));
28+
var revert = npmScripts.__set__('getPackageJsonContents', getPackageJsonContentsSpy);
29+
spyOn(npmScripts, 'getPackageJsonContents').andReturn(Q(jsonContent));
30+
31+
npmScripts.hasIonicScript('build').then(function(results) {
32+
expect(results).toEqual(true);
33+
done();
34+
revert();
35+
});
36+
});
37+
});
38+
/*
39+
describe('runIonicScript function', function() {
40+
it('should call spawn', function(done) {
41+
//'npm', ['run', scriptName].concat(argv || []), { stdio: 'inherit' }
42+
var emitter = new EventEmitter();
43+
var error = new Error();
44+
45+
spawn = jasmine.createSpy('spawnSpy', spawn).andCallFake(function() {
46+
return emitter;
47+
});
48+
49+
npmScripts.runIonicScript('test').catch(function(err) {
50+
expect(err).toEqual(error);
51+
done();
52+
});
53+
emitter.emit('error', error);
54+
});
55+
});
56+
*/
57+
describe('getPackageJsonContents method', function() {
58+
it('getPackageJsonContents should return json contents of package.json file and should memoize', function(done) {
59+
var dapath = path.join(__dirname, '../', 'fixtures/package.json');
60+
spyOn(path, 'resolve').andReturn(dapath);
61+
spyOn(fs, 'readFile').andCallThrough();
62+
63+
npmScripts.getPackageJsonContents().then(function(contents) {
64+
expect(contents).toEqual(require(dapath));
65+
66+
npmScripts.getPackageJsonContents().then(function(secondContents) {
67+
expect(secondContents).toEqual(require(dapath));
68+
expect(fs.readFile.calls.length).toEqual(1);
69+
done();
70+
});
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)