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