-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(s2/Card): support press event callbacks on standalone Card #9983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
fb91e0d
657bada
ac448c1
0343aad
f17d5a0
e1a72ef
1465235
ac40475
7aca8ea
a092284
ccceb0d
354adbe
deaa652
84b4608
3deb928
f3cd214
0a49c83
346d234
68dee0c
edbfeca
818a6fa
ccf302d
6aeb231
bfd38df
5f3eb1c
bcd1e55
bf62a86
86c8a3b
0ab4169
fc3a567
d7176d5
6e755ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,144 @@ | ||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||
| * Copyright 2025 Adobe. All rights reserved. | ||||||||||||||||||||||||
| * This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||
| * you may not use this file except in compliance with the License. You may obtain a copy | ||||||||||||||||||||||||
| * of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * Unless required by applicable law or agreed to in writing, software distributed under | ||||||||||||||||||||||||
| * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||||||||||||||||||||||||
| * OF ANY KIND, either express or implied. See the License for the specific language | ||||||||||||||||||||||||
| * governing permissions and limitations under the License. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import {act, pointerMap, render} from '@react-spectrum/test-utils-internal'; | ||||||||||||||||||||||||
| import {Card} from '../src/Card'; | ||||||||||||||||||||||||
| import {Content, Text} from '../src/Content'; | ||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||
| import userEvent from '@testing-library/user-event'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| describe('Card', () => { | ||||||||||||||||||||||||
| let user; | ||||||||||||||||||||||||
| beforeAll(() => { | ||||||||||||||||||||||||
| jest.useFakeTimers(); | ||||||||||||||||||||||||
| user = userEvent.setup({delay: null, pointerMap}); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| afterEach(() => { | ||||||||||||||||||||||||
| jest.clearAllMocks(); | ||||||||||||||||||||||||
| act(() => jest.runAllTimers()); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| afterAll(() => { | ||||||||||||||||||||||||
| jest.restoreAllMocks(); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('renders as a plain div when no press callbacks are provided', () => { | ||||||||||||||||||||||||
| let {getByText} = render( | ||||||||||||||||||||||||
| <Card> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Static Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| let el = getByText('Static Card').closest('[class]')!.parentElement!; | ||||||||||||||||||||||||
| expect(el.tagName).toBe('DIV'); | ||||||||||||||||||||||||
| expect(el).not.toHaveAttribute('role'); | ||||||||||||||||||||||||
| expect(el).not.toHaveAttribute('tabindex'); | ||||||||||||||||||||||||
|
Comment on lines
+43
to
+46
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could do something like
Suggested change
then it's less dependent on structure and implementation note, the above is psuedocode, it may need some tweaks, but that's the idea |
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('renders as role=button and fires onPress when onPress is provided', async () => { | ||||||||||||||||||||||||
| let onPress = jest.fn(); | ||||||||||||||||||||||||
| let {getByRole} = render( | ||||||||||||||||||||||||
| <Card onPress={onPress}> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Interactive Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let card = getByRole('button'); | ||||||||||||||||||||||||
| expect(card).toBeInTheDocument(); | ||||||||||||||||||||||||
| expect(card).toHaveAttribute('tabindex', '0'); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await user.click(card); | ||||||||||||||||||||||||
| expect(onPress).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('fires onAction when onAction is provided', async () => { | ||||||||||||||||||||||||
| let onAction = jest.fn(); | ||||||||||||||||||||||||
| let {getByRole} = render( | ||||||||||||||||||||||||
| <Card onAction={onAction}> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Action Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let card = getByRole('button'); | ||||||||||||||||||||||||
| await user.click(card); | ||||||||||||||||||||||||
| expect(onAction).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('fires both onPress and onAction when both are provided', async () => { | ||||||||||||||||||||||||
| let onPress = jest.fn(); | ||||||||||||||||||||||||
| let onAction = jest.fn(); | ||||||||||||||||||||||||
| let {getByRole} = render( | ||||||||||||||||||||||||
| <Card onPress={onPress} onAction={onAction}> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Both Callbacks Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let card = getByRole('button'); | ||||||||||||||||||||||||
| await user.click(card); | ||||||||||||||||||||||||
| expect(onPress).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| expect(onAction).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('fires onPressStart and onPressEnd when provided', async () => { | ||||||||||||||||||||||||
| let onPressStart = jest.fn(); | ||||||||||||||||||||||||
| let onPressEnd = jest.fn(); | ||||||||||||||||||||||||
| let {getByRole} = render( | ||||||||||||||||||||||||
| <Card onPressStart={onPressStart} onPressEnd={onPressEnd}> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Press Events Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let card = getByRole('button'); | ||||||||||||||||||||||||
| await user.click(card); | ||||||||||||||||||||||||
| expect(onPressStart).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| expect(onPressEnd).toHaveBeenCalledTimes(1); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('does not fire press callbacks when disabled', async () => { | ||||||||||||||||||||||||
| let onPress = jest.fn(); | ||||||||||||||||||||||||
| let {getByRole} = render( | ||||||||||||||||||||||||
| <Card onPress={onPress} isDisabled> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Disabled Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| let card = getByRole('button'); | ||||||||||||||||||||||||
| expect(card).not.toHaveAttribute('tabindex'); | ||||||||||||||||||||||||
| expect(card).toHaveAttribute('aria-disabled', 'true'); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| await user.click(card); | ||||||||||||||||||||||||
| expect(onPress).not.toHaveBeenCalled(); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll want a test using the keyboard. And we should check onHover and onFocus |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| it('does not expose role=button when no press callbacks are provided', () => { | ||||||||||||||||||||||||
| let {queryByRole} = render( | ||||||||||||||||||||||||
| <Card> | ||||||||||||||||||||||||
| <Content> | ||||||||||||||||||||||||
| <Text slot="title">Static Card</Text> | ||||||||||||||||||||||||
| </Content> | ||||||||||||||||||||||||
| </Card> | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| expect(queryByRole('button')).toBeNull(); | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
Comment on lines
+133
to
+143
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This is a duplicate of the first test |
||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using usePress and useHover etc and specifying role="button" we should just be able to use
useButtonit should take care of the tabIndex and aria-disabled as well