|
| 1 | +/* |
| 2 | + * Copyright 2023 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | +import {Group, GroupContext} from '..'; |
| 13 | +import {pointerMap, render} from '@react-spectrum/test-utils'; |
| 14 | +import React from 'react'; |
| 15 | +import userEvent from '@testing-library/user-event'; |
| 16 | + |
| 17 | +describe('Group', () => { |
| 18 | + let user; |
| 19 | + beforeAll(() => { |
| 20 | + user = userEvent.setup({delay: null, pointerMap}); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should render a group with default class', () => { |
| 24 | + let {getByRole} = render(<Group>Test</Group>); |
| 25 | + let group = getByRole('group'); |
| 26 | + expect(group).toHaveAttribute('class', 'react-aria-Group'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should render a group with custom class', () => { |
| 30 | + let {getByRole} = render(<Group className="test">Test</Group>); |
| 31 | + let group = getByRole('group'); |
| 32 | + expect(group).toHaveAttribute('class', 'test'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should support DOM props', () => { |
| 36 | + let {getByRole} = render(<Group data-foo="bar">Test</Group>); |
| 37 | + let group = getByRole('group'); |
| 38 | + expect(group).toHaveAttribute('data-foo', 'bar'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should support slot', () => { |
| 42 | + let {getByRole} = render( |
| 43 | + <GroupContext.Provider value={{slots: {test: {'aria-label': 'test'}}}}> |
| 44 | + <Group slot="test">Test</Group> |
| 45 | + </GroupContext.Provider> |
| 46 | + ); |
| 47 | + |
| 48 | + let group = getByRole('group'); |
| 49 | + expect(group).toHaveAttribute('slot', 'test'); |
| 50 | + expect(group).toHaveAttribute('aria-label', 'test'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should support hover', async () => { |
| 54 | + let hoverStartSpy = jest.fn(); |
| 55 | + let hoverChangeSpy = jest.fn(); |
| 56 | + let hoverEndSpy = jest.fn(); |
| 57 | + let {getByRole} = render(<Group |
| 58 | + className={({isHovered}) => isHovered ? 'hover' : ''} |
| 59 | + onHoverStart={hoverStartSpy} |
| 60 | + onHoverChange={hoverChangeSpy} |
| 61 | + onHoverEnd={hoverEndSpy}>Test</Group>); |
| 62 | + let group = getByRole('group'); |
| 63 | + |
| 64 | + expect(group).not.toHaveAttribute('data-hovered'); |
| 65 | + expect(group).not.toHaveClass('hover'); |
| 66 | + |
| 67 | + await user.hover(group); |
| 68 | + expect(group).toHaveAttribute('data-hovered', 'true'); |
| 69 | + expect(group).toHaveClass('hover'); |
| 70 | + expect(hoverStartSpy).toHaveBeenCalledTimes(1); |
| 71 | + expect(hoverChangeSpy).toHaveBeenCalledTimes(1); |
| 72 | + |
| 73 | + await user.unhover(group); |
| 74 | + expect(group).not.toHaveAttribute('data-hovered'); |
| 75 | + expect(group).not.toHaveClass('hover'); |
| 76 | + expect(hoverEndSpy).toHaveBeenCalledTimes(1); |
| 77 | + expect(hoverChangeSpy).toHaveBeenCalledTimes(2); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should support focus ring', async () => { |
| 81 | + let {getByRole} = render(<Group className={({isFocusVisible}) => isFocusVisible ? 'focus' : ''}> |
| 82 | + <input type="text" /> |
| 83 | + </Group>); |
| 84 | + let group = getByRole('group'); |
| 85 | + let input = getByRole('textbox'); |
| 86 | + |
| 87 | + expect(group).not.toHaveAttribute('data-focus-visible'); |
| 88 | + expect(group).not.toHaveClass('focus'); |
| 89 | + |
| 90 | + await user.tab(); |
| 91 | + expect(document.activeElement).toBe(input); |
| 92 | + expect(group).toHaveAttribute('data-focus-visible', 'true'); |
| 93 | + expect(group).toHaveClass('focus'); |
| 94 | + |
| 95 | + await user.tab(); |
| 96 | + expect(group).not.toHaveAttribute('data-focus-visible'); |
| 97 | + expect(group).not.toHaveClass('focus'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should not show focus ring when typing in pointer modality', async () => { |
| 101 | + let {getByRole} = render(<Group className={({isFocusVisible}) => isFocusVisible ? 'focus' : ''}> |
| 102 | + <input type="text" /> |
| 103 | + </Group>); |
| 104 | + let group = getByRole('group'); |
| 105 | + let input = getByRole('textbox'); |
| 106 | + |
| 107 | + expect(group).not.toHaveAttribute('data-focus-visible'); |
| 108 | + expect(group).not.toHaveClass('focus'); |
| 109 | + |
| 110 | + await user.click(input); |
| 111 | + await user.keyboard('a'); |
| 112 | + expect(document.activeElement).toBe(input); |
| 113 | + expect(input).toHaveValue('a'); |
| 114 | + |
| 115 | + expect(group).not.toHaveAttribute('data-focus-visible'); |
| 116 | + expect(group).not.toHaveClass('focus'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should support disabled state', () => { |
| 120 | + let {getByRole} = render(<Group isDisabled className={({isDisabled}) => isDisabled ? 'disabled' : ''}>Test</Group>); |
| 121 | + let group = getByRole('group'); |
| 122 | + |
| 123 | + expect(group).toHaveAttribute('data-disabled', 'true'); |
| 124 | + expect(group).toHaveClass('disabled'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should support render props', async () => { |
| 128 | + let {getByRole} = render(<Group>{({isHovered}) => isHovered ? 'Hovered' : 'Group'}</Group>); |
| 129 | + let group = getByRole('group'); |
| 130 | + |
| 131 | + expect(group).toHaveTextContent('Group'); |
| 132 | + |
| 133 | + await user.hover(group); |
| 134 | + expect(group).toHaveTextContent('Hovered'); |
| 135 | + |
| 136 | + await user.unhover(group); |
| 137 | + expect(group).toHaveTextContent('Group'); |
| 138 | + }); |
| 139 | +}); |
0 commit comments