2
2
import type { SinonStub } from 'sinon' ;
3
3
import sinon from 'sinon' ;
4
4
5
- import { setupIntercom } from './setup-intercom' ;
5
+ import { setupIntercom , resetIntercomAllowedCache } from './setup-intercom' ;
6
6
import { expect } from 'chai' ;
7
7
import type { IntercomScript } from './intercom-script' ;
8
8
import type { PreferencesAccess } from 'compass-preferences-model' ;
@@ -11,6 +11,36 @@ import {
11
11
type User ,
12
12
} from 'compass-preferences-model' ;
13
13
14
+ // Picking something which won't be blocked by CORS
15
+ const FAKE_HADRON_AUTO_UPDATE_ENDPOINT = 'https://compass.mongodb.com' ;
16
+
17
+ function createMockFetch ( {
18
+ integrations,
19
+ } : {
20
+ integrations : Record < string , boolean > ;
21
+ } ) : typeof globalThis . fetch {
22
+ return ( url ) => {
23
+ if ( typeof url !== 'string' ) {
24
+ throw new Error ( 'Expected url to be a string' ) ;
25
+ }
26
+ if ( url . startsWith ( FAKE_HADRON_AUTO_UPDATE_ENDPOINT ) ) {
27
+ if ( url === `${ FAKE_HADRON_AUTO_UPDATE_ENDPOINT } /api/v2/integrations` ) {
28
+ return Promise . resolve ( {
29
+ ok : true ,
30
+ json ( ) {
31
+ return Promise . resolve ( integrations ) ;
32
+ } ,
33
+ } as Response ) ;
34
+ }
35
+ } else if ( url === 'https://widget.intercom.io/widget/appid123' ) {
36
+ // NOTE: we use 301 since intercom will redirects
37
+ // to the actual location of the widget script
38
+ return Promise . resolve ( { status : 301 } as Response ) ;
39
+ }
40
+ throw new Error ( `Unexpected URL called on the fake update server: ${ url } ` ) ;
41
+ } ;
42
+ }
43
+
14
44
const mockUser : User = {
15
45
id : 'user-123' ,
16
46
createdAt : new Date ( 1649432549945 ) ,
@@ -19,7 +49,10 @@ const mockUser: User = {
19
49
20
50
describe ( 'setupIntercom' , function ( ) {
21
51
let backupEnv : Partial < typeof process . env > ;
22
- let fetchMock : SinonStub ;
52
+ let fetchMock : SinonStub <
53
+ Parameters < typeof globalThis . fetch > ,
54
+ ReturnType < typeof globalThis . fetch >
55
+ > ;
23
56
let preferences : PreferencesAccess ;
24
57
25
58
async function testRunSetupIntercom ( ) {
@@ -36,22 +69,20 @@ describe('setupIntercom', function () {
36
69
37
70
beforeEach ( async function ( ) {
38
71
backupEnv = {
72
+ HADRON_AUTO_UPDATE_ENDPOINT : process . env . HADRON_AUTO_UPDATE_ENDPOINT ,
39
73
HADRON_METRICS_INTERCOM_APP_ID :
40
74
process . env . HADRON_METRICS_INTERCOM_APP_ID ,
41
75
HADRON_PRODUCT_NAME : process . env . HADRON_PRODUCT_NAME ,
42
76
HADRON_APP_VERSION : process . env . HADRON_APP_VERSION ,
43
77
NODE_ENV : process . env . NODE_ENV ,
44
78
} ;
45
79
80
+ process . env . HADRON_AUTO_UPDATE_ENDPOINT = FAKE_HADRON_AUTO_UPDATE_ENDPOINT ;
46
81
process . env . HADRON_PRODUCT_NAME = 'My App Name' as any ;
47
82
process . env . HADRON_APP_VERSION = 'v0.0.0-test.123' ;
48
83
process . env . NODE_ENV = 'test' ;
49
84
process . env . HADRON_METRICS_INTERCOM_APP_ID = 'appid123' ;
50
- fetchMock = sinon . stub ( ) ;
51
- window . fetch = fetchMock ;
52
- // NOTE: we use 301 since intercom will redirects
53
- // to the actual location of the widget script
54
- fetchMock . resolves ( { status : 301 } as Response ) ;
85
+ fetchMock = sinon . stub ( globalThis , 'fetch' ) ;
55
86
preferences = await createSandboxFromDefaultPreferences ( ) ;
56
87
await preferences . savePreferences ( {
57
88
enableFeedbackPanel : true ,
@@ -61,16 +92,23 @@ describe('setupIntercom', function () {
61
92
} ) ;
62
93
63
94
afterEach ( function ( ) {
95
+ process . env . HADRON_AUTO_UPDATE_ENDPOINT =
96
+ backupEnv . HADRON_AUTO_UPDATE_ENDPOINT ;
64
97
process . env . HADRON_METRICS_INTERCOM_APP_ID =
65
98
backupEnv . HADRON_METRICS_INTERCOM_APP_ID ;
66
99
process . env . HADRON_PRODUCT_NAME = backupEnv . HADRON_PRODUCT_NAME as any ;
67
100
process . env . HADRON_APP_VERSION = backupEnv . HADRON_APP_VERSION as any ;
68
101
process . env . NODE_ENV = backupEnv . NODE_ENV ;
69
- fetchMock . reset ( ) ;
102
+ fetchMock . restore ( ) ;
103
+ resetIntercomAllowedCache ( ) ;
70
104
} ) ;
71
105
72
106
describe ( 'when it can be enabled' , function ( ) {
73
107
it ( 'calls intercomScript.load when feedback gets enabled and intercomScript.unload when feedback gets disabled' , async function ( ) {
108
+ fetchMock . callsFake (
109
+ createMockFetch ( { integrations : { intercom : true } } )
110
+ ) ;
111
+
74
112
await preferences . savePreferences ( {
75
113
enableFeedbackPanel : true ,
76
114
} ) ;
@@ -100,6 +138,19 @@ describe('setupIntercom', function () {
100
138
expect ( intercomScript . load ) . not . to . have . been . called ;
101
139
expect ( intercomScript . unload ) . to . have . been . called ;
102
140
} ) ;
141
+
142
+ it ( 'calls intercomScript.unload when the update server disables the integration' , async function ( ) {
143
+ fetchMock . callsFake (
144
+ createMockFetch ( { integrations : { intercom : false } } )
145
+ ) ;
146
+
147
+ await preferences . savePreferences ( {
148
+ enableFeedbackPanel : true ,
149
+ } ) ;
150
+ const { intercomScript } = await testRunSetupIntercom ( ) ;
151
+ expect ( intercomScript . load ) . not . to . have . been . called ;
152
+ expect ( intercomScript . unload ) . to . have . been . called ;
153
+ } ) ;
103
154
} ) ;
104
155
105
156
describe ( 'when cannot be enabled' , function ( ) {
0 commit comments