Skip to content

Commit

Permalink
Undefined options bug (#688)
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsilvestri authored Nov 21, 2022
1 parent 1822f61 commit c21734e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/flat-chefs-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-next': patch
---

Fixes issue where options object would not be properly assigned if properties arg was explicitly undefined
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ describe(resolveArguments, () => {
expect(options).toEqual({})
expect(cb).toEqual(fn)
})

test('options set if properties undefined', () => {
const [event, props, options] = resolveArguments(
'Test Event',
undefined,
{ context: { page: { path: '/custom' } } }
)

expect(event).toEqual('Test Event')
expect(props).toEqual({})
expect(options).toEqual({ context: { page: { path: '/custom' } } })
})
})

describe('event as object', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/browser/src/core/arguments-resolver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function resolveArguments(
: {}

let opts: Options = {}
if (isPlainObject(properties) && !isFunction(options)) {
if (!isFunction(options)) {
opts = options ?? {}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/browser/src/core/events/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ describe('Event Factory', () => {
expect(track.context).toEqual({ opt1: true })
})

test('sets context correctly if property arg is undefined', () => {
const track = factory.track('Order Completed', undefined, {
context: { page: { path: '/custom' } },
})

expect(track.context).toEqual({ page: { path: '/custom' } })
})

test('sets integrations', () => {
const track = factory.track(
'Order Completed',
Expand Down

0 comments on commit c21734e

Please sign in to comment.