14
14
*/
15
15
16
16
import * as React from 'react' ;
17
- import { render , fireEvent , screen } from '@testing-library/react' ;
17
+ import { render } from '@testing-library/react' ;
18
18
19
19
import { TrackingRoot } from '../../components/TrackingRoot' ;
20
20
import { Events } from '../../types' ;
21
21
22
22
import { usePageViewTrigger } from './usePageViewTrigger' ;
23
23
24
- const DispatchButton = ( ) => {
25
- const dispatch = usePageViewTrigger ( ) ;
24
+ const DispatchPage = ( { mockDispatchData = { } } ) => {
25
+ const dispatchPageView = usePageViewTrigger ( ) ;
26
26
27
- return (
28
- < button data-testid = "dispatch-btn" onClick = { ( ) => dispatch ( ) } >
29
- Dispatch button
30
- </ button >
31
- ) ;
27
+ React . useEffect ( ( ) => {
28
+ dispatchPageView ( mockDispatchData ) ;
29
+ // eslint-disable-next-line react-hooks/exhaustive-deps
30
+ } , [ ] ) ;
31
+
32
+ return < div > Dispatch page</ div > ;
32
33
} ;
33
34
34
35
describe ( 'usePageViewTrigger' , ( ) => {
35
36
it ( 'should provide a dispatch function that contains the pageView event' , ( ) => {
36
37
const dispatch = jest . fn ( ) ;
37
38
const app = 'test-app-hook' ;
38
- const btn = 'dispatch-btn' ;
39
39
40
40
const expected = {
41
41
app,
@@ -46,11 +46,33 @@ describe('usePageViewTrigger', () => {
46
46
47
47
render (
48
48
< TrackingRoot name = { app } onDispatch = { dispatch } >
49
- < DispatchButton />
49
+ < DispatchPage />
50
50
</ TrackingRoot > ,
51
51
) ;
52
52
53
- fireEvent . click ( screen . getByTestId ( btn ) ) ;
53
+ expect ( dispatch ) . toHaveBeenCalledWith ( expected ) ;
54
+ } ) ;
55
+
56
+ it ( 'should provide a dispatch function that contains the pageView event with optional custom parameters' , ( ) => {
57
+ const dispatch = jest . fn ( ) ;
58
+ const app = 'test-app-hook' ;
59
+ const customParameters = {
60
+ isConsentUpdate : true ,
61
+ } ;
62
+
63
+ const expected = {
64
+ app,
65
+ event : Events . pageView ,
66
+ elementTree : [ ] ,
67
+ timestamp : expect . any ( Number ) ,
68
+ customParameters,
69
+ } ;
70
+
71
+ render (
72
+ < TrackingRoot name = { app } onDispatch = { dispatch } >
73
+ < DispatchPage mockDispatchData = { { customParameters } } />
74
+ </ TrackingRoot > ,
75
+ ) ;
54
76
55
77
expect ( dispatch ) . toHaveBeenCalledWith ( expected ) ;
56
78
} ) ;
0 commit comments