From 3cb5b34e2112b2067de9233da6923d68653399a5 Mon Sep 17 00:00:00 2001 From: Jason Liu Date: Wed, 19 Jul 2023 09:32:46 -0400 Subject: [PATCH] flag for ConfigAPI.getconfig --- src/index.ts | 6 +++++- test/api/trips.test.ts | 6 ++---- test/radar.test.ts | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 6d842d91..490fe1ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -92,7 +92,11 @@ class Radar { Logger.info(`using options: ${JSON.stringify(options)}`); } - ConfigAPI.getConfig(); + // NOTE(jasonl): this allows us to run jest tests + // without having to mock the ConfigAPI.getConfig call + if (!(window as any)?.RADAR_TEST_ENV) { + ConfigAPI.getConfig(); + } } public static clear() { diff --git a/test/api/trips.test.ts b/test/api/trips.test.ts index c5f24ed0..8eac2110 100644 --- a/test/api/trips.test.ts +++ b/test/api/trips.test.ts @@ -11,6 +11,8 @@ describe('Trips', () => { beforeEach(() => { Radar.initialize('prj_test_pk_123'); + Radar.setUserId(userId); + jest.spyOn(Http, 'request'); }); afterEach(() => { @@ -19,10 +21,6 @@ describe('Trips', () => { }); describe('startTrip', () => { - beforeEach(() => { - Radar.setUserId(userId); - jest.spyOn(Http, 'request'); - }); describe('called without tripOptions', () => { it('should include tripOptions each set to unknown', async () => { diff --git a/test/radar.test.ts b/test/radar.test.ts index d1385c34..f2060877 100644 --- a/test/radar.test.ts +++ b/test/radar.test.ts @@ -11,6 +11,7 @@ import ContextAPI from '../src/api/context'; import SearchAPI from '../src/api/search'; import TrackAPI from '../src/api/track'; import RoutingAPI from '../src/api/routing'; +import ConfigAPI from '../src/api/config'; import { latitude, longitude } from './common'; @@ -51,6 +52,15 @@ describe('Radar', () => { const units = 'imperial'; describe('initialize', () => { + beforeEach(() => { + (window as any).RADAR_TEST_ENV = false; + }); + + afterEach(() => { + (window as any).RADAR_TEST_ENV = true; + jest.restoreAllMocks(); + }); + describe('no key is provided', () => { it('should throw RadarPublishableKeyError', () => { try { @@ -77,19 +87,23 @@ describe('Radar', () => { describe('test publishableKey is provided', () => { it('should initialize SDK in a test environment', () => { + jest.spyOn(ConfigAPI, 'getConfig'); Radar.initialize('_my_test_pk_123'); const options = Config.get(); expect(options.publishableKey).toEqual('_my_test_pk_123'); expect(options.live).toEqual(false); + expect(ConfigAPI.getConfig).toHaveBeenCalledTimes(1); }); }); describe('live publishableKey is provided', () => { it('should initialize SDK in a live environment', () => { + jest.spyOn(ConfigAPI, 'getConfig'); Radar.initialize('_my_live_pk_123'); const options = Config.get(); expect(options.publishableKey).toEqual('_my_live_pk_123'); expect(options.live).toEqual(true); + expect(ConfigAPI.getConfig).toHaveBeenCalledTimes(1); }); }); });