|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + |
| 13 | +import React, {useRef} from 'react'; |
| 14 | +import {render} from '@testing-library/react'; |
| 15 | +import {SlotProvider, useSlotProps} from '../'; |
| 16 | +import {triggerPress} from '@react-spectrum/test-utils'; |
| 17 | +import {useId, useSlotId} from '@react-aria/utils'; |
| 18 | +import {usePress} from '@react-aria/interactions'; |
| 19 | + |
| 20 | + |
| 21 | +describe('Slots', function () { |
| 22 | + let results = {}; |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + results = {}; |
| 26 | + }); |
| 27 | + |
| 28 | + function Component(props) { |
| 29 | + results = useSlotProps(props, 'slotname'); |
| 30 | + props = results; |
| 31 | + let ref = useRef(); |
| 32 | + let {pressProps} = usePress({onPress: props.onPress, ref}); |
| 33 | + let id = useId(props.id); |
| 34 | + return <button id={id} {...pressProps} ref={ref}>push me</button>; |
| 35 | + } |
| 36 | + |
| 37 | + it('sets props', function () { |
| 38 | + let slots = { |
| 39 | + slotname: {UNSAFE_className: 'foo', isDisabled: true, isQuiet: true} |
| 40 | + }; |
| 41 | + render( |
| 42 | + <SlotProvider slots={slots}> |
| 43 | + <Component /> |
| 44 | + </SlotProvider> |
| 45 | + ); |
| 46 | + expect(results).toMatchObject({ |
| 47 | + UNSAFE_className: 'foo', |
| 48 | + isDisabled: true, |
| 49 | + isQuiet: true |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('overrides local props', function () { |
| 54 | + let slots = { |
| 55 | + slotname: {UNSAFE_className: 'foo', isDisabled: false, isQuiet: false, label: null} |
| 56 | + }; |
| 57 | + render( |
| 58 | + <SlotProvider slots={slots}> |
| 59 | + <Component UNSAFE_className="bar" isDisabled isQuiet label="boop" /> |
| 60 | + </SlotProvider> |
| 61 | + ); |
| 62 | + expect(results).toMatchObject({ |
| 63 | + UNSAFE_className: expect.stringMatching(/(foo bar|bar foo)/), |
| 64 | + isDisabled: false, |
| 65 | + isQuiet: false, |
| 66 | + label: null |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('undefined does not override local props', function () { |
| 71 | + let slots = { |
| 72 | + slotname: {label: undefined} |
| 73 | + }; |
| 74 | + render( |
| 75 | + <SlotProvider slots={slots}> |
| 76 | + <Component label="boop" /> |
| 77 | + </SlotProvider> |
| 78 | + ); |
| 79 | + expect(results).toMatchObject({ |
| 80 | + label: 'boop' |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('chains functions', function () { |
| 85 | + let onPress = jest.fn(); |
| 86 | + let onPressUser = jest.fn(); |
| 87 | + let slots = { |
| 88 | + slotname: {onPress} |
| 89 | + }; |
| 90 | + let {getByRole} = render( |
| 91 | + <SlotProvider slots={slots}> |
| 92 | + <Component label="boop" onPress={onPressUser} /> |
| 93 | + </SlotProvider> |
| 94 | + ); |
| 95 | + triggerPress(getByRole('button')); |
| 96 | + expect(onPress).toHaveBeenCalledTimes(1); |
| 97 | + expect(onPressUser).toHaveBeenCalledTimes(1); |
| 98 | + }); |
| 99 | + |
| 100 | + it('lets users set their own id', function () { |
| 101 | + let slots = { |
| 102 | + slotname: {id: 'foo'} |
| 103 | + }; |
| 104 | + render( |
| 105 | + <SlotProvider slots={slots}> |
| 106 | + <Component label="boop" id="bar" /> |
| 107 | + </SlotProvider> |
| 108 | + ); |
| 109 | + expect(results).toMatchObject({id: 'bar'}); |
| 110 | + }); |
| 111 | + |
| 112 | + it('lets users set their own id when used in conjunction with useId even if a default is passed to useId', function () { |
| 113 | + function SlotsUseId(props) { |
| 114 | + let id = useId(props.id); |
| 115 | + return ( |
| 116 | + <> |
| 117 | + <div role="presentation" aria-controls={id}>nowhere</div> |
| 118 | + <SlotProvider slots={{slotname: {...props.slots, id}}}> |
| 119 | + <Component id="bar" /> |
| 120 | + </SlotProvider> |
| 121 | + </> |
| 122 | + ); |
| 123 | + } |
| 124 | + let {getByRole} = render(<SlotsUseId id="foo" />); |
| 125 | + expect(results).toMatchObject({id: 'bar'}); // we've merged with the user provided id |
| 126 | + expect(getByRole('presentation')).toHaveAttribute('aria-controls', 'bar'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('lets users set their own id when used in conjunction with useId', function () { |
| 130 | + function SlotsUseId(props) { |
| 131 | + let id = useId(); |
| 132 | + return ( |
| 133 | + <> |
| 134 | + <div role="presentation" aria-controls={id}>nowhere</div> |
| 135 | + <SlotProvider slots={{slotname: {...props.slots, id}}}> |
| 136 | + <Component id="bar" /> |
| 137 | + </SlotProvider> |
| 138 | + </> |
| 139 | + ); |
| 140 | + } |
| 141 | + let {getByRole} = render(<SlotsUseId />); |
| 142 | + expect(results).toMatchObject({id: 'bar'}); // we've merged with the user provided id |
| 143 | + expect(getByRole('presentation')).toHaveAttribute('aria-controls', 'bar'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('lets users set their own id when used in conjunction with useSlotId', function () { |
| 147 | + function SlotsUseSlotId() { |
| 148 | + let id = useSlotId(); |
| 149 | + return ( |
| 150 | + <> |
| 151 | + <div role="presentation" aria-controls={id}>nowhere</div> |
| 152 | + <SlotProvider slots={{slotname: {id}}}> |
| 153 | + <Component id="bar" /> |
| 154 | + </SlotProvider> |
| 155 | + </> |
| 156 | + ); |
| 157 | + } |
| 158 | + let {getByRole} = render(<SlotsUseSlotId />); |
| 159 | + expect(results).toMatchObject({id: 'bar'}); // we've merged with the user provided id |
| 160 | + expect(getByRole('presentation')).toHaveAttribute('aria-controls', 'bar'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('multiple useIds can be used together and resolve to the one used on the component', function () { |
| 164 | + let id = ''; |
| 165 | + function SlotsUseSlotId() { |
| 166 | + let id1 = useId(); |
| 167 | + let id2 = useId(); |
| 168 | + id = useRef(id2).current; |
| 169 | + |
| 170 | + return ( |
| 171 | + <> |
| 172 | + <div role="presentation" aria-controls={id1}>nowhere</div> |
| 173 | + <SlotProvider slots={{slotname: {id: id1}}}> |
| 174 | + <Component id={id2} /> |
| 175 | + </SlotProvider> |
| 176 | + </> |
| 177 | + ); |
| 178 | + } |
| 179 | + let {getByRole} = render(<SlotsUseSlotId />); |
| 180 | + expect(results).toMatchObject({id}); // we've merged with the user provided id |
| 181 | + expect(getByRole('presentation')).toHaveAttribute('aria-controls', id); |
| 182 | + }); |
| 183 | + |
| 184 | +}); |
0 commit comments