@@ -3,124 +3,140 @@ import React from 'react';
3
3
import { render , screen } from '@testing-library/react' ;
4
4
import userEvent from '@testing-library/user-event' ;
5
5
6
- import { TextArea , TextAreaBase } from '../TextArea' ;
6
+ import { TextArea } from '../TextArea' ;
7
7
import { ValidatedOptions } from '../../../helpers/constants' ;
8
8
9
9
const props = {
10
10
onChange : jest . fn ( ) ,
11
11
value : 'test textarea'
12
12
} ;
13
13
14
- describe ( 'TextArea' , ( ) => {
15
- test ( 'textarea passes value and event to onChange handler' , async ( ) => {
16
- const user = userEvent . setup ( ) ;
17
-
18
- render ( < TextAreaBase { ...props } value = "" aria-label = "test textarea" /> ) ;
19
-
20
- await user . type ( screen . getByLabelText ( 'test textarea' ) , 'a' ) ;
21
- expect ( props . onChange ) . toHaveBeenCalledWith ( 'a' , expect . any ( Object ) ) ;
22
- } ) ;
23
-
24
- test ( 'simple text area' , ( ) => {
25
- const { asFragment } = render ( < TextArea { ...props } aria-label = "simple textarea" /> ) ;
26
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
27
- } ) ;
28
-
29
- test ( 'disabled text area using isDisabled' , ( ) => {
30
- const { asFragment } = render ( < TextArea { ...props } aria-label = "is disabled textarea" isDisabled /> ) ;
31
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
32
- } ) ;
33
-
34
- test ( 'disabled text area using disabled' , ( ) => {
35
- const { asFragment } = render ( < TextArea { ...props } aria-label = "disabled textarea" disabled /> ) ;
36
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
37
- } ) ;
38
-
39
- test ( 'read only text area using isReadOnly' , ( ) => {
40
- const { asFragment } = render ( < TextArea { ...props } aria-label = "is read only textarea" isReadOnly /> ) ;
41
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
42
- } ) ;
43
-
44
- test ( 'read only text area using default readOnlyVariant' , ( ) => {
45
- const { asFragment } = render (
46
- < TextArea { ...props } aria-label = "is default read only textarea" readOnlyVariant = "default" />
47
- ) ;
48
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
49
- } ) ;
50
-
51
- test ( 'read only text area using plain readOnlyVariant' , ( ) => {
52
- const { asFragment } = render (
53
- < TextArea { ...props } aria-label = "is plain read only textarea" readOnlyVariant = "plain" />
54
- ) ;
55
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
56
- } ) ;
57
-
58
- test ( 'read only text area using readOnly' , ( ) => {
59
- const { asFragment } = render ( < TextArea { ...props } aria-label = "read only textarea" readOnly /> ) ;
60
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
61
- } ) ;
62
-
63
- test ( 'validated text area success' , ( ) => {
64
- render ( < TextArea { ...props } required validated = { ValidatedOptions . success } aria-label = "validated textarea" /> ) ;
65
- expect ( screen . getByLabelText ( 'validated textarea' ) ) . toHaveClass ( 'pf-m-success' ) ;
66
- } ) ;
67
-
68
- test ( 'validated text area warning' , ( ) => {
69
- render ( < TextArea { ...props } required validated = { ValidatedOptions . warning } aria-label = "validated textarea" /> ) ;
70
- expect ( screen . getByLabelText ( 'validated textarea' ) ) . toHaveClass ( 'pf-m-warning' ) ;
71
- } ) ;
72
-
73
- test ( 'validated text area error' , ( ) => {
74
- const { asFragment } = render (
75
- < TextArea { ...props } required validated = { ValidatedOptions . error } aria-label = "validated textarea" />
76
- ) ;
77
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
78
- } ) ;
79
-
80
- test ( 'vertically resizable text area' , ( ) => {
81
- const { asFragment } = render (
82
- < TextArea resizeOrientation = "vertical" { ...props } aria-label = "vertical resize textarea" />
83
- ) ;
84
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
85
- } ) ;
86
-
87
- test ( 'horizontally resizable text area' , ( ) => {
88
- const { asFragment } = render (
89
- < TextArea
90
- resizeOrientation = "horizontal"
91
- { ...props }
92
- required
93
- validated = { 'error' }
94
- aria-label = "horizontal resize textarea"
95
- />
96
- ) ;
97
- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
98
- } ) ;
99
-
100
- test ( 'should throw console error when no aria-label or id is given' , ( ) => {
101
- const myMock = jest . fn ( ) ;
102
- global . console = { ...global . console , error : myMock } ;
103
-
104
- render ( < TextArea { ...props } /> ) ;
105
-
106
- expect ( myMock ) . toHaveBeenCalled ( ) ;
107
- } ) ;
108
-
109
- test ( 'should not throw console error when id is given but no aria-label' , ( ) => {
110
- const myMock = jest . fn ( ) ;
111
- global . console = { ...global . console , error : myMock } ;
112
-
113
- render ( < TextArea { ...props } id = "5" /> ) ;
114
-
115
- expect ( myMock ) . not . toHaveBeenCalled ( ) ;
116
- } ) ;
117
-
118
- test ( 'should not throw console error when aria-label is given but no id' , ( ) => {
119
- const myMock = jest . fn ( ) ;
120
- global . console = { ...global . console , error : myMock } ;
121
-
122
- render ( < TextArea { ...props } aria-label = "test textarea" /> ) ;
123
-
124
- expect ( myMock ) . not . toHaveBeenCalled ( ) ;
125
- } ) ;
14
+ test ( 'Textarea input passes value and event to onChange handler' , async ( ) => {
15
+ const user = userEvent . setup ( ) ;
16
+ render ( < TextArea { ...props } value = "" aria-label = "test textarea" /> ) ;
17
+
18
+ await user . type ( screen . getByRole ( 'textbox' ) , 'a' ) ;
19
+
20
+ expect ( props . onChange ) . toHaveBeenCalledWith ( 'a' , expect . any ( Object ) ) ;
21
+ } ) ;
22
+
23
+ test ( 'Renders simple text input' , ( ) => {
24
+ render (
25
+ < div data-testid = "textarea" >
26
+ < TextArea aria-label = "simple textarea" />
27
+ </ div >
28
+ ) ;
29
+ expect ( screen . getByTestId ( 'textarea' ) . firstChild ) . toBeVisible ( ) ;
30
+ } ) ;
31
+
32
+ test ( 'Renders with custom class passed' , ( ) => {
33
+ render ( < TextArea aria-label = "custom class textarea" className = "test-class" /> ) ;
34
+
35
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'test-class' ) ;
36
+ } ) ;
37
+
38
+ test ( 'Renders text area with required attribute using isRequired' , ( ) => {
39
+ render ( < TextArea aria-label = "required textarea" isRequired /> ) ;
40
+
41
+ expect ( screen . getByRole ( 'textbox' ) ) . toBeRequired ( ) ;
42
+ } ) ;
43
+
44
+ test ( 'Text area is not required by default' , ( ) => {
45
+ render ( < TextArea aria-label = "not required textarea" /> ) ;
46
+
47
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toBeRequired ( ) ;
48
+ } ) ;
49
+
50
+ test ( 'Renders disabled text area using isDisabled' , ( ) => {
51
+ render ( < TextArea aria-label = "is disabled textarea" isDisabled /> ) ;
52
+ expect ( screen . getByRole ( 'textbox' ) ) . toBeDisabled ( ) ;
53
+ } ) ;
54
+
55
+ test ( 'Text area is not disabled by default' , ( ) => {
56
+ render ( < TextArea aria-label = "not disabled textarea" /> ) ;
57
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toBeDisabled ( ) ;
58
+ } ) ;
59
+
60
+ test ( 'Renders read only text area using readOnlyVariant' , ( ) => {
61
+ render ( < TextArea aria-label = "is read only textarea" readOnlyVariant = { 'default' } /> ) ;
62
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveAttribute ( 'readonly' ) ;
63
+ } ) ;
64
+
65
+ test ( 'Text area is not read only by default' , ( ) => {
66
+ render ( < TextArea aria-label = "not read only textarea" /> ) ;
67
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toHaveAttribute ( 'readonly' ) ;
68
+ } ) ;
69
+
70
+ test ( 'Renders text area with default class name only' , ( ) => {
71
+ render ( < TextArea aria-label = "validated textarea" /> ) ;
72
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'pf-c-form-control' , { exact : true } ) ;
73
+ } ) ;
74
+
75
+ test ( 'Renders validated text area with success className' , ( ) => {
76
+ render ( < TextArea aria-label = "validated textarea" validated = { ValidatedOptions . success } /> ) ;
77
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'pf-m-success' ) ;
78
+ } ) ;
79
+
80
+ test ( 'Renders validated text area with warning className' , ( ) => {
81
+ render ( < TextArea aria-label = "validated textarea" validated = { ValidatedOptions . warning } /> ) ;
82
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'pf-m-warning' ) ;
83
+ } ) ;
84
+
85
+ test ( 'Renders invalid text area' , ( ) => {
86
+ render ( < TextArea aria-label = "validated textarea" validated = { ValidatedOptions . error } /> ) ;
87
+ expect ( screen . getByRole ( 'textbox' ) ) . toBeInvalid ( ) ;
88
+ } ) ;
89
+
90
+ test ( 'Text area is not invalid by default' , ( ) => {
91
+ render ( < TextArea aria-label = "validated textarea" /> ) ;
92
+ expect ( screen . getByRole ( 'textbox' ) ) . not . toBeInvalid ( ) ;
93
+ } ) ;
94
+
95
+ test ( 'Renders vertically resizable text area' , ( ) => {
96
+ render ( < TextArea aria-label = "vertical resize textarea" resizeOrientation = "vertical" /> ) ;
97
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'pf-m-resize-vertical' ) ;
98
+ } ) ;
99
+
100
+ test ( 'Renders horizontally resizable text area' , ( ) => {
101
+ render ( < TextArea aria-label = "horizontal resize textarea" resizeOrientation = "horizontal" /> ) ;
102
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveClass ( 'pf-m-resize-horizontal' ) ;
103
+ } ) ;
104
+
105
+ test ( 'Throws console error when no aria-label or id is given' , ( ) => {
106
+ jest . spyOn ( global . console , 'error' ) ;
107
+
108
+ render ( < TextArea /> ) ;
109
+
110
+ expect ( console . error ) . toHaveBeenCalled ( ) ;
111
+ } ) ;
112
+
113
+ test ( 'Does not throw console error when id is given but no aria-label' , ( ) => {
114
+ jest . spyOn ( global . console , 'error' ) ;
115
+
116
+ render ( < TextArea id = "5" /> ) ;
117
+
118
+ expect ( console . error ) . not . toHaveBeenCalled ( ) ;
119
+ } ) ;
120
+
121
+ test ( 'Does not throw console error when aria-label is given but no id' , ( ) => {
122
+ jest . spyOn ( global . console , 'error' ) ;
123
+
124
+ render ( < TextArea aria-label = "test textarea" /> ) ;
125
+
126
+ expect ( console . error ) . not . toHaveBeenCalled ( ) ;
127
+ } ) ;
128
+
129
+ test ( 'TextArea can be accessed via passed ref' , ( ) => {
130
+ const testRef : React . RefObject < HTMLTextAreaElement > = React . createRef ( ) ;
131
+ render ( < TextArea ref = { testRef } /> ) ;
132
+ global . scrollTo = jest . fn ( ) ;
133
+ testRef . current ?. focus ( ) ;
134
+ expect ( screen . getByRole ( 'textbox' ) ) . toHaveFocus ( ) ;
135
+ } ) ;
136
+
137
+ test ( 'Matches the snapshot' , ( ) => {
138
+ const { asFragment } = render (
139
+ < TextArea className = "custom class" isRequired isDisabled autoResize aria-label = "test textarea" />
140
+ ) ;
141
+ expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
126
142
} ) ;
0 commit comments