1
1
'use strict' ;
2
- const expect = require ( 'chai' ) . expect ;
2
+ const { expect} = require ( 'chai' ) ;
3
3
const proxyquire = require ( 'proxyquire' ) . noCallThru ( ) ;
4
4
const sinon = require ( 'sinon' ) ;
5
5
const stripAnsi = require ( 'strip-ansi' ) ;
6
6
7
7
const modulePath = '../../../lib/utils/update-check' ;
8
8
9
9
describe ( 'Unit: Utils > update-check' , function ( ) {
10
- it ( 'rejects error if updateNotifier has an error' , function ( done ) {
10
+ it ( 'rejects error if latestVersion has an error' , function ( done ) {
11
11
const pkg = { name : 'ghost' , version : '1.0.0' } ;
12
12
const testError = new Error ( 'update check' ) ;
13
- const updateNotifer = sinon . stub ( ) . callsFake ( ( options ) => {
14
- expect ( options ) . to . exist ;
15
- expect ( options . pkg ) . to . deep . equal ( pkg ) ;
16
- expect ( options . callback ) . to . be . a ( 'function' ) ;
17
- options . callback ( testError , null ) ;
18
- } ) ;
13
+ const latestVersion = sinon . stub ( ) . rejects ( testError ) ;
19
14
20
15
const updateCheck = proxyquire ( modulePath , {
21
16
'../../package.json' : pkg ,
22
- 'update-notifier ' : updateNotifer
17
+ 'latest-version ' : latestVersion
23
18
} ) ;
24
19
25
20
updateCheck ( ) . catch ( ( err ) => {
26
21
expect ( err . message ) . to . equal ( testError . message ) ;
22
+ expect ( latestVersion . calledOnce ) . to . be . true ;
23
+ expect ( latestVersion . calledWithExactly ( 'ghost' ) ) . to . be . true ;
27
24
done ( ) ;
28
25
} ) ;
29
26
} ) ;
30
27
31
- it ( 'resolves immediately if there are no updates' , function ( ) {
28
+ it ( 'doesn\'t do anything if there are no updates' , function ( ) {
32
29
const pkg = { name : 'ghost' , version : '1.0.0' } ;
33
- const updateNotifer = sinon . stub ( ) . callsFake ( ( options ) => {
34
- expect ( options ) . to . exist ;
35
- expect ( options . pkg ) . to . deep . equal ( pkg ) ;
36
- expect ( options . callback ) . to . be . a ( 'function' ) ;
37
- options . callback ( null , { type : 'latest' } ) ;
38
- } ) ;
30
+ const latestVersion = sinon . stub ( ) . resolves ( '1.0.0' ) ;
39
31
const logStub = sinon . stub ( ) ;
40
32
41
33
const updateCheck = proxyquire ( modulePath , {
42
34
'../../package.json' : pkg ,
43
- 'update-notifier' : updateNotifer
44
- } ) ;
45
-
46
- return updateCheck ( { log : logStub } ) . then ( ( ) => {
47
- expect ( logStub . called ) . to . be . false ;
48
- } ) ;
49
- } ) ;
50
-
51
- it ( 'doesn\'t log a message if the type of update is not major, minor, or patch' , function ( ) {
52
- const pkg = { name : 'ghost' , version : '1.0.0' } ;
53
- const updateNotifer = sinon . stub ( ) . callsFake ( ( options ) => {
54
- expect ( options ) . to . exist ;
55
- expect ( options . pkg ) . to . deep . equal ( pkg ) ;
56
- expect ( options . callback ) . to . be . a ( 'function' ) ;
57
- options . callback ( null , { type : 'prerelease' } ) ;
58
- } ) ;
59
- const logStub = sinon . stub ( ) ;
60
-
61
- const updateCheck = proxyquire ( modulePath , {
62
- '../../package.json' : pkg ,
63
- 'update-notifier' : updateNotifer
35
+ 'latest-version' : latestVersion
64
36
} ) ;
65
37
66
38
return updateCheck ( { log : logStub } ) . then ( ( ) => {
67
39
expect ( logStub . called ) . to . be . false ;
40
+ expect ( latestVersion . calledOnce ) . to . be . true ;
41
+ expect ( latestVersion . calledWithExactly ( 'ghost' ) ) . to . be . true ;
68
42
} ) ;
69
43
} ) ;
70
44
71
45
it ( 'logs a message if an update is available' , function ( ) {
72
46
const pkg = { name : 'ghost' , version : '1.0.0' } ;
73
- const updateNotifer = sinon . stub ( ) . callsFake ( ( options ) => {
74
- expect ( options ) . to . exist ;
75
- expect ( options . pkg ) . to . deep . equal ( pkg ) ;
76
- expect ( options . callback ) . to . be . a ( 'function' ) ;
77
- options . callback ( null , { type : 'minor' } ) ;
78
- } ) ;
47
+ const latestVersion = sinon . stub ( ) . resolves ( '1.1.0' ) ;
79
48
const logStub = sinon . stub ( ) ;
80
49
81
50
const updateCheck = proxyquire ( modulePath , {
82
51
'../../package.json' : pkg ,
83
- 'update-notifier ' : updateNotifer
52
+ 'latest-version ' : latestVersion
84
53
} ) ;
85
54
86
55
return updateCheck ( { log : logStub } ) . then ( ( ) => {
@@ -89,6 +58,9 @@ describe('Unit: Utils > update-check', function () {
89
58
const log = logStub . args [ 0 ] [ 0 ] ;
90
59
91
60
expect ( stripAnsi ( log ) ) . to . match ( / Y o u a r e r u n n i n g a n o u t d a t e d v e r s i o n o f G h o s t - C L I / ) ;
61
+
62
+ expect ( latestVersion . calledOnce ) . to . be . true ;
63
+ expect ( latestVersion . calledWithExactly ( 'ghost' ) ) . to . be . true ;
92
64
} ) ;
93
65
} ) ;
94
66
} ) ;
0 commit comments