File size: 1,795 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
/**
 * @jest-environment jsdom
 */

import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import Suggestions from '..';

const defaultProps = {
	suggest: jest.fn(),
	suggestions: [
		{
			label: 'Apple',
		},
		{
			label: 'Pear',
		},
		{
			label: 'Orange',
		},
	],
};

describe( '<Suggestions>', () => {
	afterEach( () => {
		jest.resetAllMocks();
	} );

	test( 'render basic list of suggestions', () => {
		const { container } = render( <Suggestions { ...defaultProps } /> );

		expect( screen.getAllByRole( 'button' ) ).toHaveLength( 3 );

		expect( container.firstChild ).toMatchSnapshot();
	} );

	test( 'highlights the query inside the suggestions', () => {
		render( <Suggestions { ...defaultProps } query="LE" /> );

		const suggestions = screen.getAllByRole( 'button' );

		expect( suggestions[ 0 ] ).toHaveTextContent( 'Apple' );
		expect( suggestions[ 1 ] ).toHaveTextContent( 'Pear' );
		expect( suggestions[ 2 ] ).toHaveTextContent( 'Orange' );

		expect( suggestions[ 0 ] ).toHaveClass( 'has-highlight' );
		expect( suggestions[ 1 ] ).not.toHaveClass( 'has-highlight' );
		expect( suggestions[ 2 ] ).not.toHaveClass( 'has-highlight' );

		expect( screen.getByText( 'le' ) ).toHaveClass( 'is-emphasized' );
	} );

	test( 'uncategorized suggestions always appear first', () => {
		render(
			<Suggestions
				{ ...defaultProps }
				suggestions={ [ { label: 'Carrot', category: 'Vegetable' }, ...defaultProps.suggestions ] }
			/>
		);

		const suggestions = screen.getAllByRole( 'button' );

		expect( suggestions[ 0 ] ).toHaveTextContent( 'Apple' );
		expect( suggestions[ 1 ] ).toHaveTextContent( 'Pear' );
		expect( suggestions[ 2 ] ).toHaveTextContent( 'Orange' );
		expect( suggestions[ 3 ] ).toHaveTextContent( 'Carrot' );
	} );
} );