Skip to content

Commit 938d4e3

Browse files
committed
Fix tests
1 parent 6b0812b commit 938d4e3

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/compass-intercom/src/setup-intercom.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}) {
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+
1444
const mockUser: User = {
1545
id: 'user-123',
1646
createdAt: new Date(1649432549945),
@@ -39,13 +69,15 @@ describe('setupIntercom', function () {
3969

4070
beforeEach(async function () {
4171
backupEnv = {
72+
HADRON_AUTO_UPDATE_ENDPOINT: process.env.HADRON_AUTO_UPDATE_ENDPOINT,
4273
HADRON_METRICS_INTERCOM_APP_ID:
4374
process.env.HADRON_METRICS_INTERCOM_APP_ID,
4475
HADRON_PRODUCT_NAME: process.env.HADRON_PRODUCT_NAME,
4576
HADRON_APP_VERSION: process.env.HADRON_APP_VERSION,
4677
NODE_ENV: process.env.NODE_ENV,
4778
};
4879

80+
process.env.HADRON_AUTO_UPDATE_ENDPOINT = FAKE_HADRON_AUTO_UPDATE_ENDPOINT;
4981
process.env.HADRON_PRODUCT_NAME = 'My App Name' as any;
5082
process.env.HADRON_APP_VERSION = 'v0.0.0-test.123';
5183
process.env.NODE_ENV = 'test';
@@ -60,6 +92,8 @@ describe('setupIntercom', function () {
6092
});
6193

6294
afterEach(function () {
95+
process.env.HADRON_AUTO_UPDATE_ENDPOINT =
96+
backupEnv.HADRON_AUTO_UPDATE_ENDPOINT;
6397
process.env.HADRON_METRICS_INTERCOM_APP_ID =
6498
backupEnv.HADRON_METRICS_INTERCOM_APP_ID;
6599
process.env.HADRON_PRODUCT_NAME = backupEnv.HADRON_PRODUCT_NAME as any;
@@ -70,6 +104,10 @@ describe('setupIntercom', function () {
70104

71105
describe('when it can be enabled', function () {
72106
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+
73111
await preferences.savePreferences({
74112
enableFeedbackPanel: true,
75113
});
@@ -99,6 +137,19 @@ describe('setupIntercom', function () {
99137
expect(intercomScript.load).not.to.have.been.called;
100138
expect(intercomScript.unload).to.have.been.called;
101139
});
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+
});
102153
});
103154

104155
describe('when cannot be enabled', function () {

0 commit comments

Comments
 (0)