File size: 1,757 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
51
52
53
54
55
56
57
58
59
60
61
/**
 * @jest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import FormTextInput from '../';

describe( '<FormTextInput />', () => {
	test( 'should add the provided class names', () => {
		render( <FormTextInput className="test" isError isValid /> );

		const input = screen.getByRole( 'textbox' );

		expect( input ).toHaveClass( 'test' );
		expect( input ).toHaveClass( 'is-error' );
		expect( input ).toHaveClass( 'is-valid' );
	} );

	test( 'should have form-text-input class name', () => {
		render( <FormTextInput /> );

		const input = screen.getByRole( 'textbox' );

		expect( input ).toHaveClass( 'form-text-input' );
	} );

	test( "should pass props aside from component's own to the input", () => {
		render( <FormTextInput placeholder="test placeholder" /> );

		const input = screen.getByRole( 'textbox' );

		expect( input ).toHaveAttribute( 'placeholder', 'test placeholder' );
	} );

	test( 'should call select if selectOnFocus is true', async () => {
		const value = 'arbitrary-value';
		render( <FormTextInput value={ value } selectOnFocus /> );

		const input = screen.getByRole( 'textbox' );

		await userEvent.click( input );

		const selected = input.value.substring( input.selectionStart, input.selectionEnd );

		expect( selected ).toBe( value );
	} );

	test( 'should not call select if selectOnFocus is false', async () => {
		const value = 'arbitrary-value';
		render( <FormTextInput value={ value } selectOnFocus={ false } /> );

		const input = screen.getByRole( 'textbox' );

		await userEvent.click( input );

		const selected = input.value.substring( input.selectionStart, input.selectionEnd );

		expect( selected ).toBe( '' );
	} );
} );