File size: 2,007 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * @jest-environment jsdom
 */

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FormPhoneInput } from '../';

const countriesList = [
	{
		code: 'US',
		name: 'United States (+1)',
		numeric_code: '+1',
		country_name: 'United States',
	},
	{
		code: 'AR',
		name: 'Argentina (+54)',
		numeric_code: '+54',
		country_name: 'Argentina',
	},
];

describe( 'FormPhoneInput', () => {
	const localizeProps = { translate: ( string ) => string };

	describe( 'getValue()', () => {
		test( 'should set country from props', () => {
			render(
				<FormPhoneInput
					countriesList={ countriesList }
					initialCountryCode={ countriesList[ 1 ].code }
					{ ...localizeProps }
				/>
			);

			const option = screen.getByRole( 'option', { selected: true } );

			expect( option.textContent ).toBe( countriesList[ 1 ].name );
		} );

		test( 'should set country to first element when not specified', () => {
			render( <FormPhoneInput countriesList={ countriesList } { ...localizeProps } /> );

			const option = screen.getByRole( 'option', { selected: true } );

			expect( option.textContent ).toBe( countriesList[ 0 ].name );
		} );

		test( 'should update country on change', async () => {
			const onChange = jest.fn();
			const { container } = render(
				<FormPhoneInput
					onChange={ onChange }
					countriesList={ countriesList }
					{ ...localizeProps }
				/>
			);

			const [ select ] = container.getElementsByClassName( 'form-country-select' );

			await userEvent.selectOptions( select, countriesList[ 1 ].code );

			expect( onChange ).toHaveBeenCalledWith(
				expect.objectContaining( { countryData: countriesList[ 1 ] } )
			);
		} );

		test( 'should have no country with empty countryList', () => {
			render( <FormPhoneInput countriesList={ [] } { ...localizeProps } /> );

			const option = screen.getByRole( 'option', { selected: true } );

			expect( option.textContent ).toBe( 'Loading…' );
		} );
	} );
} );