File size: 3,879 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * @jest-environment jsdom
 */

import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { Button } from '..';
import Gridicon from '../../gridicon'; // eslint-disable-line no-restricted-imports

describe( 'Button', () => {
	describe( 'renders', () => {
		test( 'with modifiers', () => {
			render( <Button scary primary borderless compact /> );

			const button = screen.getByRole( 'button' );

			expect( button ).toHaveClass( 'is-compact' );
			expect( button ).toHaveClass( 'is-primary' );
			expect( button ).toHaveClass( 'is-scary' );
			expect( button ).toHaveClass( 'is-borderless' );
		} );

		test( 'without modifiers', () => {
			render( <Button /> );

			const button = screen.getByRole( 'button' );

			expect( button ).toHaveClass( 'button' );
			expect( button ).not.toHaveClass( 'is-compact' );
			expect( button ).not.toHaveClass( 'is-primary' );
			expect( button ).not.toHaveClass( 'is-scary' );
			expect( button ).not.toHaveClass( 'is-borderless' );
		} );

		test( 'disabled', () => {
			render( <Button disabled /> );

			expect( screen.getByRole( 'button' ) ).toBeDisabled();
		} );

		test( 'with child', () => {
			const icon = <Gridicon size={ 18 } icon="arrow-left" />;

			const { container, rerender } = render( icon );

			rerender( <Button>{ icon }</Button> );

			expect( screen.getByRole( 'button' ) ).toContainElement( container.firstChild );
		} );
	} );

	describe( 'with href prop', () => {
		test( 'renders as a link', () => {
			render( <Button href="https://wordpress.com/" /> );

			expect( screen.getByRole( 'link' ) ).toHaveAttribute( 'href', 'https://wordpress.com/' );
		} );

		test( 'ignores type prop and renders a link without type attribute', () => {
			render( <Button href="https://wordpress.com/" type="submit" /> );

			expect( screen.getByRole( 'link' ) ).not.toHaveAttribute( 'type' );
		} );

		test( 'including target and rel props renders a link with target and rel attributes', () => {
			render( <Button href="https://wordpress.com/" target="_blank" rel="noopener noreferrer" /> );

			const button = screen.getByRole( 'link' );

			expect( button ).toHaveAttribute( 'target', '_blank' );
			expect( button ).toHaveAttribute( 'rel', expect.stringMatching( /\bnoopener\b/ ) );
			expect( button ).toHaveAttribute( 'rel', expect.stringMatching( /\bnoreferrer\b/ ) );
		} );

		test( 'adds noopener noreferrer rel if target is specified', () => {
			render( <Button href="https://wordpress.com/" target="_blank" /> );

			const button = screen.getByRole( 'link' );

			expect( button ).toHaveAttribute( 'target', '_blank' );
			expect( button ).toHaveAttribute( 'rel', expect.stringMatching( /\bnoopener\b/ ) );
			expect( button ).toHaveAttribute( 'rel', expect.stringMatching( /\bnoreferrer\b/ ) );
		} );
	} );

	describe( 'without href prop', () => {
		test( 'renders as a button', () => {
			render( <Button target="_blank" rel="noopener noreferrer" /> );

			expect( screen.getByRole( 'button' ) ).not.toHaveAttribute( 'href' );
		} );

		test( 'renders button with type attribute set to "button" by default', () => {
			render( <Button target="_blank" rel="noopener noreferrer" /> );

			expect( screen.getByRole( 'button' ) ).toHaveAttribute( 'type', 'button' );
		} );

		test( 'renders button with type attribute set to type prop if specified', () => {
			const typeProp = 'submit';

			render( <Button target="_blank" rel="noopener noreferrer" type={ typeProp } /> );

			expect( screen.getByRole( 'button' ) ).toHaveAttribute( 'type', typeProp );
		} );

		test( 'renders button without rel and target attributes', () => {
			render( <Button target="_blank" rel="noopener noreferrer" /> );

			const button = screen.getByRole( 'button' );

			expect( button ).not.toHaveAttribute( 'target' );
			expect( button ).not.toHaveAttribute( 'rel' );
		} );
	} );
} );