Skip to content

Commit eb7cc73

Browse files
committed
refactor arg resolver on page to address edge cases
1 parent 7ac9cc7 commit eb7cc73

File tree

3 files changed

+120
-44
lines changed

3 files changed

+120
-44
lines changed

.changeset/fast-lions-kick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Add support for more argument resolver edge cases

packages/browser/src/core/arguments-resolver/__tests__/index.test.ts

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ describe(resolveArguments, () => {
194194
})
195195
})
196196

197-
describe(resolvePageArguments, () => {
197+
describe('resolvePageArguments - analytics.page([category], [name], [properties], [options], [callback])', () => {
198198
test('empty strings ("", "", null, { integrations })', () => {
199199
const [category, name, properties, options] = resolvePageArguments(
200200
'',
@@ -221,6 +221,36 @@ describe(resolvePageArguments, () => {
221221
})
222222
})
223223

224+
it('should accept (category, name)', () => {
225+
const [category, name, properties, options, cb] = resolvePageArguments(
226+
'category',
227+
'name'
228+
)
229+
230+
expect(name).toEqual('name')
231+
expect(category).toEqual('category')
232+
expect(properties).toEqual({})
233+
expect(options).toEqual({})
234+
expect(cb).toEqual(undefined)
235+
})
236+
237+
it('should accept (category, name, properties, options, cb)', () => {
238+
const fn = jest.fn()
239+
const [category, name, properties, options, cb] = resolvePageArguments(
240+
'foo',
241+
'name',
242+
bananaPhone,
243+
baseOptions,
244+
fn
245+
)
246+
247+
expect(category).toEqual('foo')
248+
expect(name).toEqual('name')
249+
expect(properties).toEqual(bananaPhone)
250+
expect(options).toEqual(baseOptions)
251+
expect(cb).toEqual(fn)
252+
})
253+
224254
test('should accept (category, name, properties, callback)', () => {
225255
const fn = jest.fn()
226256
const [category, name, properties, options, cb] = resolvePageArguments(
@@ -254,52 +284,48 @@ describe(resolvePageArguments, () => {
254284
expect(options).toEqual({})
255285
})
256286

257-
it('should accept (name, properties, options, callback)', () => {
258-
const fn = jest.fn()
287+
it('should accept (name, properties)', () => {
259288
const [category, name, properties, options, cb] = resolvePageArguments(
260289
'name',
261-
bananaPhone,
262-
baseOptions,
263-
fn
290+
bananaPhone
264291
)
265292

266-
expect(category).toEqual(null)
267293
expect(name).toEqual('name')
294+
expect(category).toEqual(null)
268295
expect(properties).toEqual(bananaPhone)
269-
expect(options).toEqual(baseOptions)
270-
expect(cb).toEqual(fn)
296+
expect(options).toEqual({})
297+
expect(cb).toEqual(undefined)
271298
})
272299

273-
it('should accept (name, properties, callback)', () => {
300+
it('should accept (name, properties, options, callback)', () => {
274301
const fn = jest.fn()
275302
const [category, name, properties, options, cb] = resolvePageArguments(
276303
'name',
277304
bananaPhone,
305+
baseOptions,
278306
fn
279307
)
280308

281309
expect(category).toEqual(null)
282310
expect(name).toEqual('name')
283311
expect(properties).toEqual(bananaPhone)
312+
expect(options).toEqual(baseOptions)
284313
expect(cb).toEqual(fn)
285-
expect(options).toEqual({})
286314
})
287315

288-
it('should accept (category, name, properties, options, cb)', () => {
316+
it('should accept (name, properties, callback)', () => {
289317
const fn = jest.fn()
290318
const [category, name, properties, options, cb] = resolvePageArguments(
291-
'foo',
292319
'name',
293320
bananaPhone,
294-
baseOptions,
295321
fn
296322
)
297323

298-
expect(category).toEqual('foo')
324+
expect(category).toEqual(null)
299325
expect(name).toEqual('name')
300326
expect(properties).toEqual(bananaPhone)
301-
expect(options).toEqual(baseOptions)
302327
expect(cb).toEqual(fn)
328+
expect(options).toEqual({})
303329
})
304330

305331
it('should accept (name, callback)', () => {
@@ -348,7 +374,7 @@ describe(resolvePageArguments, () => {
348374
expect(category).toEqual(null)
349375
})
350376

351-
test('should accept (category = null, name, properties, options, callback)', () => {
377+
test('should accept (null, name, properties, options, callback)', () => {
352378
const fn = jest.fn()
353379
const [category, name, properties, options, cb] = resolvePageArguments(
354380
null,
@@ -365,7 +391,7 @@ describe(resolvePageArguments, () => {
365391
expect(cb).toEqual(fn)
366392
})
367393

368-
test('should accept (category, name = null, properties, options, callback)', () => {
394+
test('should accept (category, null, properties, options, callback)', () => {
369395
const fn = jest.fn()
370396
const [category, name, properties, options, cb] = resolvePageArguments(
371397
'category',
@@ -376,12 +402,43 @@ describe(resolvePageArguments, () => {
376402
)
377403

378404
expect(category).toEqual('category')
379-
expect(name).toEqual('name')
405+
expect(name).toEqual(null)
380406
expect(properties).toEqual(bananaPhone)
381407
expect(options).toEqual(baseOptions)
382408
expect(cb).toEqual(fn)
383409
})
384410

411+
test('should accept (category, undefined, properties, options, callback)', () => {
412+
const fn = jest.fn()
413+
const [category, name, properties, options, cb] = resolvePageArguments(
414+
'category',
415+
undefined,
416+
bananaPhone,
417+
baseOptions,
418+
fn
419+
)
420+
421+
expect(category).toEqual('category')
422+
expect(name).toEqual(undefined)
423+
expect(properties).toEqual(bananaPhone)
424+
expect(options).toEqual(baseOptions)
425+
expect(cb).toEqual(fn)
426+
})
427+
428+
test('should accept (null, null, properties)', () => {
429+
const [category, name, properties, options, cb] = resolvePageArguments(
430+
null,
431+
null,
432+
bananaPhone
433+
)
434+
435+
expect(category).toEqual(null)
436+
expect(name).toEqual(null)
437+
expect(properties).toEqual(bananaPhone)
438+
expect(options).toEqual({})
439+
expect(cb).toEqual(undefined)
440+
})
441+
385442
test('should accept (null, null, properties, options, callback)', () => {
386443
const fn = jest.fn()
387444
const [category, name, properties, options, cb] = resolvePageArguments(
@@ -442,6 +499,16 @@ describe(resolvePageArguments, () => {
442499
expect(options).toEqual({})
443500
expect(cb).toEqual(fn)
444501
})
502+
503+
test('should accept (null, null, null, null, callback)', () => {
504+
const fn = jest.fn()
505+
const [category, name, properties, options, cb] = resolvePageArguments(fn)
506+
expect(category).toEqual(null)
507+
expect(name).toEqual(null)
508+
expect(properties).toEqual({})
509+
expect(options).toEqual({})
510+
expect(cb).toEqual(fn)
511+
})
445512
})
446513

447514
describe(resolveUserArguments, () => {

packages/browser/src/core/arguments-resolver/index.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,12 @@ export function resolvePageArguments(
7070
Options,
7171
Callback | undefined
7272
] {
73-
let resolvedProperties: EventProperties
74-
let resolvedOptions: Options
7573
let resolvedCategory: string | undefined | null = null
7674
let resolvedName: string | undefined | null = null
75+
let resolvedProperties: EventProperties
76+
let resolvedOptions: Options
7777
const args = [category, name, properties, options, callback]
7878

79-
// handle:
80-
// - analytics.page('name')
81-
// - analytics.page('category', 'name')
82-
const strings = args.filter(isString)
83-
if (strings.length === 1) {
84-
resolvedCategory = null
85-
resolvedName = strings[0]
86-
} else if (strings.length === 2) {
87-
if (typeof args[0] === 'string') {
88-
resolvedCategory = args[0]
89-
}
90-
if (typeof args[1] === 'string') {
91-
resolvedName = args[1]
92-
}
93-
}
94-
95-
// handle: analytics.page('category', 'name', properties, options, callback)
9679
const resolvedCallback = args.find(isFunction) as Callback | undefined
9780

9881
// handle:
@@ -106,22 +89,43 @@ export function resolvePageArguments(
10689
// - analytics.page('category', 'name', properties, options, callback)
10790
// - analytics.page('category', 'name', callback)
10891
// - analytics.page(callback), etc
109-
args.forEach((obj, argIdx) => {
110-
if (isPlainObject(obj)) {
92+
args.forEach((arg, argIdx) => {
93+
if (isString(arg)) {
11194
if (argIdx === 0) {
112-
resolvedProperties = obj
95+
if (isString(arg)) {
96+
resolvedName = arg
97+
}
98+
}
99+
if (argIdx === 1) {
100+
resolvedCategory = args[0] as string | undefined | null
101+
resolvedName = arg
102+
}
103+
}
104+
105+
if (isPlainObject(arg)) {
106+
if (argIdx === 0) {
107+
resolvedProperties = arg
113108
}
114109

115110
if (argIdx === 1 || argIdx == 2) {
116111
if (isNil(resolvedProperties)) {
117-
resolvedProperties = obj
112+
resolvedProperties = arg
118113
} else {
119-
resolvedOptions = obj
114+
resolvedOptions = arg
120115
}
121116
}
122117

123118
if (argIdx === 3) {
124-
resolvedOptions = obj
119+
resolvedOptions = arg
120+
}
121+
}
122+
123+
if (isNil(arg)) {
124+
if (argIdx === 1) {
125+
if (isString(args[0])) {
126+
resolvedName = arg
127+
resolvedCategory = args[0]
128+
}
125129
}
126130
}
127131
})

0 commit comments

Comments
 (0)