1
+ import * as _ from 'lodash' ;
2
+ import * as os from 'os' ;
3
+ import * as fs from 'fs' ;
4
+ import { CompletedRequest } from 'mockttp' ;
5
+ import { setupInterceptor , itIsAvailable , itCanBeActivated } from './interceptor-test-utils' ;
6
+
7
+ import { expect } from 'chai' ;
8
+
9
+ const interceptorSetup = setupInterceptor ( 'electron' ) ;
10
+
11
+ // We use slack as a test app. Doesn't really matter what it is, but
12
+ // slack is pretty common, easy to kill & restart, and fairly representative.
13
+ const electronAppPath : string | undefined = [
14
+ '/usr/bin/slack' ,
15
+ '/Applications/Slack.app/Contents/MacOS/Slack' ,
16
+ `${ os . homedir ( ) } \\AppData\\Local\\slack\\slack.exe`
17
+ ] . find ( ( path ) => fs . existsSync ( path ) ) ;
18
+
19
+ describe ( 'Electron interception' , function ( ) {
20
+ this . timeout ( 5000000 ) ;
21
+
22
+ beforeEach ( async ( ) => {
23
+ const { server } = await interceptorSetup ;
24
+ await server . start ( ) ;
25
+ await server . anyRequest ( ) . thenPassThrough ( ) ;
26
+ } ) ;
27
+
28
+ afterEach ( async ( ) => {
29
+ const { server, interceptor } = await interceptorSetup ;
30
+
31
+ await interceptor . deactivate ( server . port ) ;
32
+ await server . stop ( ) ;
33
+ } ) ;
34
+
35
+ itIsAvailable ( interceptorSetup ) ;
36
+
37
+ it ( 'can be activated' , async function ( ) {
38
+ if ( ! electronAppPath ) this . skip ( ) ;
39
+
40
+ const { interceptor, server } = await interceptorSetup ;
41
+
42
+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( false ) ;
43
+
44
+ await interceptor . activate ( server . port , {
45
+ pathToApplication : electronAppPath
46
+ } ) ;
47
+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( true ) ;
48
+ expect ( interceptor . isActive ( server . port + 1 ) ) . to . equal ( false ) ;
49
+
50
+ await interceptor . deactivate ( server . port ) ;
51
+ expect ( interceptor . isActive ( server . port ) ) . to . equal ( false ) ;
52
+ } ) ;
53
+
54
+ it ( 'successfully makes requests' , async function ( ) {
55
+ if ( ! electronAppPath ) this . skip ( ) ;
56
+
57
+ const { server, interceptor } = await interceptorSetup ;
58
+
59
+ const slackRequestReceived = new Promise < CompletedRequest > ( ( resolve ) =>
60
+ server . on ( 'request' , ( req ) => {
61
+ if ( req . url . includes ( 'slack.com' ) ) {
62
+ resolve ( req ) ;
63
+ }
64
+ } )
65
+ ) ;
66
+
67
+ await interceptor . activate ( server . port , {
68
+ pathToApplication : electronAppPath
69
+ } ) ;
70
+
71
+ // Only resolves if a slack request is intercepted
72
+ await slackRequestReceived ;
73
+ } ) ;
74
+ } ) ;
0 commit comments