@@ -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
+ } ) {
22
+ return async ( 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 {
29
+ ok : true ,
30
+ async json ( ) {
31
+ return 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 { 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 ) ,
@@ -39,13 +69,15 @@ describe('setupIntercom', function () {
39
69
40
70
beforeEach ( async function ( ) {
41
71
backupEnv = {
72
+ HADRON_AUTO_UPDATE_ENDPOINT : process . env . HADRON_AUTO_UPDATE_ENDPOINT ,
42
73
HADRON_METRICS_INTERCOM_APP_ID :
43
74
process . env . HADRON_METRICS_INTERCOM_APP_ID ,
44
75
HADRON_PRODUCT_NAME : process . env . HADRON_PRODUCT_NAME ,
45
76
HADRON_APP_VERSION : process . env . HADRON_APP_VERSION ,
46
77
NODE_ENV : process . env . NODE_ENV ,
47
78
} ;
48
79
80
+ process . env . HADRON_AUTO_UPDATE_ENDPOINT = FAKE_HADRON_AUTO_UPDATE_ENDPOINT ;
49
81
process . env . HADRON_PRODUCT_NAME = 'My App Name' as any ;
50
82
process . env . HADRON_APP_VERSION = 'v0.0.0-test.123' ;
51
83
process . env . NODE_ENV = 'test' ;
@@ -60,6 +92,8 @@ describe('setupIntercom', function () {
60
92
} ) ;
61
93
62
94
afterEach ( function ( ) {
95
+ process . env . HADRON_AUTO_UPDATE_ENDPOINT =
96
+ backupEnv . HADRON_AUTO_UPDATE_ENDPOINT ;
63
97
process . env . HADRON_METRICS_INTERCOM_APP_ID =
64
98
backupEnv . HADRON_METRICS_INTERCOM_APP_ID ;
65
99
process . env . HADRON_PRODUCT_NAME = backupEnv . HADRON_PRODUCT_NAME as any ;
@@ -70,6 +104,10 @@ describe('setupIntercom', function () {
70
104
71
105
describe ( 'when it can be enabled' , function ( ) {
72
106
it ( 'calls intercomScript.load when feedback gets enabled and intercomScript.unload when feedback gets disabled' , async function ( ) {
107
+ fetchMock . callsFake (
108
+ createMockFetch ( { integrations : { intercom : true } } )
109
+ ) ;
110
+
73
111
await preferences . savePreferences ( {
74
112
enableFeedbackPanel : true ,
75
113
} ) ;
@@ -99,6 +137,19 @@ describe('setupIntercom', function () {
99
137
expect ( intercomScript . load ) . not . to . have . been . called ;
100
138
expect ( intercomScript . unload ) . to . have . been . called ;
101
139
} ) ;
140
+
141
+ it ( 'calls intercomScript.unload when the update server disables the integration' , async function ( ) {
142
+ fetchMock . callsFake (
143
+ createMockFetch ( { integrations : { intercom : false } } )
144
+ ) ;
145
+
146
+ await preferences . savePreferences ( {
147
+ enableFeedbackPanel : true ,
148
+ } ) ;
149
+ const { intercomScript } = await testRunSetupIntercom ( ) ;
150
+ expect ( intercomScript . load ) . not . to . have . been . called ;
151
+ expect ( intercomScript . unload ) . to . have . been . called ;
152
+ } ) ;
102
153
} ) ;
103
154
104
155
describe ( 'when cannot be enabled' , function ( ) {
0 commit comments