1
1
/// <reference lib="dom" />
2
2
3
- import { PostHogSurveys } from '../posthog-surveys'
4
- import {
5
- SurveyType ,
6
- SurveyQuestionType ,
7
- Survey ,
8
- MultipleSurveyQuestion ,
9
- SurveyQuestionBranchingType ,
10
- SurveyQuestion ,
11
- RatingSurveyQuestion ,
12
- } from '../posthog-surveys-types'
3
+ import { generateSurveys , getNextSurveyStep } from '../extensions/surveys'
13
4
import {
14
5
canActivateRepeatedly ,
15
6
getDisplayOrderChoices ,
16
7
getDisplayOrderQuestions ,
17
8
} from '../extensions/surveys/surveys-utils'
18
- import { PostHogPersistence } from '../posthog-persistence'
19
9
import { PostHog } from '../posthog-core'
10
+ import { PostHogPersistence } from '../posthog-persistence'
11
+ import { PostHogSurveys } from '../posthog-surveys'
12
+ import {
13
+ MultipleSurveyQuestion ,
14
+ RatingSurveyQuestion ,
15
+ Survey ,
16
+ SurveyQuestion ,
17
+ SurveyQuestionBranchingType ,
18
+ SurveyQuestionType ,
19
+ SurveyType ,
20
+ } from '../posthog-surveys-types'
20
21
import { DecideResponse , PostHogConfig , Properties } from '../types'
21
- import { window } from '../utils/globals'
22
- import { RequestRouter } from '../utils/request-router'
23
- import { assignableWindow } from '../utils/globals'
24
- import { generateSurveys } from '../extensions/surveys'
25
22
import * as globals from '../utils/globals'
23
+ import { assignableWindow , window } from '../utils/globals'
24
+ import { RequestRouter } from '../utils/request-router'
26
25
27
26
describe ( 'surveys' , ( ) => {
28
27
let config : PostHogConfig
@@ -914,8 +913,8 @@ describe('surveys', () => {
914
913
{ type : SurveyQuestionType . Open , question : 'Question A' } ,
915
914
{ type : SurveyQuestionType . Open , question : 'Question B' } ,
916
915
] as SurveyQuestion [ ]
917
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( 1 )
918
- expect ( surveys . getNextSurveyStep ( survey , 1 , 'Some response' ) ) . toEqual ( SurveyQuestionBranchingType . End )
916
+ expect ( getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( 1 )
917
+ expect ( getNextSurveyStep ( survey , 1 , 'Some response' ) ) . toEqual ( SurveyQuestionBranchingType . End )
919
918
} )
920
919
921
920
it ( 'should branch out to `end`' , ( ) => {
@@ -927,7 +926,7 @@ describe('surveys', () => {
927
926
} ,
928
927
{ type : SurveyQuestionType . Open , question : 'Question B' } ,
929
928
] as SurveyQuestion [ ]
930
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( SurveyQuestionBranchingType . End )
929
+ expect ( getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( SurveyQuestionBranchingType . End )
931
930
} )
932
931
933
932
it ( 'should branch out to a specific question' , ( ) => {
@@ -940,7 +939,7 @@ describe('surveys', () => {
940
939
{ type : SurveyQuestionType . Open , question : 'Question B' } ,
941
940
{ type : SurveyQuestionType . Open , question : 'Question C' } ,
942
941
] as SurveyQuestion [ ]
943
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( 2 )
942
+ expect ( getNextSurveyStep ( survey , 0 , 'Some response' ) ) . toEqual ( 2 )
944
943
} )
945
944
946
945
// Single-choice
@@ -960,9 +959,9 @@ describe('surveys', () => {
960
959
{ type : SurveyQuestionType . Open , question : 'Why no?' } ,
961
960
{ type : SurveyQuestionType . Open , question : 'Why maybe?' } ,
962
961
] as unknown [ ] as SurveyQuestion [ ]
963
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'Yes' ) ) . toEqual ( 1 )
964
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'No' ) ) . toEqual ( 2 )
965
- expect ( surveys . getNextSurveyStep ( survey , 0 , 'Maybe' ) ) . toEqual ( 3 )
962
+ expect ( getNextSurveyStep ( survey , 0 , 'Yes' ) ) . toEqual ( 1 )
963
+ expect ( getNextSurveyStep ( survey , 0 , 'No' ) ) . toEqual ( 2 )
964
+ expect ( getNextSurveyStep ( survey , 0 , 'Maybe' ) ) . toEqual ( 3 )
966
965
} )
967
966
968
967
// Response-based branching, scale 1-3
@@ -982,9 +981,9 @@ describe('surveys', () => {
982
981
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
983
982
] as unknown [ ] as SurveyQuestion [ ]
984
983
985
- expect ( surveys . getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
986
- expect ( surveys . getNextSurveyStep ( survey , 0 , 2 ) ) . toEqual ( 2 )
987
- expect ( surveys . getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 3 )
984
+ expect ( getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
985
+ expect ( getNextSurveyStep ( survey , 0 , 2 ) ) . toEqual ( 2 )
986
+ expect ( getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 3 )
988
987
} )
989
988
990
989
// Response-based branching, scale 1-5
@@ -1004,9 +1003,9 @@ describe('surveys', () => {
1004
1003
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
1005
1004
] as unknown as SurveyQuestion [ ]
1006
1005
1007
- expect ( surveys . getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1008
- expect ( surveys . getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 2 )
1009
- expect ( surveys . getNextSurveyStep ( survey , 0 , 5 ) ) . toEqual ( 3 )
1006
+ expect ( getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1007
+ expect ( getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 2 )
1008
+ expect ( getNextSurveyStep ( survey , 0 , 5 ) ) . toEqual ( 3 )
1010
1009
} )
1011
1010
1012
1011
// Response-based branching, scale 1-7
@@ -1026,13 +1025,13 @@ describe('surveys', () => {
1026
1025
{ type : SurveyQuestionType . Open , question : 'Great! What did you enjoy the most?' } ,
1027
1026
] as unknown as SurveyQuestion [ ]
1028
1027
1029
- expect ( surveys . getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1030
- expect ( surveys . getNextSurveyStep ( survey , 0 , 2 ) ) . toEqual ( 1 )
1031
- expect ( surveys . getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 1 )
1032
- expect ( surveys . getNextSurveyStep ( survey , 0 , 4 ) ) . toEqual ( 2 )
1033
- expect ( surveys . getNextSurveyStep ( survey , 0 , 5 ) ) . toEqual ( 3 )
1034
- expect ( surveys . getNextSurveyStep ( survey , 0 , 6 ) ) . toEqual ( 3 )
1035
- expect ( surveys . getNextSurveyStep ( survey , 0 , 7 ) ) . toEqual ( 3 )
1028
+ expect ( getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1029
+ expect ( getNextSurveyStep ( survey , 0 , 2 ) ) . toEqual ( 1 )
1030
+ expect ( getNextSurveyStep ( survey , 0 , 3 ) ) . toEqual ( 1 )
1031
+ expect ( getNextSurveyStep ( survey , 0 , 4 ) ) . toEqual ( 2 )
1032
+ expect ( getNextSurveyStep ( survey , 0 , 5 ) ) . toEqual ( 3 )
1033
+ expect ( getNextSurveyStep ( survey , 0 , 6 ) ) . toEqual ( 3 )
1034
+ expect ( getNextSurveyStep ( survey , 0 , 7 ) ) . toEqual ( 3 )
1036
1035
} )
1037
1036
1038
1037
// Response-based branching, scale 0-10 (NPS)
@@ -1052,9 +1051,9 @@ describe('surveys', () => {
1052
1051
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
1053
1052
] as unknown as SurveyQuestion [ ]
1054
1053
1055
- expect ( surveys . getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1056
- expect ( surveys . getNextSurveyStep ( survey , 0 , 8 ) ) . toEqual ( 2 )
1057
- expect ( surveys . getNextSurveyStep ( survey , 0 , 10 ) ) . toEqual ( 3 )
1054
+ expect ( getNextSurveyStep ( survey , 0 , 1 ) ) . toEqual ( 1 )
1055
+ expect ( getNextSurveyStep ( survey , 0 , 8 ) ) . toEqual ( 2 )
1056
+ expect ( getNextSurveyStep ( survey , 0 , 10 ) ) . toEqual ( 3 )
1058
1057
} )
1059
1058
1060
1059
it ( 'should display questions in the order AGCEHDFB' , ( ) => {
@@ -1108,7 +1107,7 @@ describe('surveys', () => {
1108
1107
for ( let i = 0 ; i < survey . questions . length ; i ++ ) {
1109
1108
const currentQuestion = survey . questions [ currentStep ]
1110
1109
actualOrder . push ( currentQuestion . question )
1111
- currentStep = surveys . getNextSurveyStep ( survey , currentStep , 'Some response' )
1110
+ currentStep = getNextSurveyStep ( survey , currentStep , 'Some response' )
1112
1111
}
1113
1112
1114
1113
expect ( desiredOrder ) . toEqual ( actualOrder )
@@ -1167,7 +1166,7 @@ describe('surveys', () => {
1167
1166
for ( const answer of answers ) {
1168
1167
const currentQuestion = survey . questions [ currentStep ]
1169
1168
actualOrder . push ( currentQuestion . question )
1170
- currentStep = surveys . getNextSurveyStep ( survey , currentStep , answer )
1169
+ currentStep = getNextSurveyStep ( survey , currentStep , answer )
1171
1170
}
1172
1171
expect ( desiredOrder ) . toEqual ( actualOrder )
1173
1172
expect ( currentStep ) . toEqual ( SurveyQuestionBranchingType . End )
@@ -1180,7 +1179,7 @@ describe('surveys', () => {
1180
1179
for ( const answer of answers ) {
1181
1180
const currentQuestion = survey . questions [ currentStep ]
1182
1181
actualOrder . push ( currentQuestion . question )
1183
- currentStep = surveys . getNextSurveyStep ( survey , currentStep , answer )
1182
+ currentStep = getNextSurveyStep ( survey , currentStep , answer )
1184
1183
}
1185
1184
expect ( desiredOrder ) . toEqual ( actualOrder )
1186
1185
expect ( currentStep ) . toEqual ( SurveyQuestionBranchingType . End )
@@ -1193,7 +1192,7 @@ describe('surveys', () => {
1193
1192
for ( const answer of answers ) {
1194
1193
const currentQuestion = survey . questions [ currentStep ]
1195
1194
actualOrder . push ( currentQuestion . question )
1196
- currentStep = surveys . getNextSurveyStep ( survey , currentStep , answer )
1195
+ currentStep = getNextSurveyStep ( survey , currentStep , answer )
1197
1196
}
1198
1197
expect ( desiredOrder ) . toEqual ( actualOrder )
1199
1198
expect ( currentStep ) . toEqual ( SurveyQuestionBranchingType . End )
@@ -1210,7 +1209,7 @@ describe('surveys', () => {
1210
1209
for ( const answer of answers ) {
1211
1210
const currentQuestion = survey . questions [ currentStep ]
1212
1211
actualOrder . push ( currentQuestion . question )
1213
- currentStep = surveys . getNextSurveyStep ( survey , currentStep , answer )
1212
+ currentStep = getNextSurveyStep ( survey , currentStep , answer )
1214
1213
}
1215
1214
expect ( desiredOrder ) . toEqual ( actualOrder )
1216
1215
expect ( currentStep ) . toEqual ( SurveyQuestionBranchingType . End )
@@ -1231,7 +1230,7 @@ describe('surveys', () => {
1231
1230
{ type : SurveyQuestionType . Open , question : 'Seems you are not completely happy. Tell us more!' } ,
1232
1231
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
1233
1232
] as unknown as SurveyQuestion [ ]
1234
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 1 ) ) . toThrow ( 'The scale must be one of: 3, 5, 7, 10' )
1233
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 1 ) ) . toThrow ( 'The scale must be one of: 3, 5, 7, 10' )
1235
1234
} )
1236
1235
1237
1236
it ( 'should throw an error for a response value out of the valid range' , ( ) => {
@@ -1249,13 +1248,13 @@ describe('surveys', () => {
1249
1248
{ type : SurveyQuestionType . Open , question : 'Seems you are not completely happy. Tell us more!' } ,
1250
1249
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
1251
1250
] as unknown as SurveyQuestion [ ]
1252
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-3' )
1251
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-3' )
1253
1252
; ( survey . questions [ 0 ] as RatingSurveyQuestion ) . scale = 5
1254
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-5' )
1253
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-5' )
1255
1254
; ( survey . questions [ 0 ] as RatingSurveyQuestion ) . scale = 7
1256
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-7' )
1255
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 1-7' )
1257
1256
; ( survey . questions [ 0 ] as RatingSurveyQuestion ) . scale = 10
1258
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 0-10' )
1257
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 20 ) ) . toThrow ( 'The response must be in range 0-10' )
1259
1258
} )
1260
1259
1261
1260
it ( 'should throw an error for if a response value in a rating question is not an integer' , ( ) => {
@@ -1273,10 +1272,8 @@ describe('surveys', () => {
1273
1272
{ type : SurveyQuestionType . Open , question : 'Seems you are not completely happy. Tell us more!' } ,
1274
1273
{ type : SurveyQuestionType . Open , question : 'Glad to hear that. Tell us more!' } ,
1275
1274
] as unknown as SurveyQuestion [ ]
1276
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , '2' ) ) . toThrow ( 'The response type must be an integer' )
1277
- expect ( ( ) => surveys . getNextSurveyStep ( survey , 0 , 'some_string' ) ) . toThrow (
1278
- 'The response type must be an integer'
1279
- )
1275
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , '2' ) ) . toThrow ( 'The response type must be an integer' )
1276
+ expect ( ( ) => getNextSurveyStep ( survey , 0 , 'some_string' ) ) . toThrow ( 'The response type must be an integer' )
1280
1277
} )
1281
1278
} )
1282
1279
0 commit comments