File size: 1,598 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
 * @jest-environment jsdom
 */
import { act, renderHook } from '@testing-library/react';
import { useHandleClickLink } from '../use-handle-click-link';
import { useRecordTracksEventWithUserIsDevAccount } from '../use-record-tracks-event-with-user-is-dev-account';

jest.mock( '../use-record-tracks-event-with-user-is-dev-account', () => ( {
	useRecordTracksEventWithUserIsDevAccount: jest.fn(),
} ) );

describe( 'useHandleClickLink', () => {
	let mockRecordTracksEventWithUserIsDevAccount;

	const tracksHandle = 'calypso_me_developer_learn_more';

	beforeAll( () => {
		mockRecordTracksEventWithUserIsDevAccount = jest.fn();
		useRecordTracksEventWithUserIsDevAccount.mockReturnValue(
			mockRecordTracksEventWithUserIsDevAccount
		);
	} );

	describe( 'ensure track events report correct property', () => {
		it( 'should report element id when defined', () => {
			const { result } = renderHook( () => useHandleClickLink() );
			const elementId = 'my-feature';
			const event = { currentTarget: { id: elementId } };

			act( () => result.current( event ) );

			expect( mockRecordTracksEventWithUserIsDevAccount ).toHaveBeenCalledWith( tracksHandle, {
				feature: elementId,
			} );
		} );

		it( 'should report href when element id is not defined', () => {
			const { result } = renderHook( () => useHandleClickLink() );
			const href = 'https://wordpress.com';
			const event = { currentTarget: { href } };

			act( () => result.current( event ) );

			expect( mockRecordTracksEventWithUserIsDevAccount ).toHaveBeenCalledWith( tracksHandle, {
				feature: href,
			} );
		} );
	} );
} );