From 5f10a769c9809eec847f41e3a226888d6eaa7578 Mon Sep 17 00:00:00 2001 From: Li Yi Yu Date: Tue, 26 Sep 2023 18:52:45 -0400 Subject: [PATCH] move empty array return into getSurveys --- src/__tests__/surveys.js | 7 +++++++ src/posthog-surveys.ts | 7 ++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/__tests__/surveys.js b/src/__tests__/surveys.js index 20024e24b..8290e6339 100644 --- a/src/__tests__/surveys.js +++ b/src/__tests__/surveys.js @@ -101,6 +101,13 @@ describe('surveys', () => { expect(given.instance._send_request).toHaveBeenCalledTimes(2) }) + it('getSurveys returns empty array if surveys are undefined', () => { + given('surveysResponse', () => ({ surveys: undefined })) + given.surveys.getSurveys((data) => { + expect(data).toEqual([]) + }) + }) + describe('getActiveMatchingSurveys', () => { const draftSurvey = { name: 'draft survey', diff --git a/src/posthog-surveys.ts b/src/posthog-surveys.ts index 76cfdf657..1894e4650 100644 --- a/src/posthog-surveys.ts +++ b/src/posthog-surveys.ts @@ -10,14 +10,14 @@ export class PostHogSurveys { } getSurveys(callback: SurveyCallback, forceReload = false) { - const existingSurveys = this.instance.get_property(SURVEYS) + const existingSurveys = this.instance.get_property(SURVEYS) || [] if (!existingSurveys || forceReload) { this.instance._send_request( `${this.instance.get_config('api_host')}/api/surveys/?token=${this.instance.get_config('token')}`, {}, { method: 'GET' }, (response) => { - const surveys = response.surveys + const surveys = response.surveys || [] this.instance.persistence?.register({ [SURVEYS]: surveys }) return callback(surveys) } @@ -29,9 +29,6 @@ export class PostHogSurveys { getActiveMatchingSurveys(callback: SurveyCallback, forceReload = false) { this.getSurveys((surveys) => { - if (!surveys) { - return callback([]) - } const activeSurveys = surveys.filter((survey) => { return !!(survey.start_date && !survey.end_date) })