|
| 1 | +/* |
| 2 | + * Copyright 2024 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 {actHook as act, renderHook} from '@react-spectrum/test-utils-internal'; |
| 13 | +import {KeyboardEvent, PressEvent} from '@react-types/shared'; |
| 14 | +import {useDisclosure} from '../src/useDisclosure'; |
| 15 | +import {useDisclosureState} from '@react-stately/disclosure'; |
| 16 | + |
| 17 | +describe('useDisclosure', () => { |
| 18 | + let defaultProps = {}; |
| 19 | + let ref = {current: document.createElement('div')}; |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should return correct aria attributes when collapsed', () => { |
| 26 | + let {result} = renderHook(() => { |
| 27 | + let state = useDisclosureState(defaultProps); |
| 28 | + return useDisclosure({}, state, ref); |
| 29 | + }); |
| 30 | + |
| 31 | + let {buttonProps, panelProps} = result.current; |
| 32 | + |
| 33 | + expect(buttonProps['aria-expanded']).toBe(false); |
| 34 | + expect(panelProps.hidden).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should return correct aria attributes when expanded', () => { |
| 38 | + let {result} = renderHook(() => { |
| 39 | + let state = useDisclosureState({defaultExpanded: true}); |
| 40 | + return useDisclosure({}, state, ref); |
| 41 | + }); |
| 42 | + |
| 43 | + let {buttonProps, panelProps} = result.current; |
| 44 | + |
| 45 | + expect(buttonProps['aria-expanded']).toBe(true); |
| 46 | + expect(panelProps.hidden).toBe(false); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle expanding on press event', () => { |
| 50 | + let {result} = renderHook(() => { |
| 51 | + let state = useDisclosureState({}); |
| 52 | + let disclosure = useDisclosure({}, state, ref); |
| 53 | + return {state, disclosure}; |
| 54 | + }); |
| 55 | + |
| 56 | + act(() => { |
| 57 | + result.current.disclosure.buttonProps.onPress?.({pointerType: 'mouse'} as PressEvent); |
| 58 | + }); |
| 59 | + |
| 60 | + expect(result.current.state.isExpanded).toBe(true); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle expanding on keydown event', () => { |
| 64 | + let {result} = renderHook(() => { |
| 65 | + let state = useDisclosureState({}); |
| 66 | + let disclosure = useDisclosure({}, state, ref); |
| 67 | + return {state, disclosure}; |
| 68 | + }); |
| 69 | + |
| 70 | + let preventDefault = jest.fn(); |
| 71 | + let event = (e: Partial<KeyboardEvent>) => ({...e, preventDefault} as KeyboardEvent); |
| 72 | + |
| 73 | + act(() => { |
| 74 | + result.current.disclosure.buttonProps.onKeyDown?.(event({key: 'Enter', preventDefault}) as KeyboardEvent); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(preventDefault).toHaveBeenCalledTimes(1); |
| 78 | + |
| 79 | + expect(result.current.state.isExpanded).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should not toggle when disabled', () => { |
| 83 | + let {result} = renderHook(() => { |
| 84 | + let state = useDisclosureState({}); |
| 85 | + let disclosure = useDisclosure({isDisabled: true}, state, ref); |
| 86 | + return {state, disclosure}; |
| 87 | + }); |
| 88 | + |
| 89 | + act(() => { |
| 90 | + result.current.disclosure.buttonProps.onPress?.({pointerType: 'mouse'} as PressEvent); |
| 91 | + }); |
| 92 | + |
| 93 | + expect(result.current.state.isExpanded).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should set correct IDs for accessibility', () => { |
| 97 | + let {result} = renderHook(() => { |
| 98 | + let state = useDisclosureState({}); |
| 99 | + return useDisclosure({}, state, ref); |
| 100 | + }); |
| 101 | + |
| 102 | + let {buttonProps, panelProps} = result.current; |
| 103 | + |
| 104 | + expect(buttonProps['aria-controls']).toBe(panelProps.id); |
| 105 | + expect(panelProps['aria-labelledby']).toBe(buttonProps.id); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should expand when beforematch event occurs', () => { |
| 109 | + // Mock 'onbeforematch' support on document.body |
| 110 | + // @ts-ignore |
| 111 | + const originalOnBeforeMatch = document.body.onbeforematch; |
| 112 | + Object.defineProperty(document.body, 'onbeforematch', { |
| 113 | + value: null, |
| 114 | + writable: true, |
| 115 | + configurable: true |
| 116 | + }); |
| 117 | + |
| 118 | + const ref = {current: document.createElement('div')}; |
| 119 | + |
| 120 | + const {result} = renderHook(() => { |
| 121 | + const state = useDisclosureState({}); |
| 122 | + const disclosure = useDisclosure({}, state, ref); |
| 123 | + return {state, disclosure}; |
| 124 | + }); |
| 125 | + |
| 126 | + expect(result.current.state.isExpanded).toBe(false); |
| 127 | + expect(ref.current.getAttribute('hidden')).toBe('until-found'); |
| 128 | + |
| 129 | + // Simulate the 'beforematch' event |
| 130 | + act(() => { |
| 131 | + const event = new Event('beforematch', {bubbles: true}); |
| 132 | + ref.current.dispatchEvent(event); |
| 133 | + }); |
| 134 | + |
| 135 | + expect(result.current.state.isExpanded).toBe(true); |
| 136 | + expect(ref.current.hasAttribute('hidden')).toBe(false); |
| 137 | + |
| 138 | + Object.defineProperty(document.body, 'onbeforematch', { |
| 139 | + value: originalOnBeforeMatch, |
| 140 | + writable: true, |
| 141 | + configurable: true |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should not expand when beforematch event occurs if controlled and closed', () => { |
| 146 | + // Mock 'onbeforematch' support on document.body |
| 147 | + // @ts-ignore |
| 148 | + const originalOnBeforeMatch = document.body.onbeforematch; |
| 149 | + Object.defineProperty(document.body, 'onbeforematch', { |
| 150 | + value: null, |
| 151 | + writable: true, |
| 152 | + configurable: true |
| 153 | + }); |
| 154 | + |
| 155 | + const ref = {current: document.createElement('div')}; |
| 156 | + |
| 157 | + const onExpandedChange = jest.fn(); |
| 158 | + |
| 159 | + const {result} = renderHook(() => { |
| 160 | + const state = useDisclosureState({isExpanded: false, onExpandedChange}); |
| 161 | + const disclosure = useDisclosure({isExpanded: false}, state, ref); |
| 162 | + return {state, disclosure}; |
| 163 | + }); |
| 164 | + |
| 165 | + expect(result.current.state.isExpanded).toBe(false); |
| 166 | + expect(ref.current.getAttribute('hidden')).toBeNull(); |
| 167 | + |
| 168 | + // Simulate the 'beforematch' event |
| 169 | + act(() => { |
| 170 | + const event = new Event('beforematch', {bubbles: true}); |
| 171 | + ref.current.dispatchEvent(event); |
| 172 | + }); |
| 173 | + |
| 174 | + expect(result.current.state.isExpanded).toBe(false); |
| 175 | + expect(ref.current.getAttribute('hidden')).toBeNull(); |
| 176 | + expect(onExpandedChange).not.toHaveBeenCalled(); |
| 177 | + |
| 178 | + Object.defineProperty(document.body, 'onbeforematch', { |
| 179 | + value: originalOnBeforeMatch, |
| 180 | + writable: true, |
| 181 | + configurable: true |
| 182 | + }); |
| 183 | + }); |
| 184 | +}); |
0 commit comments