File size: 2,113 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
/**
 * @jest-environment jsdom
 */
import { render, screen } from '@testing-library/react';
import PluginAction from '../plugin-action';

jest.mock( 'calypso/components/info-popover', () =>
	require( 'calypso/components/empty-component' )
);

jest.mock( '@wordpress/components', () => ( {
	ToggleControl: () => <input type="checkbox" data-testid="toggle-control" />,
} ) );

describe( 'PluginAction', () => {
	describe( 'rendering with form toggle', () => {
		test( 'should have plugin-action class', () => {
			const { container } = render( <PluginAction /> );
			expect( container.firstChild ).toHaveClass( 'plugin-action' );
		} );

		test( 'should render compact form toggle when no children passed', () => {
			render( <PluginAction /> );
			const toggle = screen.queryByRole( 'checkbox' );
			expect( toggle ).toBeInTheDocument();
		} );

		test( 'should render a plugin action label alongside children', () => {
			render(
				<PluginAction label="plugin-action-label">
					<span data-testid="plugin-action-children" />
				</PluginAction>
			);

			const label = screen.queryByText( 'plugin-action-label' );
			const children = screen.queryByTestId( 'plugin-action-children' );

			expect( label ).toBeInTheDocument();
			expect( label ).toHaveClass( 'plugin-action__label-text' );

			expect( children ).toBeInTheDocument();
			expect( children.parentNode ).toHaveClass( 'plugin-action__children' );
		} );
	} );

	describe( 'rendering children', () => {
		test( 'should not render a form toggle when children exist', () => {
			render(
				<PluginAction>
					<span />
				</PluginAction>
			);

			const toggle = screen.queryByTestId( 'toggle-control' );
			expect( toggle ).not.toBeInTheDocument();
		} );

		test( 'should render child within plugin-action__children container', () => {
			render(
				<PluginAction>
					<span data-testid="plugin-action-children" />
				</PluginAction>
			);

			const children = screen.getByTestId( 'plugin-action-children' );

			expect( children ).toBeInTheDocument();
			expect( children.parentNode ).toHaveClass( 'plugin-action__children' );
		} );
	} );
} );