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

/* eslint-disable wpcalypso/jsx-classname-namespace */
import { matchers } from '@emotion/jest';
import { ThemeProvider } from '@emotion/react';
import { render } from '@testing-library/react';
import FormFieldAnnotation from '../components/form-field-annotation';

// Add the custom matchers provided by '@emotion/jest'
expect.extend( matchers );

const theme = {
	colors: {
		textColor: 'blue',
		textColorLight: 'lightblue',
		borderColor: 'black',
		error: 'red',
	},
	weights: { bold: '15pt' },
};

describe( 'FormFieldAnnotation', () => {
	describe( 'with no error and not disabled', () => {
		let MyFormFieldAnnotation = null;

		beforeEach( () => {
			MyFormFieldAnnotation = () => (
				<ThemeProvider theme={ theme }>
					<FormFieldAnnotation
						formFieldId="fieldId"
						labelText="A Label"
						labelId="labelId"
						normalDescription="A description"
						descriptionId="descriptionId"
						errorDescription="An Error Message"
						isError={ false }
						isDisabled={ false }
						className="test__annotation_class"
					>
						<span id="fieldId">child contents</span>
					</FormFieldAnnotation>
				</ThemeProvider>
			);
		} );

		it( 'renders the description string', () => {
			const { getAllByText } = render( <MyFormFieldAnnotation /> );
			expect( getAllByText( 'A description' )[ 0 ] ).toBeInTheDocument();
		} );

		it( 'does not render the error string', () => {
			const { queryAllByText } = render( <MyFormFieldAnnotation /> );
			expect( queryAllByText( 'An Error Message' )[ 0 ] ).toBeUndefined();
		} );
	} );

	describe( 'with error and not disabled', () => {
		let MyFormFieldAnnotation = null;

		beforeEach( () => {
			MyFormFieldAnnotation = () => (
				<ThemeProvider theme={ theme }>
					<FormFieldAnnotation
						formFieldId="fieldId"
						labelText="A Label"
						labelId="labelId"
						normalDescription="A description"
						descriptionId="descriptionId"
						errorDescription="An Error Message"
						isError="true"
						isDisabled="false"
						className="test__annotation_class"
					>
						<span id="fieldId">child contents</span>
					</FormFieldAnnotation>
				</ThemeProvider>
			);
		} );

		it( 'does not render the description string', () => {
			const { queryAllByText } = render( <MyFormFieldAnnotation /> );
			expect( queryAllByText( 'A description' )[ 0 ] ).toBeUndefined();
		} );

		it( 'renders the error string', () => {
			const { getAllByText } = render( <MyFormFieldAnnotation /> );
			expect( getAllByText( 'An Error Message' )[ 0 ] ).toBeInTheDocument();
		} );
	} );
} );