Skip to content

Commit

Permalink
[FSSDK-8184] override attribute bug fix of track method
Browse files Browse the repository at this point in the history
  • Loading branch information
junaed-optimizely committed Nov 6, 2024
1 parent f3185b7 commit 76240a9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
52 changes: 31 additions & 21 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1390,36 +1390,46 @@ describe('ReactSDKClient', () => {
expect(mockFn).toHaveBeenCalledTimes(0);
});

it('track works as expected', () => {
const mockFn = mockInnerClient.track as jest.Mock;

instance.track('evt1');

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user1', { foo: 'bar' }, undefined);
describe('track with different parameters', () => {
it('track with only event key, calls inner client with valid arguments', () => {
const mockFn = mockInnerClient.track as jest.Mock;
instance.track('evt1');

mockFn.mockReset();
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user1', { foo: 'bar' }, undefined);
});

instance.track('evt1', 'user2', { bar: 'baz' });
it('track with event key and overrided user id and attributes, calls inner client with valid arguments', () => {
const mockFn = mockInnerClient.track as jest.Mock;
instance.track('evt1', 'user2', { bar: 'baz' });

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user2', { bar: 'baz' }, undefined);
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user2', { bar: 'baz' }, undefined);
});

mockFn.mockReset();
it('track with event key and event tags, calls inner client with valid arguments', () => {
const mockFn = mockInnerClient.track as jest.Mock;
instance.track('evt1', { tagKey: 'tagVal' });

// Use pre-set user with event tags
instance.track('evt1', { tagKey: 'tagVal' });
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user1', { foo: 'bar' }, { tagKey: 'tagVal' });
});

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user1', { foo: 'bar' }, { tagKey: 'tagVal' });
it('track with event key, overrided user id and attributes and event tags, calls inner client with valid arguments', () => {
const mockFn = mockInnerClient.track as jest.Mock;
instance.track('evt1', 'user3', { bla: 'bla' }, { tagKey: 'tagVal' });

mockFn.mockReset();
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user3', { bla: 'bla' }, { tagKey: 'tagVal' });
});

// Use overrides with event tags
instance.track('evt1', 'user3', { bla: 'bla' }, { tagKey: 'tagVal' });
it.only('track with event key, tags, and overrided attributes, calls inner client with valid arguments', () => {
const mockFn = mockInnerClient.track as jest.Mock;
instance.track('evt1', { tagKey: 'tagVal' }, { bla: 'bla' });

expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user3', { bla: 'bla' }, { tagKey: 'tagVal' });
expect(mockFn).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith('evt1', 'user1', { bla: 'bla' }, { tagKey: 'tagVal' });
});
});
});

Expand Down
1 change: 0 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
if (typeof overrideUserId !== 'undefined' && typeof overrideUserId !== 'string') {
eventTags = overrideUserId;
overrideUserId = undefined;
overrideAttributes = undefined;
}

const user = this.getUserWithOverrides(overrideUserId, overrideAttributes);
Expand Down

0 comments on commit 76240a9

Please sign in to comment.