forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnpmScripts.spec.js
More file actions
145 lines (133 loc) · 4.18 KB
/
npmScripts.spec.js
File metadata and controls
145 lines (133 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
var rewire = require('rewire');
var optimist = require('optimist');
var path = require('path');
var fs = require('fs');
var npmScripts = rewire('../../lib/utils/npmScripts');
var Q = require('q');
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('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();
});
});
});
});
describe('consolidate options', function() {
var consolidateOptions = npmScripts.__get__('consolidateOptions');
it('should consolidate options to the first array element', function() {
var options = {
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
p: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
};
var results = consolidateOptions(['port', 'p'], options);
expect(results).toEqual({
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
});
});
it('should consolidate duplicates', function() {
var options = {
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
p: 8100,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
};
var results = consolidateOptions(['port', 'p'], options);
expect(results).toEqual({
_: ['run', 'ios'],
consolelogs: false,
c: true,
serverlogs: false,
s: true,
debug: false,
release: false,
l: true,
port: 8100,
r: 6000,
'$0': 'ionic',
v2: true,
runLivereload: true,
isPlatformServe: true
});
});
});
describe('optionsToArray options', function() {
var optionsToArray = npmScripts.__get__('optionsToArray');
it('should convert argv options into an array', function() {
var processArguments = ['node', 'ionic', 'run', 'ios', '--port', '8100', '--livereload'];
var rawCliArguments = processArguments.slice(2);
var argv = optimist(rawCliArguments).argv;
var results = optionsToArray(argv);
expect(results).toEqual([
'--port', 8100,
'--livereload',
]);
});
});