|
10 | 10 | * governing permissions and limitations under the License.
|
11 | 11 | */
|
12 | 12 |
|
13 |
| -import {Input, Label, Text, TextField, TextFieldContext} from '../'; |
| 13 | +import {Input, Label, Text, TextArea, TextField, TextFieldContext} from '../'; |
14 | 14 | import React from 'react';
|
15 | 15 | import {render} from '@react-spectrum/test-utils';
|
16 | 16 | import userEvent from '@testing-library/user-event';
|
17 | 17 |
|
18 | 18 | let TestTextField = (props) => (
|
19 | 19 | <TextField defaultValue="test" data-foo="bar" {...props}>
|
20 | 20 | <Label>Test</Label>
|
21 |
| - <Input {...props.inputProps} /> |
| 21 | + <props.input {...props.inputProps} /> |
22 | 22 | <Text slot="description">Description</Text>
|
23 | 23 | <Text slot="errorMessage">Error</Text>
|
24 | 24 | </TextField>
|
25 | 25 | );
|
26 | 26 |
|
27 | 27 | describe('TextField', () => {
|
28 |
| - it('provides slots', () => { |
29 |
| - let {getByRole} = render(<TestTextField />); |
30 |
| - |
31 |
| - let input = getByRole('textbox'); |
32 |
| - expect(input).toHaveValue('test'); |
33 |
| - expect(input).toHaveAttribute('class', 'react-aria-Input'); |
34 |
| - |
35 |
| - expect(input.closest('.react-aria-TextField')).toHaveAttribute('data-foo', 'bar'); |
36 |
| - |
37 |
| - expect(input).toHaveAttribute('aria-labelledby'); |
38 |
| - let label = document.getElementById(input.getAttribute('aria-labelledby')); |
39 |
| - expect(label).toHaveAttribute('class', 'react-aria-Label'); |
40 |
| - expect(label).toHaveTextContent('Test'); |
41 |
| - |
42 |
| - expect(input).toHaveAttribute('aria-describedby'); |
43 |
| - expect(input.getAttribute('aria-describedby').split(' ').map(id => document.getElementById(id).textContent).join(' ')).toBe('Description Error'); |
44 |
| - }); |
45 |
| - |
46 |
| - it('should support slot', () => { |
47 |
| - let {getByRole} = render( |
48 |
| - <TextFieldContext.Provider value={{slots: {test: {'aria-label': 'test'}}}}> |
49 |
| - <TestTextField slot="test" /> |
50 |
| - </TextFieldContext.Provider> |
51 |
| - ); |
52 |
| - |
53 |
| - let textbox = getByRole('textbox'); |
54 |
| - expect(textbox.closest('.react-aria-TextField')).toHaveAttribute('slot', 'test'); |
55 |
| - expect(textbox).toHaveAttribute('aria-label', 'test'); |
56 |
| - }); |
57 |
| - |
58 |
| - it('should support hover state', () => { |
59 |
| - let {getByRole} = render(<TestTextField inputProps={{className: ({isHovered}) => isHovered ? 'hover' : ''}} />); |
60 |
| - let input = getByRole('textbox'); |
61 |
| - |
62 |
| - expect(input).not.toHaveAttribute('data-hovered'); |
63 |
| - expect(input).not.toHaveClass('hover'); |
64 |
| - |
65 |
| - userEvent.hover(input); |
66 |
| - expect(input).toHaveAttribute('data-hovered', 'true'); |
67 |
| - expect(input).toHaveClass('hover'); |
68 |
| - |
69 |
| - userEvent.unhover(input); |
70 |
| - expect(input).not.toHaveAttribute('data-hovered'); |
71 |
| - expect(input).not.toHaveClass('hover'); |
72 |
| - }); |
73 |
| - |
74 |
| - it('should support focus visible state', () => { |
75 |
| - let {getByRole} = render(<TestTextField inputProps={{className: ({isFocusVisible}) => isFocusVisible ? 'focus' : ''}} />); |
76 |
| - let input = getByRole('textbox'); |
77 |
| - |
78 |
| - expect(input).not.toHaveAttribute('data-focus-visible'); |
79 |
| - expect(input).not.toHaveClass('focus'); |
80 |
| - |
81 |
| - userEvent.tab(); |
82 |
| - expect(document.activeElement).toBe(input); |
83 |
| - expect(input).toHaveAttribute('data-focus-visible', 'true'); |
84 |
| - expect(input).toHaveClass('focus'); |
85 |
| - |
86 |
| - userEvent.tab(); |
87 |
| - expect(input).not.toHaveAttribute('data-focus-visible'); |
88 |
| - expect(input).not.toHaveClass('focus'); |
| 28 | + describe.each([ |
| 29 | + {name: 'Input', component: Input}, |
| 30 | + {name: 'TextArea', component: TextArea}] |
| 31 | + )('$name', ({name, component}) => { |
| 32 | + it('provides slots', () => { |
| 33 | + let {getByRole} = render(<TestTextField input={component} />); |
| 34 | + |
| 35 | + let input = getByRole('textbox'); |
| 36 | + expect(input).toHaveValue('test'); |
| 37 | + expect(input).toHaveAttribute('class', `react-aria-${name}`); |
| 38 | + if (name === 'Input') { |
| 39 | + expect(input).toHaveAttribute('type', 'text'); |
| 40 | + } else { |
| 41 | + expect(input).not.toHaveAttribute('type'); |
| 42 | + } |
| 43 | + |
| 44 | + expect(input.closest('.react-aria-TextField')).toHaveAttribute('data-foo', 'bar'); |
| 45 | + |
| 46 | + expect(input).toHaveAttribute('aria-labelledby'); |
| 47 | + let label = document.getElementById(input.getAttribute('aria-labelledby')); |
| 48 | + expect(label).toHaveAttribute('class', 'react-aria-Label'); |
| 49 | + expect(label).toHaveTextContent('Test'); |
| 50 | + |
| 51 | + expect(input).toHaveAttribute('aria-describedby'); |
| 52 | + expect(input.getAttribute('aria-describedby').split(' ').map(id => document.getElementById(id).textContent).join(' ')).toBe('Description Error'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should support slot', () => { |
| 56 | + let {getByRole} = render( |
| 57 | + <TextFieldContext.Provider value={{slots: {test: {'aria-label': 'test'}}}}> |
| 58 | + <TestTextField slot="test" input={component} /> |
| 59 | + </TextFieldContext.Provider> |
| 60 | + ); |
| 61 | + |
| 62 | + let textbox = getByRole('textbox'); |
| 63 | + expect(textbox.closest('.react-aria-TextField')).toHaveAttribute('slot', 'test'); |
| 64 | + expect(textbox).toHaveAttribute('aria-label', 'test'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should support hover state', () => { |
| 68 | + let {getByRole} = render(<TestTextField input={component} inputProps={{className: ({isHovered}) => isHovered ? 'hover' : ''}} />); |
| 69 | + let input = getByRole('textbox'); |
| 70 | + |
| 71 | + expect(input).not.toHaveAttribute('data-hovered'); |
| 72 | + expect(input).not.toHaveClass('hover'); |
| 73 | + |
| 74 | + userEvent.hover(input); |
| 75 | + expect(input).toHaveAttribute('data-hovered', 'true'); |
| 76 | + expect(input).toHaveClass('hover'); |
| 77 | + |
| 78 | + userEvent.unhover(input); |
| 79 | + expect(input).not.toHaveAttribute('data-hovered'); |
| 80 | + expect(input).not.toHaveClass('hover'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should support focus visible state', () => { |
| 84 | + let {getByRole} = render(<TestTextField input={component} inputProps={{className: ({isFocusVisible}) => isFocusVisible ? 'focus' : ''}} />); |
| 85 | + let input = getByRole('textbox'); |
| 86 | + |
| 87 | + expect(input).not.toHaveAttribute('data-focus-visible'); |
| 88 | + expect(input).not.toHaveClass('focus'); |
| 89 | + |
| 90 | + userEvent.tab(); |
| 91 | + expect(document.activeElement).toBe(input); |
| 92 | + expect(input).toHaveAttribute('data-focus-visible', 'true'); |
| 93 | + expect(input).toHaveClass('focus'); |
| 94 | + |
| 95 | + userEvent.tab(); |
| 96 | + expect(input).not.toHaveAttribute('data-focus-visible'); |
| 97 | + expect(input).not.toHaveClass('focus'); |
| 98 | + }); |
89 | 99 | });
|
90 | 100 | });
|
0 commit comments