Skip to content

Commit

Permalink
fix: default integrations settings are set properly when unable to fe…
Browse files Browse the repository at this point in the history
…tch (#684)

Co-authored-by: Mohamed K. Coulibali <[email protected]>
  • Loading branch information
konoufo and Mohamed K. Coulibali authored Sep 28, 2022
1 parent 076848f commit 0627deb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions packages/core/src/__tests__/internal/fetchSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { getMockLogger } from '../__helpers__/mockLogger';
import { SegmentClient } from '../../analytics';
import { MockSegmentStore } from '../__helpers__/mockSegmentStore';
import { SEGMENT_DESTINATION_KEY } from '../../plugins/SegmentDestination';
import { settingsCDN } from '../../constants';

describe('internal #getSettings', () => {
const defaultIntegrationSettings = {
integrations: {
// This one is injected by the mock
[SEGMENT_DESTINATION_KEY]: {},
// Make sure the value associated with this key here is different
// from the initial value in `store.settings` as set by the mock store.
// Otherwise we can't actually test that default settings are set correctly
// i.e. tests that should fail could misleadingly appear to succeed.
[SEGMENT_DESTINATION_KEY]: { apiKey: 'bar', apiHost: 'boo' },
},
};
const store = new MockSegmentStore();
Expand All @@ -30,7 +34,7 @@ describe('internal #getSettings', () => {
});

afterEach(() => {
jest.resetAllMocks();
jest.clearAllMocks();
});

it('fetches the settings succesfully ', async () => {
Expand All @@ -44,14 +48,12 @@ describe('internal #getSettings', () => {
await client.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
'https://cdn-settings.segment.com/v1/projects/123-456/settings'
`${settingsCDN}/${clientArgs.config.writeKey}/settings`
);

expect(setSettingsSpy).toHaveBeenCalledWith(mockJSONResponse.integrations);
expect(store.settings.get()).toEqual(mockJSONResponse.integrations);
expect(client.settings.get()).toEqual({
...mockJSONResponse.integrations,
});
expect(client.settings.get()).toEqual(mockJSONResponse.integrations);
});

it('fails to the settings succesfully and uses the default if specified', async () => {
Expand All @@ -61,10 +63,12 @@ describe('internal #getSettings', () => {
await client.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
'https://cdn-settings.segment.com/v1/projects/123-456/settings'
`${settingsCDN}/${clientArgs.config.writeKey}/settings`
);

expect(setSettingsSpy).toHaveBeenCalledWith(defaultIntegrationSettings);
expect(setSettingsSpy).toHaveBeenCalledWith(
defaultIntegrationSettings.integrations
);
expect(store.settings.get()).toEqual(
defaultIntegrationSettings.integrations
);
Expand All @@ -84,7 +88,7 @@ describe('internal #getSettings', () => {
await anotherClient.fetchSettings();

expect(fetch).toHaveBeenCalledWith(
'https://cdn-settings.segment.com/v1/projects/123-456/settings'
`${settingsCDN}/${clientArgs.config.writeKey}/settings`
);
expect(setSettingsSpy).not.toHaveBeenCalled();
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class SegmentClient {
}`
);
if (this.config.defaultSettings) {
await this.store.settings.set(this.config.defaultSettings);
await this.store.settings.set(this.config.defaultSettings.integrations);
}
}
}
Expand Down

0 comments on commit 0627deb

Please sign in to comment.