@posthog/core 1.69.9 → 1.71.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/capture-v1/package.json +6 -0
- package/dist/capture-v1/compression.d.ts +18 -0
- package/dist/capture-v1/compression.d.ts.map +1 -0
- package/dist/capture-v1/compression.js +70 -0
- package/dist/capture-v1/compression.mjs +26 -0
- package/dist/capture-v1/errors.d.ts +48 -0
- package/dist/capture-v1/errors.d.ts.map +1 -0
- package/dist/capture-v1/errors.js +82 -0
- package/dist/capture-v1/errors.mjs +38 -0
- package/dist/capture-v1/index.d.ts +15 -0
- package/dist/capture-v1/index.d.ts.map +1 -0
- package/dist/capture-v1/index.js +88 -0
- package/dist/capture-v1/index.mjs +7 -0
- package/dist/capture-v1/sender.d.ts +150 -0
- package/dist/capture-v1/sender.d.ts.map +1 -0
- package/dist/capture-v1/sender.js +554 -0
- package/dist/capture-v1/sender.mjs +507 -0
- package/dist/capture-v1/serialize.d.ts +30 -0
- package/dist/capture-v1/serialize.d.ts.map +1 -0
- package/dist/capture-v1/serialize.js +106 -0
- package/dist/capture-v1/serialize.mjs +59 -0
- package/dist/capture-v1/transform.d.ts +19 -0
- package/dist/capture-v1/transform.d.ts.map +1 -0
- package/dist/capture-v1/transform.js +118 -0
- package/dist/capture-v1/transform.mjs +77 -0
- package/dist/capture-v1/types.d.ts +52 -0
- package/dist/capture-v1/types.d.ts.map +1 -0
- package/dist/capture-v1/types.js +44 -0
- package/dist/capture-v1/types.mjs +3 -0
- package/dist/capture-v1/uuid.d.ts +9 -0
- package/dist/capture-v1/uuid.d.ts.map +1 -0
- package/dist/capture-v1/uuid.js +75 -0
- package/dist/capture-v1/uuid.mjs +34 -0
- package/dist/featureFlagLocalEvaluation.d.ts +2 -0
- package/dist/featureFlagLocalEvaluation.d.ts.map +1 -1
- package/dist/featureFlagLocalEvaluation.js +38 -0
- package/dist/featureFlagLocalEvaluation.mjs +38 -0
- package/dist/featureFlagUtils.d.ts +7 -5
- package/dist/featureFlagUtils.d.ts.map +1 -1
- package/dist/featureFlagUtils.js +17 -11
- package/dist/featureFlagUtils.mjs +14 -11
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -16
- package/dist/index.mjs +1 -1
- package/dist/posthog-core-stateless.d.ts +44 -7
- package/dist/posthog-core-stateless.d.ts.map +1 -1
- package/dist/posthog-core-stateless.js +103 -24
- package/dist/posthog-core-stateless.mjs +103 -24
- package/dist/posthog-core.d.ts +138 -53
- package/dist/posthog-core.d.ts.map +1 -1
- package/dist/posthog-core.js +341 -115
- package/dist/posthog-core.mjs +338 -115
- package/dist/types.d.ts +33 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +25 -12
- package/dist/utils/index.mjs +1 -1
- package/dist/utils/json-utils.d.ts +11 -1
- package/dist/utils/json-utils.d.ts.map +1 -1
- package/dist/utils/json-utils.js +50 -2
- package/dist/utils/json-utils.mjs +46 -1
- package/package.json +13 -3
- package/src/capture-v1/compression.ts +50 -0
- package/src/capture-v1/errors.ts +91 -0
- package/src/capture-v1/index.ts +34 -0
- package/src/capture-v1/sender.ts +779 -0
- package/src/capture-v1/serialize.ts +113 -0
- package/src/capture-v1/transform.ts +135 -0
- package/src/capture-v1/types.ts +59 -0
- package/src/capture-v1/uuid.ts +53 -0
- package/src/featureFlagLocalEvaluation.ts +51 -0
- package/src/featureFlagUtils.ts +22 -10
- package/src/index.ts +1 -0
- package/src/posthog-core-stateless.ts +190 -35
- package/src/posthog-core.ts +615 -178
- package/src/types.ts +31 -1
- package/src/utils/index.ts +1 -1
- package/src/utils/json-utils.ts +64 -1
- package/src/error-tracking/chunk-ids.spec.ts +0 -77
- package/src/error-tracking/coercers/error-event-coercer.spec.ts +0 -142
- package/src/error-tracking/coercers/promise-rejection-event.spec.ts +0 -77
- package/src/error-tracking/coercers/string-coercer.spec.ts +0 -26
- package/src/error-tracking/error-properties-builder.aggregate-errors.spec.ts +0 -412
- package/src/error-tracking/error-properties-builder.coerce.spec.ts +0 -337
- package/src/error-tracking/error-properties-builder.metadata.spec.ts +0 -318
- package/src/error-tracking/error-properties-builder.parse.spec.ts +0 -37
- package/src/error-tracking/exception-steps.spec.ts +0 -115
- package/src/error-tracking/parsers/base.spec.ts +0 -156
- package/src/error-tracking/parsers/index.spec.ts +0 -270
- package/src/error-tracking/parsers/node.spec.ts +0 -34
- package/src/error-tracking/release.spec.ts +0 -34
- package/src/logs/index.spec.ts +0 -2421
- package/src/logs/logs-utils.spec.ts +0 -266
- package/src/metrics/index.spec.ts +0 -1249
- package/src/surveys/activation.spec.ts +0 -61
- package/src/surveys/events.spec.ts +0 -183
- package/src/surveys/keys.spec.ts +0 -29
- package/src/surveys/property-matching.spec.ts +0 -539
- package/src/surveys/shuffling.spec.ts +0 -51
- package/src/surveys/translations.spec.ts +0 -274
- package/src/surveys/validation.spec.ts +0 -168
- package/src/traces/config.spec.ts +0 -271
- package/src/traces/ids.spec.ts +0 -115
- package/src/traces/index.spec.ts +0 -3287
- package/src/traces/live-spans.spec.ts +0 -69
- package/src/traces/otlp.spec.ts +0 -368
- package/src/traces/span.spec.ts +0 -1137
- package/src/traces/traceparent.spec.ts +0 -184
- package/src/utils/bot-detection.spec.ts +0 -107
- package/src/utils/bucketed-rate-limiter.spec.ts +0 -327
- package/src/utils/json-utils.spec.ts +0 -115
- package/src/utils/named-error.spec.ts +0 -36
- package/src/utils/number-utils.spec.ts +0 -212
- package/src/utils/otlp-any-value.spec.ts +0 -376
- package/src/utils/otlp-resource.spec.ts +0 -171
- package/src/utils/promise-queue.spec.ts +0 -55
- package/src/utils/string-utils.spec.ts +0 -158
- package/src/utils/type-utils.spec.ts +0 -38
- package/src/utils/webview-app-utils.spec.ts +0 -118
- package/src/vendor/uuidv7.spec.ts +0 -26
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
-
import {
|
|
3
|
-
applySurveyTranslation,
|
|
4
|
-
detectSurveyLanguage,
|
|
5
|
-
findBestTranslationMatch,
|
|
6
|
-
getLanguageFromStoredPersonProperties,
|
|
7
|
-
} from './translations'
|
|
8
|
-
import { SurveyQuestionType, SurveyType } from '../types'
|
|
9
|
-
import type { Logger, Survey } from '../types'
|
|
10
|
-
|
|
11
|
-
const createBaseSurvey = (): Survey => ({
|
|
12
|
-
id: 'test-survey',
|
|
13
|
-
name: 'Test Survey',
|
|
14
|
-
description: 'Test Description',
|
|
15
|
-
type: SurveyType.Popover,
|
|
16
|
-
questions: [
|
|
17
|
-
{
|
|
18
|
-
type: SurveyQuestionType.Open,
|
|
19
|
-
question: 'What do you think?',
|
|
20
|
-
id: 'q1',
|
|
21
|
-
originalQuestionIndex: 0,
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
appearance: {
|
|
25
|
-
thankYouMessageHeader: 'Thank you!',
|
|
26
|
-
thankYouMessageDescription: 'We appreciate your feedback',
|
|
27
|
-
thankYouMessageCloseButtonText: 'Close',
|
|
28
|
-
},
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
describe('survey translations', () => {
|
|
32
|
-
describe('detectSurveyLanguage', () => {
|
|
33
|
-
it.each([
|
|
34
|
-
{
|
|
35
|
-
name: 'prioritizes override language',
|
|
36
|
-
input: {
|
|
37
|
-
overrideLanguage: 'de',
|
|
38
|
-
storedPersonProperties: { language: 'es' },
|
|
39
|
-
locale: 'fr',
|
|
40
|
-
},
|
|
41
|
-
expected: 'de',
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
name: 'uses person language when override is missing',
|
|
45
|
-
input: {
|
|
46
|
-
storedPersonProperties: { language: 'es' },
|
|
47
|
-
locale: 'fr',
|
|
48
|
-
},
|
|
49
|
-
expected: 'es',
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
name: 'falls back to locale',
|
|
53
|
-
input: {
|
|
54
|
-
storedPersonProperties: { some_other_property: 'value' },
|
|
55
|
-
locale: 'fr-CA',
|
|
56
|
-
},
|
|
57
|
-
expected: 'fr-CA',
|
|
58
|
-
},
|
|
59
|
-
{
|
|
60
|
-
name: 'returns null when no sources exist',
|
|
61
|
-
input: {
|
|
62
|
-
storedPersonProperties: { some_other_property: 'value' },
|
|
63
|
-
},
|
|
64
|
-
expected: null,
|
|
65
|
-
},
|
|
66
|
-
{
|
|
67
|
-
name: 'trims override language',
|
|
68
|
-
input: {
|
|
69
|
-
overrideLanguage: ' es ',
|
|
70
|
-
},
|
|
71
|
-
expected: 'es',
|
|
72
|
-
},
|
|
73
|
-
])('$name', ({ input, expected }) => {
|
|
74
|
-
expect(detectSurveyLanguage(input)).toBe(expected)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
it('reads language from stored person properties', () => {
|
|
78
|
-
expect(getLanguageFromStoredPersonProperties({ language: 'it' })).toBe('it')
|
|
79
|
-
expect(getLanguageFromStoredPersonProperties({ language: ' ' })).toBeNull()
|
|
80
|
-
expect(getLanguageFromStoredPersonProperties({})).toBeNull()
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('does not log language detection by default', () => {
|
|
84
|
-
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
|
85
|
-
|
|
86
|
-
try {
|
|
87
|
-
expect(detectSurveyLanguage({ locale: 'en-US' })).toBe('en-US')
|
|
88
|
-
expect(logSpy).not.toHaveBeenCalled()
|
|
89
|
-
} finally {
|
|
90
|
-
logSpy.mockRestore()
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
it('logs language detection through a provided logger', () => {
|
|
95
|
-
const logger: Logger = {
|
|
96
|
-
debug: vi.fn(),
|
|
97
|
-
info: vi.fn(),
|
|
98
|
-
warn: vi.fn(),
|
|
99
|
-
error: vi.fn(),
|
|
100
|
-
critical: vi.fn(),
|
|
101
|
-
createLogger: vi.fn(),
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
expect(detectSurveyLanguage({ locale: 'en-US' }, logger)).toBe('en-US')
|
|
105
|
-
expect(logger.info).toHaveBeenCalledWith('Using detected locale: en-US')
|
|
106
|
-
})
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
describe('findBestTranslationMatch', () => {
|
|
110
|
-
it('supports exact and base-language matches', () => {
|
|
111
|
-
expect(findBestTranslationMatch({ fr: {}, 'fr-CA': {} }, 'FR-ca')).toBe('fr-CA')
|
|
112
|
-
expect(findBestTranslationMatch({ fr: {} }, 'fr-CA')).toBe('fr')
|
|
113
|
-
expect(findBestTranslationMatch({ es: {} }, 'de')).toBeNull()
|
|
114
|
-
})
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
describe('applySurveyTranslation', () => {
|
|
118
|
-
it('returns original survey when no translations exist', () => {
|
|
119
|
-
const survey = createBaseSurvey()
|
|
120
|
-
const result = applySurveyTranslation(survey, 'fr')
|
|
121
|
-
|
|
122
|
-
expect(result.survey).toEqual(survey)
|
|
123
|
-
expect(result.matchedKey).toBeNull()
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
it('applies survey-level translations', () => {
|
|
127
|
-
const survey = createBaseSurvey()
|
|
128
|
-
survey.translations = {
|
|
129
|
-
fr: {
|
|
130
|
-
name: 'Enquete Test',
|
|
131
|
-
thankYouMessageHeader: 'Merci',
|
|
132
|
-
thankYouMessageDescription: 'Merci pour votre retour',
|
|
133
|
-
thankYouMessageCloseButtonText: 'Fermer',
|
|
134
|
-
},
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const result = applySurveyTranslation(survey, 'fr')
|
|
138
|
-
|
|
139
|
-
expect(result.survey.name).toBe('Enquete Test')
|
|
140
|
-
expect(result.survey.appearance?.thankYouMessageHeader).toBe('Merci')
|
|
141
|
-
expect(result.survey.appearance?.thankYouMessageDescription).toBe('Merci pour votre retour')
|
|
142
|
-
expect(result.survey.appearance?.thankYouMessageCloseButtonText).toBe('Fermer')
|
|
143
|
-
expect(result.matchedKey).toBe('fr')
|
|
144
|
-
})
|
|
145
|
-
|
|
146
|
-
it('applies translations for the submit and back button labels', () => {
|
|
147
|
-
const survey = createBaseSurvey()
|
|
148
|
-
survey.appearance = { ...survey.appearance, submitButtonText: 'Submit', backButtonText: 'Back' }
|
|
149
|
-
survey.translations = {
|
|
150
|
-
fr: {
|
|
151
|
-
submitButtonText: 'Envoyer',
|
|
152
|
-
backButtonText: 'Retour',
|
|
153
|
-
},
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const result = applySurveyTranslation(survey, 'fr')
|
|
157
|
-
|
|
158
|
-
expect(result.survey.appearance?.submitButtonText).toBe('Envoyer')
|
|
159
|
-
expect(result.survey.appearance?.backButtonText).toBe('Retour')
|
|
160
|
-
expect(result.matchedKey).toBe('fr')
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
it('applies translations for the intro screen fields', () => {
|
|
164
|
-
const survey = createBaseSurvey()
|
|
165
|
-
survey.appearance = {
|
|
166
|
-
...survey.appearance,
|
|
167
|
-
displayIntroScreen: true,
|
|
168
|
-
introScreenHeader: 'Welcome!',
|
|
169
|
-
introScreenDescription: 'Two quick questions.',
|
|
170
|
-
introScreenButtonText: 'Get started',
|
|
171
|
-
}
|
|
172
|
-
survey.translations = {
|
|
173
|
-
fr: {
|
|
174
|
-
introScreenHeader: 'Bienvenue !',
|
|
175
|
-
introScreenDescription: 'Deux questions rapides.',
|
|
176
|
-
introScreenButtonText: 'Commencer',
|
|
177
|
-
},
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
const result = applySurveyTranslation(survey, 'fr')
|
|
181
|
-
|
|
182
|
-
expect(result.survey.appearance?.introScreenHeader).toBe('Bienvenue !')
|
|
183
|
-
expect(result.survey.appearance?.introScreenDescription).toBe('Deux questions rapides.')
|
|
184
|
-
expect(result.survey.appearance?.introScreenButtonText).toBe('Commencer')
|
|
185
|
-
expect(result.survey.appearance?.displayIntroScreen).toBe(true)
|
|
186
|
-
expect(result.matchedKey).toBe('fr')
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
it('applies question-level translations', () => {
|
|
190
|
-
const survey = createBaseSurvey()
|
|
191
|
-
survey.questions = [
|
|
192
|
-
{
|
|
193
|
-
type: SurveyQuestionType.Rating,
|
|
194
|
-
question: 'How was it?',
|
|
195
|
-
id: 'q1',
|
|
196
|
-
originalQuestionIndex: 0,
|
|
197
|
-
display: 'number',
|
|
198
|
-
scale: 5,
|
|
199
|
-
lowerBoundLabel: 'Bad',
|
|
200
|
-
upperBoundLabel: 'Great',
|
|
201
|
-
translations: {
|
|
202
|
-
pt: {
|
|
203
|
-
question: 'Como foi?',
|
|
204
|
-
lowerBoundLabel: 'Ruim',
|
|
205
|
-
upperBoundLabel: 'Otimo',
|
|
206
|
-
},
|
|
207
|
-
},
|
|
208
|
-
},
|
|
209
|
-
{
|
|
210
|
-
type: SurveyQuestionType.MultipleChoice,
|
|
211
|
-
question: 'Pick one',
|
|
212
|
-
id: 'q2',
|
|
213
|
-
originalQuestionIndex: 1,
|
|
214
|
-
choices: ['One', 'Other'],
|
|
215
|
-
hasOpenChoice: true,
|
|
216
|
-
translations: {
|
|
217
|
-
pt: {
|
|
218
|
-
choices: ['Um', 'Outro'],
|
|
219
|
-
},
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
]
|
|
223
|
-
|
|
224
|
-
const result = applySurveyTranslation(survey, 'pt-BR')
|
|
225
|
-
|
|
226
|
-
expect(result.survey.questions[0].question).toBe('Como foi?')
|
|
227
|
-
expect('lowerBoundLabel' in result.survey.questions[0] && result.survey.questions[0].lowerBoundLabel).toBe('Ruim')
|
|
228
|
-
expect('upperBoundLabel' in result.survey.questions[0] && result.survey.questions[0].upperBoundLabel).toBe(
|
|
229
|
-
'Otimo'
|
|
230
|
-
)
|
|
231
|
-
expect('choices' in result.survey.questions[1] && result.survey.questions[1].choices).toEqual(['Um', 'Outro'])
|
|
232
|
-
expect(result.matchedKey).toBe('pt')
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
it('uses question-level match when there is no survey-level translation', () => {
|
|
236
|
-
const survey = createBaseSurvey()
|
|
237
|
-
survey.questions[0].translations = {
|
|
238
|
-
de: {
|
|
239
|
-
question: 'Was denkst du?',
|
|
240
|
-
},
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const result = applySurveyTranslation(survey, 'de')
|
|
244
|
-
|
|
245
|
-
expect(result.survey.questions[0].question).toBe('Was denkst du?')
|
|
246
|
-
expect(result.matchedKey).toBe('de')
|
|
247
|
-
})
|
|
248
|
-
|
|
249
|
-
it('preserves custom survey and question fields for shared consumers', () => {
|
|
250
|
-
const survey = {
|
|
251
|
-
name: 'Custom Survey',
|
|
252
|
-
customSurveyField: true,
|
|
253
|
-
questions: [
|
|
254
|
-
{
|
|
255
|
-
question: 'Pick one',
|
|
256
|
-
customQuestionField: 'native-renderer',
|
|
257
|
-
translations: {
|
|
258
|
-
fr: {
|
|
259
|
-
question: 'Choisissez une option',
|
|
260
|
-
},
|
|
261
|
-
},
|
|
262
|
-
},
|
|
263
|
-
],
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const result = applySurveyTranslation(survey, 'fr')
|
|
267
|
-
|
|
268
|
-
expect(result.survey.customSurveyField).toBe(true)
|
|
269
|
-
expect(result.survey.questions[0].question).toBe('Choisissez une option')
|
|
270
|
-
expect(result.survey.questions[0].customQuestionField).toBe('native-renderer')
|
|
271
|
-
expect(result.matchedKey).toBe('fr')
|
|
272
|
-
})
|
|
273
|
-
})
|
|
274
|
-
})
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import { getValidationError, getLengthFromRules, getRequirementsHint } from './validation'
|
|
2
|
-
import { SurveyValidationType, SurveyValidationRule } from '../types'
|
|
3
|
-
|
|
4
|
-
describe('getValidationError', () => {
|
|
5
|
-
describe('required field (trim fix)', () => {
|
|
6
|
-
it('returns error for empty string when required', () => {
|
|
7
|
-
expect(getValidationError('', undefined, false)).toBe('This field is required')
|
|
8
|
-
})
|
|
9
|
-
|
|
10
|
-
it('returns error for whitespace-only when required', () => {
|
|
11
|
-
expect(getValidationError(' ', undefined, false)).toBe('This field is required')
|
|
12
|
-
expect(getValidationError('\t\n', undefined, false)).toBe('This field is required')
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
it('returns false for valid content when required', () => {
|
|
16
|
-
expect(getValidationError('hello', undefined, false)).toBe(false)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('returns false for empty when optional', () => {
|
|
20
|
-
expect(getValidationError('', undefined, true)).toBe(false)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
it('returns false for whitespace when optional', () => {
|
|
24
|
-
expect(getValidationError(' ', undefined, true)).toBe(false)
|
|
25
|
-
})
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
describe('backwards compat - no validation rules', () => {
|
|
29
|
-
it('works when validation is undefined', () => {
|
|
30
|
-
expect(getValidationError('hello', undefined, false)).toBe(false)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('works when validation is empty array', () => {
|
|
34
|
-
expect(getValidationError('hello', [], false)).toBe(false)
|
|
35
|
-
})
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
describe('minLength validation', () => {
|
|
39
|
-
const rules: SurveyValidationRule[] = [{ type: SurveyValidationType.MinLength, value: 5 }]
|
|
40
|
-
|
|
41
|
-
it('returns error when too short', () => {
|
|
42
|
-
expect(getValidationError('abc', rules, false)).toBe('Please enter at least 5 characters')
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('returns false when exact length', () => {
|
|
46
|
-
expect(getValidationError('abcde', rules, false)).toBe(false)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('returns false when longer than min', () => {
|
|
50
|
-
expect(getValidationError('abcdefgh', rules, false)).toBe(false)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('uses custom error message if provided', () => {
|
|
54
|
-
const customRules: SurveyValidationRule[] = [
|
|
55
|
-
{ type: SurveyValidationType.MinLength, value: 5, errorMessage: 'Too short!' },
|
|
56
|
-
]
|
|
57
|
-
expect(getValidationError('abc', customRules, false)).toBe('Too short!')
|
|
58
|
-
})
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
describe('maxLength validation', () => {
|
|
62
|
-
const rules: SurveyValidationRule[] = [{ type: SurveyValidationType.MaxLength, value: 10 }]
|
|
63
|
-
|
|
64
|
-
it('returns error when too long', () => {
|
|
65
|
-
expect(getValidationError('12345678901', rules, false)).toBe('Please enter no more than 10 characters')
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
it('returns false when exact length', () => {
|
|
69
|
-
expect(getValidationError('1234567890', rules, false)).toBe(false)
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('returns false when shorter than max', () => {
|
|
73
|
-
expect(getValidationError('12345', rules, false)).toBe(false)
|
|
74
|
-
})
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
describe('minLength + maxLength combined', () => {
|
|
78
|
-
const rules: SurveyValidationRule[] = [
|
|
79
|
-
{ type: SurveyValidationType.MinLength, value: 5 },
|
|
80
|
-
{ type: SurveyValidationType.MaxLength, value: 10 },
|
|
81
|
-
]
|
|
82
|
-
|
|
83
|
-
it('fails when too short', () => {
|
|
84
|
-
expect(getValidationError('abc', rules, false)).toContain('at least 5')
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
it('fails when too long', () => {
|
|
88
|
-
expect(getValidationError('12345678901', rules, false)).toContain('no more than 10')
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('passes when in range', () => {
|
|
92
|
-
expect(getValidationError('1234567', rules, false)).toBe(false)
|
|
93
|
-
})
|
|
94
|
-
})
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
describe('getLengthFromRules', () => {
|
|
98
|
-
it('returns undefined when no rules', () => {
|
|
99
|
-
expect(getLengthFromRules(undefined, SurveyValidationType.MinLength)).toBeUndefined()
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
it('returns undefined when requested type not present', () => {
|
|
103
|
-
expect(
|
|
104
|
-
getLengthFromRules([{ type: SurveyValidationType.MaxLength, value: 10 }], SurveyValidationType.MinLength)
|
|
105
|
-
).toBeUndefined()
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
it('returns minLength value when present', () => {
|
|
109
|
-
expect(
|
|
110
|
-
getLengthFromRules([{ type: SurveyValidationType.MinLength, value: 5 }], SurveyValidationType.MinLength)
|
|
111
|
-
).toBe(5)
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
it('returns maxLength value when present', () => {
|
|
115
|
-
expect(
|
|
116
|
-
getLengthFromRules([{ type: SurveyValidationType.MaxLength, value: 100 }], SurveyValidationType.MaxLength)
|
|
117
|
-
).toBe(100)
|
|
118
|
-
})
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
describe('getRequirementsHint', () => {
|
|
122
|
-
describe('min=1 edge case', () => {
|
|
123
|
-
it('returns undefined for min=1 (redundant/useless hint)', () => {
|
|
124
|
-
expect(getRequirementsHint(1, undefined)).toBeUndefined()
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
it('returns undefined for min=1 even with maxLength', () => {
|
|
128
|
-
// min=1 is still redundant, but maxLength hint should show
|
|
129
|
-
expect(getRequirementsHint(1, 100)).toBe('Maximum 100 characters')
|
|
130
|
-
})
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
describe('minLength only', () => {
|
|
134
|
-
it('returns hint for min > 1', () => {
|
|
135
|
-
expect(getRequirementsHint(5, undefined)).toBe('Enter at least 5 characters')
|
|
136
|
-
})
|
|
137
|
-
|
|
138
|
-
it('uses singular for min=2 (edge case after min=1 is hidden)', () => {
|
|
139
|
-
expect(getRequirementsHint(2, undefined)).toBe('Enter at least 2 characters')
|
|
140
|
-
})
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
describe('maxLength only', () => {
|
|
144
|
-
it('returns hint for maxLength', () => {
|
|
145
|
-
expect(getRequirementsHint(undefined, 100)).toBe('Maximum 100 characters')
|
|
146
|
-
})
|
|
147
|
-
|
|
148
|
-
it('uses singular for max=1', () => {
|
|
149
|
-
expect(getRequirementsHint(undefined, 1)).toBe('Maximum 1 character')
|
|
150
|
-
})
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
describe('minLength + maxLength', () => {
|
|
154
|
-
it('returns combined hint when both present', () => {
|
|
155
|
-
expect(getRequirementsHint(5, 100)).toBe('Enter 5-100 characters')
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
it('shows only maxLength when min=1', () => {
|
|
159
|
-
expect(getRequirementsHint(1, 50)).toBe('Maximum 50 characters')
|
|
160
|
-
})
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
describe('no constraints', () => {
|
|
164
|
-
it('returns undefined when no min or max', () => {
|
|
165
|
-
expect(getRequirementsHint(undefined, undefined)).toBeUndefined()
|
|
166
|
-
})
|
|
167
|
-
})
|
|
168
|
-
})
|