22import type { SinonStub } from 'sinon' ;
33import sinon from 'sinon' ;
44
5- import { setupIntercom } from './setup-intercom' ;
5+ import { setupIntercom , resetIntercomAllowedCache } from './setup-intercom' ;
66import { expect } from 'chai' ;
77import type { IntercomScript } from './intercom-script' ;
88import type { PreferencesAccess } from 'compass-preferences-model' ;
@@ -11,6 +11,36 @@ import {
1111 type User ,
1212} from 'compass-preferences-model' ;
1313
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+
1444const mockUser : User = {
1545 id : 'user-123' ,
1646 createdAt : new Date ( 1649432549945 ) ,
@@ -19,7 +49,10 @@ const mockUser: User = {
1949
2050describe ( 'setupIntercom' , function ( ) {
2151 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+ > ;
2356 let preferences : PreferencesAccess ;
2457
2558 async function testRunSetupIntercom ( ) {
@@ -36,22 +69,20 @@ describe('setupIntercom', function () {
3669
3770 beforeEach ( async function ( ) {
3871 backupEnv = {
72+ HADRON_AUTO_UPDATE_ENDPOINT : process . env . HADRON_AUTO_UPDATE_ENDPOINT ,
3973 HADRON_METRICS_INTERCOM_APP_ID :
4074 process . env . HADRON_METRICS_INTERCOM_APP_ID ,
4175 HADRON_PRODUCT_NAME : process . env . HADRON_PRODUCT_NAME ,
4276 HADRON_APP_VERSION : process . env . HADRON_APP_VERSION ,
4377 NODE_ENV : process . env . NODE_ENV ,
4478 } ;
4579
80+ process . env . HADRON_AUTO_UPDATE_ENDPOINT = FAKE_HADRON_AUTO_UPDATE_ENDPOINT ;
4681 process . env . HADRON_PRODUCT_NAME = 'My App Name' as any ;
4782 process . env . HADRON_APP_VERSION = 'v0.0.0-test.123' ;
4883 process . env . NODE_ENV = 'test' ;
4984 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' ) ;
5586 preferences = await createSandboxFromDefaultPreferences ( ) ;
5687 await preferences . savePreferences ( {
5788 enableFeedbackPanel : true ,
@@ -61,16 +92,23 @@ describe('setupIntercom', function () {
6192 } ) ;
6293
6394 afterEach ( function ( ) {
95+ process . env . HADRON_AUTO_UPDATE_ENDPOINT =
96+ backupEnv . HADRON_AUTO_UPDATE_ENDPOINT ;
6497 process . env . HADRON_METRICS_INTERCOM_APP_ID =
6598 backupEnv . HADRON_METRICS_INTERCOM_APP_ID ;
6699 process . env . HADRON_PRODUCT_NAME = backupEnv . HADRON_PRODUCT_NAME as any ;
67100 process . env . HADRON_APP_VERSION = backupEnv . HADRON_APP_VERSION as any ;
68101 process . env . NODE_ENV = backupEnv . NODE_ENV ;
69- fetchMock . reset ( ) ;
102+ fetchMock . restore ( ) ;
103+ resetIntercomAllowedCache ( ) ;
70104 } ) ;
71105
72106 describe ( 'when it can be enabled' , function ( ) {
73107 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+
74112 await preferences . savePreferences ( {
75113 enableFeedbackPanel : true ,
76114 } ) ;
@@ -100,6 +138,19 @@ describe('setupIntercom', function () {
100138 expect ( intercomScript . load ) . not . to . have . been . called ;
101139 expect ( intercomScript . unload ) . to . have . been . called ;
102140 } ) ;
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+ } ) ;
103154 } ) ;
104155
105156 describe ( 'when cannot be enabled' , function ( ) {
0 commit comments