Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(home): describe template card actions for screen readers (#43660)

yyhab53c19145ea
diff --git a/web/features/home/__tests__/template-card.spec.tsx b/web/features/home/__tests__/template-card.spec.tsx
index 9b72f33152f..10e9cf6e2fb 100644
--- a/web/features/home/__tests__/template-card.spec.tsx
+++ b/web/features/home/__tests__/template-card.spec.tsx
@@ -83,6 +83,7 @@ describe('TemplateCard', () => {
[AppModeEnum.CHAT, 'APP.TYPES.CHATBOT'],
[AppModeEnum.ADVANCED_CHAT, 'APP.TYPES.ADVANCED'],
[AppModeEnum.AGENT_CHAT, 'APP.TYPES.AGENT'],
+ [AppModeEnum.AGENT, 'APP.TYPES.AGENT'],
[AppModeEnum.WORKFLOW, 'APP.TYPES.WORKFLOW'],
[AppModeEnum.COMPLETION, 'APP.TYPES.COMPLETION'],
])('should render correct mode label for %s mode', (mode, label) => {
@@ -108,6 +109,39 @@ describe('TemplateCard', () => {
})
describe('User Interactions', () => {
+ it.each([
+ ['CLOUD', true, 'explore.appCard.try'],
+ ['CLOUD', false, 'explore.appCard.try'],
+ ['COMMUNITY', true, 'explore.tryApp.createFromSampleApp'],
+ ] as const)(
+ 'describes the Agent card action on %s with canCreate=%s',
+ async (edition, canCreate, action) => {
+ const user = userEvent.setup()
+ deploymentEdition = edition
+ const templateName = 'An Agent template with a full descriptive name'
+ const app = createApp({
+ app: { mode: AppModeEnum.AGENT, name: templateName },
+ description:
+ 'A full template description that remains available when visually truncated.',
+ })
+
+ renderComponent({ app, canCreate })
+
+ const cardButton = screen.getByRole('button', { name: templateName })
+ expect(cardButton).toHaveAccessibleDescription(`${action} ${app.description}`)
+
+ await user.click(cardButton)
+
+ if (edition === 'CLOUD') {
+ expect(onTry).toHaveBeenCalledWith(app)
+ expect(onCreate).not.toHaveBeenCalled()
+ } else {
+ expect(onCreate).toHaveBeenCalledTimes(1)
+ expect(onTry).not.toHaveBeenCalled()
+ }
+ },
+ )
+
it('should make the app card clickable on cloud edition', () => {
renderComponent({
app: createApp({ app: { ...createApp().app, mode: AppModeEnum.WORKFLOW } }),
@@ -122,8 +156,8 @@ describe('TemplateCard', () => {
it('should not render hover action buttons', () => {
renderComponent({ canCreate: true })
- expect(screen.queryByText('explore.appCard.addToWorkspace')).not.toBeInTheDocument()
- expect(screen.queryByText('explore.appCard.try')).not.toBeInTheDocument()
+ expect(screen.getAllByRole('button')).toHaveLength(1)
+ expect(screen.getByRole('button', { name: 'Sample App' })).toBeInTheDocument()
})
it('should make the app card clickable outside cloud edition when create is allowed', () => {
@@ -150,6 +184,24 @@ describe('TemplateCard', () => {
})
describe('Edge Cases', () => {
+ it.each([
+ ['CLOUD', 'explore.appCard.try'],
+ ['COMMUNITY', 'explore.tryApp.createFromSampleApp'],
+ ] as const)(
+ 'describes the card action without a template description on %s',
+ (edition, action) => {
+ deploymentEdition = edition
+ renderComponent({
+ app: createApp({ app: { mode: AppModeEnum.AGENT }, description: '' }),
+ canCreate: true,
+ })
+
+ expect(screen.getByRole('button', { name: 'Sample App' })).toHaveAccessibleDescription(
+ action,
+ )
+ },
+ )
+
it('should truncate long app name with title attribute', () => {
const longName = 'A Very Long Application Name That Should Be Truncated'
renderComponent({ app: createApp({ app: { ...createApp().app, name: longName } }) })
@@ -188,14 +240,15 @@ describe('TemplateCard', () => {
expect(mockTrackEvent).not.toHaveBeenCalled()
})
- it('should call the card action when Enter is pressed on app card', async () => {
+ it.each(['{Enter}', ' '])('opens an Agent template with %s from the keyboard', async (key) => {
const user = userEvent.setup()
- const app = createApp()
+ const app = createApp({ app: { mode: AppModeEnum.AGENT } })
renderComponent({ app, canCreate: true })
- screen.getByRole('button', { name: 'Sample App' }).focus()
- await user.keyboard('{Enter}')
+ await user.tab()
+ expect(screen.getByRole('button', { name: 'Sample App' })).toHaveFocus()
+ await user.keyboard(key)
expect(onTry).toHaveBeenCalledWith(app)
})
diff --git a/web/features/home/template-card.tsx b/web/features/home/template-card.tsx
index 28270912a4c..a55c7a22239 100644
--- a/web/features/home/template-card.tsx
+++ b/web/features/home/template-card.tsx
@@ -18,12 +18,13 @@ type TemplateCardProps = {
}
export function TemplateCard({ app, canCreate, onCreate, onTry }: TemplateCardProps) {
- const { t } = useTranslation(['app'])
+ const { t } = useTranslation(['app', 'explore'])
const { data: deploymentEdition } = useSuspenseQuery({
...systemFeaturesQueryOptions(),
select: ({ deployment_edition }) => deployment_edition,
})
const nameId = useId()
+ const actionDescriptionId = useId()
const descriptionId = useId()
const appBasicInfo = app.app
const appName = appBasicInfo?.name ?? ''
@@ -67,9 +68,17 @@ export function TemplateCard({ app, canCreate, onCreate, onTry }: TemplateCardPr
type="button"
className="absolute inset-0 z-10 cursor-pointer appearance-none rounded-xl border-0 bg-transparent p-0 outline-hidden focus-visible:inset-ring-2 focus-visible:inset-ring-state-accent-solid"
aria-labelledby={nameId}
- aria-describedby={app.description ? descriptionId : undefined}
+ aria-describedby={
+ app.description ? `${actionDescriptionId} ${descriptionId}` : actionDescriptionId
+ }
onClick={handleCardClick}
- />
+ >
+ <span id={actionDescriptionId} className="sr-only">
+ {canViewApp
+ ? t(($) => $['appCard.try'], { ns: 'explore' })
+ : t(($) => $['tryApp.createFromSampleApp'], { ns: 'explore' })}
+ </span>
+ </button>
)}
<div className="flex shrink-0 items-center gap-3 px-4 pt-4 pb-2">
<div className="relative shrink-0">