1
1
const { assert } = require ( 'chai' ) ;
2
2
const mock = require ( 'mock-require' ) ;
3
+ const sinon = require ( 'sinon' ) ;
3
4
const { join } = require ( 'path' ) ;
5
+ const { clone } = require ( 'lodash' ) ;
4
6
5
7
describe ( 'index' , ( ) => {
8
+ let env ;
6
9
let app ;
7
10
let protocol ;
8
11
let onAppReady ;
9
12
let protocolName ;
10
13
let handlerOptions ;
14
+ let handlerStub ;
11
15
12
16
function register ( options ) {
13
17
require ( '../..' ) ( Object . assign ( {
@@ -17,14 +21,37 @@ describe('index', () => {
17
21
} , options ) ) ;
18
22
}
19
23
20
- before ( ( ) => {
21
- mock ( '../../lib/handler' , options => handlerOptions = options ) ;
24
+ //
25
+ // Mock our handler library
26
+ //
27
+ beforeEach ( ( ) => {
28
+ handlerStub = sinon . stub ( ) ;
29
+ handlerStub . callsArgWith ( 1 , { path : '/path/to/index.html' } ) ;
30
+
31
+ mock ( '../../lib/handler' , options => {
32
+ handlerOptions = options ;
33
+ return handlerStub ;
34
+ } ) ;
22
35
} ) ;
23
36
24
- after ( ( ) => {
37
+ afterEach ( ( ) => {
25
38
mock . stop ( '../../lib/handler' ) ;
26
39
} ) ;
27
40
41
+ //
42
+ // Sandbox any changes to the environment
43
+ //
44
+ beforeEach ( ( ) => {
45
+ env = clone ( process . env ) ;
46
+ } ) ;
47
+
48
+ afterEach ( ( ) => {
49
+ process . env = env ;
50
+ } ) ;
51
+
52
+ //
53
+ // Set up our stubbed app and protocol
54
+ //
28
55
beforeEach ( ( ) => {
29
56
app = {
30
57
on ( evt , cb ) {
@@ -53,6 +80,8 @@ describe('index', () => {
53
80
endpoint : 'dist' ,
54
81
indexPath : join ( '.' , 'index.html' ) ,
55
82
} ) ;
83
+ assert . ok ( handlerStub . calledWith ( { url : 'serve://dist' } ) ) ;
84
+ assert . equal ( process . env . ELECTRON_PROTOCOL_SERVE_INDEX , '/path/to/index.html' ) ;
56
85
} ) ;
57
86
58
87
it ( 'works with a custom cwd' , ( ) => {
@@ -69,6 +98,8 @@ describe('index', () => {
69
98
endpoint : 'dist' ,
70
99
indexPath : join ( 'foo' , 'bar' , 'index.html' ) ,
71
100
} ) ;
101
+ assert . ok ( handlerStub . calledWith ( { url : 'serve://dist' } ) ) ;
102
+ assert . equal ( process . env . ELECTRON_PROTOCOL_SERVE_INDEX , '/path/to/index.html' ) ;
72
103
} ) ;
73
104
74
105
it ( 'works with non-default arguments' , ( ) => {
@@ -88,6 +119,8 @@ describe('index', () => {
88
119
endpoint : 'so' ,
89
120
indexPath : join ( 'we' , 'meet' , 'again.html' ) ,
90
121
} ) ;
122
+ assert . ok ( handlerStub . calledWith ( { url : 'friend://so' } ) ) ;
123
+ assert . equal ( process . env . ELECTRON_PROTOCOL_SERVE_INDEX , '/path/to/index.html' ) ;
91
124
} ) ;
92
125
93
126
it ( 'required a cwd' , ( ) => {
0 commit comments