Skip to content

Commit c21734e

Browse files
Undefined options bug (#688)
1 parent 1822f61 commit c21734e

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

.changeset/flat-chefs-bathe.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+
Fixes issue where options object would not be properly assigned if properties arg was explicitly undefined

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ describe(resolveArguments, () => {
101101
expect(options).toEqual({})
102102
expect(cb).toEqual(fn)
103103
})
104+
105+
test('options set if properties undefined', () => {
106+
const [event, props, options] = resolveArguments(
107+
'Test Event',
108+
undefined,
109+
{ context: { page: { path: '/custom' } } }
110+
)
111+
112+
expect(event).toEqual('Test Event')
113+
expect(props).toEqual({})
114+
expect(options).toEqual({ context: { page: { path: '/custom' } } })
115+
})
104116
})
105117

106118
describe('event as object', () => {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function resolveArguments(
3838
: {}
3939

4040
let opts: Options = {}
41-
if (isPlainObject(properties) && !isFunction(options)) {
41+
if (!isFunction(options)) {
4242
opts = options ?? {}
4343
}
4444

packages/browser/src/core/events/__tests__/index.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ describe('Event Factory', () => {
156156
expect(track.context).toEqual({ opt1: true })
157157
})
158158

159+
test('sets context correctly if property arg is undefined', () => {
160+
const track = factory.track('Order Completed', undefined, {
161+
context: { page: { path: '/custom' } },
162+
})
163+
164+
expect(track.context).toEqual({ page: { path: '/custom' } })
165+
})
166+
159167
test('sets integrations', () => {
160168
const track = factory.track(
161169
'Order Completed',

0 commit comments

Comments
 (0)