1
- import { render , screen } from '@testing-library/react' ;
1
+ import { fireEvent , render , screen } from '@testing-library/react' ;
2
2
import { axe } from 'jest-axe' ;
3
3
import * as React from 'react' ;
4
4
import { CodeArea , CodeAreaProps } from '../code-area' ;
@@ -9,41 +9,90 @@ describe('<CodeArea>', () => {
9
9
function renderComponent ( props ?: Partial < CodeAreaProps > ) {
10
10
return render (
11
11
< CodeArea
12
+ id = "mock-id"
12
13
label = "mock-label"
13
- onBeforeChange = { jest . fn ( ) }
14
+ onChangeEditor = { jest . fn ( ) }
15
+ onChangeText = { jest . fn ( ) }
14
16
value = "mock-value"
15
17
{ ...props }
16
18
/>
17
19
) ;
18
20
}
19
21
20
- it ( 'renders the label prop' , ( ) => {
21
- renderComponent ( { label : 'test label' } ) ;
22
- expect ( screen . getByLabelText ( 'test label' ) ) . toBeInTheDocument ( ) ;
23
- } ) ;
22
+ describe ( 'When CodeMirror is enabled' , ( ) => {
23
+ it ( 'renders the label prop' , ( ) => {
24
+ renderComponent ( { label : 'test label' } ) ;
25
+ expect ( screen . getByLabelText ( 'test label' ) ) . toBeInTheDocument ( ) ;
26
+ } ) ;
24
27
25
- it ( 'renders the value prop' , ( ) => {
26
- renderComponent ( { value : 'test value' } ) ;
27
- expect ( screen . getByLabelText ( 'mock-label' ) ) . toHaveValue ( 'test value' ) ;
28
- } ) ;
28
+ it ( 'renders the value prop' , ( ) => {
29
+ renderComponent ( { value : 'test value' } ) ;
30
+ expect ( screen . getByLabelText ( 'mock-label' ) ) . toHaveValue ( 'test value' ) ;
31
+ } ) ;
29
32
30
- it ( 'sets a style on the container based on the fontFamily and fontScale props' , ( ) => {
31
- renderComponent ( { fontFamily : 'my-custom-font' , fontScale : 1.5 } ) ;
33
+ it ( 'sets a style on the container based on the fontFamily and fontScale props' , ( ) => {
34
+ renderComponent ( { fontFamily : 'my-custom-font' , fontScale : 1.5 } ) ;
32
35
33
- const inputStyle = window . getComputedStyle (
34
- // eslint-disable-next-line testing-library/no-node-access
35
- document . querySelector ( '.code-area' ) !
36
- ) ;
36
+ const inputStyle = window . getComputedStyle (
37
+ // eslint-disable-next-line testing-library/no-node-access
38
+ document . querySelector ( '.code-area' ) !
39
+ ) ;
40
+
41
+ expect ( inputStyle . getPropertyValue ( 'font-family' ) ) . toBe ( 'my-custom-font' ) ;
42
+ expect ( inputStyle . getPropertyValue ( 'font-size' ) ) . toBe ( '150%' ) ;
43
+ } ) ;
44
+
45
+ // Changes are handled by props on react-codemirror2 and not tested here.
46
+
47
+ it ( 'is accessible' , async ( ) => {
48
+ const { container} = renderComponent ( ) ;
37
49
38
- expect ( inputStyle . getPropertyValue ( 'font-family' ) ) . toBe ( 'my-custom-font' ) ;
39
- expect ( inputStyle . getPropertyValue ( 'font-size' ) ) . toBe ( '150%' ) ;
50
+ expect ( await axe ( container ) ) . toHaveNoViolations ( ) ;
51
+ } ) ;
40
52
} ) ;
41
53
42
- // Changes are handled by props on react-codemirror2 and not tested here.
54
+ describe ( 'When using a browser native textarea' , ( ) => {
55
+ it ( 'renders the label prop' , ( ) => {
56
+ renderComponent ( { label : 'test label' , useCodeMirror : false } ) ;
57
+ expect ( screen . getByLabelText ( 'test label' ) ) . toBeInTheDocument ( ) ;
58
+ } ) ;
59
+
60
+ it ( 'renders the value prop' , ( ) => {
61
+ renderComponent ( { value : 'test value' , useCodeMirror : false } ) ;
62
+ expect ( screen . getByLabelText ( 'mock-label' ) ) . toHaveValue ( 'test value' ) ;
63
+ } ) ;
64
+
65
+ it ( 'sets a style on the container based on the fontFamily and fontScale props' , ( ) => {
66
+ renderComponent ( {
67
+ fontFamily : 'my-custom-font' ,
68
+ fontScale : 1.5 ,
69
+ useCodeMirror : false
70
+ } ) ;
71
+
72
+ const inputStyle = window . getComputedStyle (
73
+ // eslint-disable-next-line testing-library/no-node-access
74
+ document . querySelector ( '.code-area' ) !
75
+ ) ;
76
+
77
+ expect ( inputStyle . getPropertyValue ( 'font-family' ) ) . toBe ( 'my-custom-font' ) ;
78
+ expect ( inputStyle . getPropertyValue ( 'font-size' ) ) . toBe ( '150%' ) ;
79
+ } ) ;
80
+
81
+ it ( 'calls the onChangeText prop when the textarea is changed' , ( ) => {
82
+ const onChangeText = jest . fn ( ) ;
83
+
84
+ renderComponent ( { onChangeText, useCodeMirror : false } ) ;
85
+ expect ( onChangeText ) . not . toHaveBeenCalled ( ) ;
86
+ fireEvent . change ( screen . getByLabelText ( 'mock-label' ) , {
87
+ target : { value : 'change' }
88
+ } ) ;
89
+ expect ( onChangeText . mock . calls ) . toEqual ( [ [ 'change' ] ] ) ;
90
+ } ) ;
43
91
44
- it ( 'is accessible' , async ( ) => {
45
- const { container} = renderComponent ( ) ;
92
+ it ( 'is accessible' , async ( ) => {
93
+ const { container} = renderComponent ( { useCodeMirror : false } ) ;
46
94
47
- expect ( await axe ( container ) ) . toHaveNoViolations ( ) ;
95
+ expect ( await axe ( container ) ) . toHaveNoViolations ( ) ;
96
+ } ) ;
48
97
} ) ;
49
98
} ) ;
0 commit comments