Skip to content

Commit d0f425b

Browse files
committed
fix argument resolver null issue
1 parent 7c24459 commit d0f425b

File tree

2 files changed

+122
-33
lines changed

2 files changed

+122
-33
lines changed

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

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

197197
describe(resolvePageArguments, () => {
198-
test('should accept (category, name, properties, options, callback)', () => {
199-
const fn = jest.fn()
200-
const [category, name, properties, options, cb] = resolvePageArguments(
201-
'category',
202-
'name',
203-
bananaPhone,
204-
baseOptions,
205-
fn
206-
)
207-
208-
expect(category).toEqual('category')
209-
expect(name).toEqual('name')
210-
expect(properties).toEqual(bananaPhone)
211-
expect(options).toEqual(baseOptions)
212-
expect(cb).toEqual(fn)
213-
})
214-
215-
test('empty strings ("", "", "", { integrations })', () => {
198+
test('empty strings ("", "", null, { integrations })', () => {
216199
const [category, name, properties, options] = resolvePageArguments(
217200
'',
218201
'',
@@ -302,6 +285,23 @@ describe(resolvePageArguments, () => {
302285
expect(options).toEqual({})
303286
})
304287

288+
it('should accept (category, name, properties, options, cb)', () => {
289+
const fn = jest.fn()
290+
const [category, name, properties, options, cb] = resolvePageArguments(
291+
'foo',
292+
'name',
293+
bananaPhone,
294+
baseOptions,
295+
fn
296+
)
297+
298+
expect(category).toEqual('foo')
299+
expect(name).toEqual('name')
300+
expect(properties).toEqual(bananaPhone)
301+
expect(options).toEqual(baseOptions)
302+
expect(cb).toEqual(fn)
303+
})
304+
305305
it('should accept (name, callback)', () => {
306306
const fn = jest.fn()
307307
const [category, name, properties, options, cb] = resolvePageArguments(
@@ -347,6 +347,74 @@ describe(resolvePageArguments, () => {
347347
expect(name).toEqual(null)
348348
expect(category).toEqual(null)
349349
})
350+
351+
test('should accept (category = null, name, properties, options, callback)', () => {
352+
const fn = jest.fn()
353+
const [category, name, properties, options, cb] = resolvePageArguments(
354+
null,
355+
'name',
356+
bananaPhone,
357+
baseOptions,
358+
fn
359+
)
360+
361+
expect(category).toEqual(null)
362+
expect(name).toEqual('name')
363+
expect(properties).toEqual(bananaPhone)
364+
expect(options).toEqual(baseOptions)
365+
expect(cb).toEqual(fn)
366+
})
367+
368+
test('should accept (null, null, properties, options, callback)', () => {
369+
const fn = jest.fn()
370+
const [category, name, properties, options, cb] = resolvePageArguments(
371+
null,
372+
null,
373+
bananaPhone,
374+
baseOptions,
375+
fn
376+
)
377+
378+
expect(category).toEqual(null)
379+
expect(name).toEqual(null)
380+
expect(properties).toEqual(bananaPhone)
381+
expect(options).toEqual(baseOptions)
382+
expect(cb).toEqual(fn)
383+
})
384+
385+
test('should accept (null, null, null, options, callback)', () => {
386+
const fn = jest.fn()
387+
const [category, name, properties, options, cb] = resolvePageArguments(
388+
null,
389+
null,
390+
null,
391+
baseOptions,
392+
fn
393+
)
394+
395+
expect(category).toEqual(null)
396+
expect(name).toEqual(null)
397+
expect(properties).toEqual({})
398+
expect(options).toEqual(baseOptions)
399+
expect(cb).toEqual(fn)
400+
})
401+
402+
test('should accept (undefined, undefined, undefined, options, callback)', () => {
403+
const fn = jest.fn()
404+
const [category, name, properties, options, cb] = resolvePageArguments(
405+
undefined,
406+
undefined,
407+
undefined,
408+
baseOptions,
409+
fn
410+
)
411+
412+
expect(category).toEqual(null)
413+
expect(name).toEqual(null)
414+
expect(properties).toEqual({})
415+
expect(options).toEqual(baseOptions)
416+
expect(cb).toEqual(fn)
417+
})
350418
})
351419

352420
describe(resolveUserArguments, () => {

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

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ export function resolveArguments(
5252
return [name, data, opts, cb]
5353
}
5454

55+
const isNil = (val: any): val is null | undefined =>
56+
val === null || val === undefined
57+
5558
/**
5659
* Helper for page, screen methods
5760
*/
5861
export function resolvePageArguments(
59-
category?: string | object,
60-
name?: string | object | Callback,
62+
category?: string | object | null,
63+
name?: string | object | Callback | null,
6164
properties?: EventProperties | Options | Callback | null,
6265
options?: Options | Callback,
6366
callback?: Callback
@@ -68,38 +71,56 @@ export function resolvePageArguments(
6871
Options,
6972
Callback | undefined
7073
] {
74+
let resolvedProperties: EventProperties
75+
let resolvedOptions: Options
7176
let resolvedCategory: string | undefined | null = null
7277
let resolvedName: string | undefined | null = null
7378
const args = [category, name, properties, options, callback]
7479

75-
const strings = args.filter(isString)
76-
if (strings[0] !== undefined && strings[1] !== undefined) {
77-
resolvedCategory = strings[0]
78-
resolvedName = strings[1]
80+
if (typeof args[0] === 'string') {
81+
resolvedCategory = args[0]
82+
}
83+
if (typeof args[1] === 'string') {
84+
resolvedName = args[1]
7985
}
8086

87+
// if there is just one string in the first two args, it's the name
88+
const strings = [args[0], args[1]].filter(isString)
8189
if (strings.length === 1) {
8290
resolvedCategory = null
8391
resolvedName = strings[0]
8492
}
8593

94+
// if there is any function, it's always the callback
8695
const resolvedCallback = args.find(isFunction) as Callback | undefined
8796

88-
const objects = args.filter((obj) => {
89-
if (resolvedName === null) {
90-
return isPlainObject(obj)
97+
args.forEach((obj, argIdx) => {
98+
if (isPlainObject(obj)) {
99+
if (argIdx === 0) {
100+
resolvedProperties = obj
101+
}
102+
if (argIdx === 1 || argIdx == 2) {
103+
if (isNil(resolvedProperties)) {
104+
resolvedProperties = obj
105+
} else {
106+
resolvedOptions = obj
107+
}
108+
}
109+
110+
// if it's the third argument and it's an object, it's always properties
111+
if (argIdx === 3) {
112+
resolvedOptions = obj
113+
}
91114
}
92-
return isPlainObject(obj) || obj === null
93-
}) as Array<JSONObject | null>
115+
})
94116

95-
const resolvedProperties = (objects[0] ?? {}) as EventProperties
96-
const resolvedOptions = (objects[1] ?? {}) as Options
117+
// if there is an object and it's the fourth argument, it's options
97118

98119
return [
99120
resolvedCategory,
100121
resolvedName,
101-
resolvedProperties,
102-
resolvedOptions,
122+
(resolvedProperties ??= {}),
123+
(resolvedOptions ??= {}),
103124
resolvedCallback,
104125
]
105126
}

0 commit comments

Comments
 (0)