File size: 1,659 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
/**
 * @jest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { HiddenInput } from '../hidden-input';

describe( 'HiddenInput', () => {
	let originalScroll;
	const noop = () => {};
	const defaultProps = {
		text: 'Love cannot be hidden.',
		label: 'Your name',
	};

	beforeAll( () => {
		originalScroll = window.scroll;
		window.scroll = noop;
	} );

	afterAll( () => {
		window.scroll = originalScroll;
	} );

	test( 'it should return expected elements with defaultProps and no props value', () => {
		render( <HiddenInput { ...defaultProps } /> );
		expect( screen.queryByText( 'Love cannot be hidden.' ) ).toBeVisible();
		expect( screen.queryByPlaceholderText( 'Your name' ) ).not.toBeInTheDocument();
	} );

	test( 'it should hide toggle link and render a full input field when the field value is not empty', () => {
		const fieldValue = 'Not empty';
		render( <HiddenInput { ...defaultProps } value={ fieldValue } /> );
		expect( screen.queryByText( 'Love cannot be hidden.' ) ).not.toBeInTheDocument();
		expect( screen.queryByPlaceholderText( 'Your name' ) ).toHaveValue( fieldValue );
	} );

	test( 'it should toggle input field when the toggle link is clicked', async () => {
		const { container } = render( <HiddenInput { ...defaultProps } /> );
		expect( screen.queryByText( 'Love cannot be hidden.' ) ).toBeVisible();
		await userEvent.click( container.getElementsByTagName( 'a' )[ 0 ] );
		expect( screen.queryByText( 'Love cannot be hidden.' ) ).not.toBeInTheDocument();
		expect( screen.queryByPlaceholderText( 'Your name' ) ).toBeVisible();
	} );
} );