Skip to content

Commit

Permalink
add track callback test
Browse files Browse the repository at this point in the history
  • Loading branch information
KurtGokhan committed Dec 24, 2023
1 parent 2fc1a58 commit 064b957
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
4 changes: 1 addition & 3 deletions src/lib/track-callback.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export function createTrackCallback<TBase extends ReactOnBase = ReactOn>(useTrac
const resolvedName = (name ?? String(callback)) as TType;
const track = useTrack();

const trackFn = useCurrentCallback((...args: any[]) =>
track({ type: resolvedName, values: props.values, args }),
);
const trackFn = useCurrentCallback((...args: any[]) => track({ type: resolvedName, values: props.values, args }));

const originalCallback = children.props[callback];
const handle = useCallback(
Expand Down
50 changes: 38 additions & 12 deletions tests/base.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
import { render } from '@testing-library/react';
import { TrackEvent, Tracking, TrackingHandler, TrackingHandlerFn } from 'src';
import { TrackCallback, TrackEvent } from 'src';
import { createTrackingWrapper } from './utils';

describe('react-on', () => {
test('should work', () => {
const spy = jest.fn<void, Parameters<TrackingHandlerFn>>();
describe('track-event', () => {
test('should work', () => {
const { wrapper, spy } = createTrackingWrapper({ values: { a: 5, b: 't' } });

const { getByTestId } = render(<Tracking values={{ a: 5, b: 't' }}>
<TrackingHandler onHandle={spy}>
const { getByTestId } = render(
<TrackEvent event='click'>
<button data-testid="test">Click me</button>
</TrackEvent>
</TrackingHandler>
</Tracking>)
<button data-testid='button'>Click me</button>
</TrackEvent>,
{ wrapper },
);

const button = getByTestId('test');
button.click();
getByTestId('button').click();

expect(spy).toHaveBeenCalledWith({ values: { a: 5, b: 't' }, type: 'click', args: [expect.objectContaining({})] });
expect(spy).toHaveBeenCalledWith({
values: { a: 5, b: 't' },
type: 'click',
args: [expect.objectContaining({})],
});
});
});

describe('track-callback', () => {
test('should work', () => {
const { wrapper, spy } = createTrackingWrapper({ values: { a: 5, b: 't' } });

const { getByTestId } = render(
<TrackCallback callback='onClick'>
<button data-testid='button'>Click me</button>
</TrackCallback>,
{ wrapper },
);

getByTestId('button').click();

expect(spy).toHaveBeenCalledWith({
values: { a: 5, b: 't' },
type: 'onClick',
args: [expect.objectContaining({})],
});
});
});
});
18 changes: 18 additions & 0 deletions tests/utils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Tracking, TrackingHandler, TrackingHandlerFn, TrackingProps } from 'src';
export * as userEvent from '@testing-library/user-event';

export function createTrackingWrapper(defaultProps: TrackingProps = {}) {
const spy = jest.fn<void, Parameters<TrackingHandlerFn>>();

const wrapper = (baseProps: TrackingProps) => {
const { children, ...props } = { ...defaultProps, ...baseProps };

return (
<Tracking {...props}>
<TrackingHandler onHandle={spy}>{children}</TrackingHandler>
</Tracking>
);
};

return { wrapper, spy };
}

0 comments on commit 064b957

Please sign in to comment.