-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2fc1a58
commit 064b957
Showing
3 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({})], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |