File size: 5,049 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import ShallowRenderer from 'react-test-renderer/shallow';
import { CountedTextarea } from '../';

describe( 'index', () => {
	let renderer;

	beforeEach( () => {
		renderer = new ShallowRenderer();
	} );

	test( 'should render the character count of the passed value', () => {
		renderer.render( <CountedTextarea value="Hello World!" /> );
		const result = renderer.getRenderOutput();

		expect( result.props.className ).toEqual( 'counted-textarea' );
		expect( result.props.children ).toHaveLength( 2 );
		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '12 characters' );
	} );

	test( 'should render warning styles when the acceptable length is exceeded', () => {
		renderer.render( <CountedTextarea value="Hello World!" acceptableLength={ 10 } /> );
		const result = renderer.getRenderOutput();

		expect( result.props.className ).toEqual( 'counted-textarea is-exceeding-acceptable-length' );
	} );

	test( 'should apply className to the wrapper element', () => {
		renderer.render( <CountedTextarea value="Hello World!" className="custom-class" /> );
		const result = renderer.getRenderOutput();

		expect( result.props.className ).toEqual( 'counted-textarea custom-class' );
	} );

	test( 'should pass props to the child textarea', () => {
		const value = 'Hello World!';
		const placeholder = 'placeholder test';

		renderer.render(
			<CountedTextarea value={ value } className="custom-class" placeholder={ placeholder } />
		);
		const result = renderer.getRenderOutput();

		expect( result.props.children ).toHaveLength( 2 );
		expect( result.props.children[ 0 ].props.value ).toEqual( value );
		expect( result.props.children[ 0 ].props.placeholder ).toEqual( placeholder );
		expect( result.props.children[ 0 ].props.className ).toEqual( 'counted-textarea__input' );
	} );

	test( 'should not use the placeholder as the counted item if value is empty and countPlaceholderLength is not set', () => {
		const value = '';
		const placeholder = 'placeholder test';

		renderer.render(
			<CountedTextarea value={ value } className="custom-class" placeholder={ placeholder } />
		);
		const result = renderer.getRenderOutput();

		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '0 characters' );
	} );

	test( 'should use the placeholder as the counted item if value is empty and countPlaceholderLength is true', () => {
		const value = '';
		const placeholder = 'placeholder test';

		renderer.render(
			<CountedTextarea
				value={ value }
				className="custom-class"
				placeholder={ placeholder }
				countPlaceholderLength
			/>
		);
		const result = renderer.getRenderOutput();

		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '16 characters' );
	} );

	test( 'should use the value as the counted item if value is set', () => {
		const value = 'Hello World!';
		const placeholder = 'placeholder test';

		renderer.render(
			<CountedTextarea value={ value } className="custom-class" placeholder={ placeholder } />
		);
		const result = renderer.getRenderOutput();

		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '12 characters' );
	} );

	test( 'should not pass acceptableLength prop to the child textarea', () => {
		const value = 'Hello World!';
		const acceptableLength = 140;

		renderer.render(
			<CountedTextarea
				value={ value }
				className="custom-class"
				acceptableLength={ acceptableLength }
			/>
		);
		const result = renderer.getRenderOutput();

		expect( result.props.children ).toHaveLength( 2 );
		expect( result.props.children[ 0 ].props.value ).toEqual( value );
		expect( result.props.children[ 0 ].props.acceptableLength ).toBeUndefined();
		expect( result.props.children[ 0 ].props.className ).toEqual( 'counted-textarea__input' );
	} );

	test( 'should render a reversed count when set to showRemainingCount', () => {
		const value = 'Hello World!';
		const acceptableLength = 140;

		renderer.render(
			<CountedTextarea
				value={ value }
				acceptableLength={ acceptableLength }
				showRemainingCharacters
			/>
		);
		const result = renderer.getRenderOutput();

		expect( result.props.className ).toEqual( 'counted-textarea' );
		expect( result.props.children ).toHaveLength( 2 );
		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '128 characters remaining' );
	} );

	test( 'should render additional panel content when set', () => {
		const value = 'Hello World!';
		const acceptableLength = 140;
		const additionalPanelContent = 'Extra stuff';

		renderer.render(
			<CountedTextarea
				value={ value }
				acceptableLength={ acceptableLength }
				showRemainingCharacters
			>
				{ additionalPanelContent }
			</CountedTextarea>
		);
		const result = renderer.getRenderOutput();

		expect( result.props.className ).toEqual( 'counted-textarea' );
		expect( result.props.children ).toHaveLength( 2 );
		expect( result.props.children[ 1 ].props.children[ 0 ] ).toEqual( '128 characters remaining' );
		expect( result.props.children[ 1 ].props.children[ 1 ] ).toEqual( 'Extra stuff' );
	} );
} );