Skip to content

Commit

Permalink
move empty array return into getSurveys
Browse files Browse the repository at this point in the history
  • Loading branch information
liyiy committed Sep 26, 2023
1 parent e9955bb commit 5f10a76
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/__tests__/surveys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 2 additions & 5 deletions src/posthog-surveys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
})
Expand Down

0 comments on commit 5f10a76

Please sign in to comment.