Skip to content

Commit

Permalink
chore: Adjust logic for last item and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
taheramr committed Feb 10, 2025
1 parent dfdedd5 commit 9342646
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
36 changes: 18 additions & 18 deletions src/breadcrumb-group/__integ__/breadcrumb-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,23 @@ describe('BreadcrumbGroup', () => {
)
);

test(
'Last item is focusable when truncated',
setupTest(
async page => {
await page.click('#focus-target-long-text');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(true);
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(false);
await expect(page.getActiveElementId()).resolves.toBe('focus-target-short-text');
},
{ width: 950, height: 800 }
)
test.each([
[{ width: 770, height: 800 }, 'when truncated'],
[{ width: 1200, height: 800 }, 'default state'],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
])('Last item should be focusable %s', (viewportSize, scenario) =>
setupTest(async page => {
await page.click('#focus-target-long-text');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(true);

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 1: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 2: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 3: BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check failure on line 185 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

BreadcrumbGroup › Last item should be focusable { width: 1200

expect(received).resolves.toBe(expected) // Object.is equality Expected: true Received: false at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:185:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13
await page.keys('Tab');
await expect(page.isTooltipDisplayed()).resolves.toBe(false);
await expect(page.getActiveElementId()).resolves.toBe('focus-target-short-text');

Check warning on line 188 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 1: BreadcrumbGroup › Last item should be focusable { width: 770

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:188:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 188 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 2: BreadcrumbGroup › Last item should be focusable { width: 770

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:188:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check warning on line 188 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

RETRY 3: BreadcrumbGroup › Last item should be focusable { width: 770

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:188:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13

Check failure on line 188 in src/breadcrumb-group/__integ__/breadcrumb-group.test.ts

View workflow job for this annotation

GitHub Actions / dry-run / Components integration tests shards (1)

BreadcrumbGroup › Last item should be focusable { width: 770

expect(received).resolves.toBe(expected) // Object.is equality Expected: "focus-target-short-text" Received: "" at Object.toBe (node_modules/expect/build/index.js:174:22) at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:188:56 at src/breadcrumb-group/__integ__/breadcrumb-group.test.ts:72:5 at node_modules/@cloudscape-design/browser-test-tools/use-browser.js:36:13
}, viewportSize)()
);
});
8 changes: 7 additions & 1 deletion src/breadcrumb-group/__tests__/breadcrumb-item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,16 @@ describe('BreadcrumbGroup Item', () => {

test('should not be a link', () => {
expect(lastLink.getElement()).not.toHaveAttribute('href');
expect(lastLink.getElement()).not.toHaveAttribute('aria-current');
expect(lastLink.getElement().tagName).toEqual('SPAN');
});

test('should have proper aria-labels and focusable', () => {
expect(lastLink.getElement()).toHaveAttribute('aria-current');
expect(lastLink.getElement()).toHaveAttribute('aria-disabled');
expect(lastLink.getElement().getAttribute('role')).toBe('link');
expect(lastLink.getElement().getAttribute('tabindex')).toBe('0');
});

test('should not trigger click event', () => {
lastLink.click();
expect(onClickSpy).not.toHaveBeenCalled();
Expand Down
25 changes: 14 additions & 11 deletions src/breadcrumb-group/item/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ const BreadcrumbItemWithPopover = <T extends BreadcrumbGroupProps.Item>({
}}
onMouseLeave={() => setShowPopover(false)}
anchorAttributes={anchorAttributes}
{...(isLast ? { tabIndex: 0 } : {})}
>
{children}
</Item>
Expand All @@ -68,16 +67,25 @@ type ItemProps = React.HTMLAttributes<HTMLElement> & {
isLast: boolean;
};
const Item = React.forwardRef<HTMLElement, ItemProps>(
({ anchorAttributes, children, isLast, ...itemAttributes }, ref) =>
isLast ? (
<span ref={ref} className={styles.anchor} {...itemAttributes}>
({ anchorAttributes, children, isLast, ...itemAttributes }, ref) => {
return isLast ? (
<span
ref={ref}
className={styles.anchor}
role="link"
aria-disabled={true}
aria-current="page"
tabIndex={0}
{...itemAttributes}
>
{children}
</span>
) : (
<a ref={ref as React.Ref<HTMLAnchorElement>} className={styles.anchor} {...itemAttributes} {...anchorAttributes}>
{children}
</a>
)
);
}
);

export function BreadcrumbItem<T extends BreadcrumbGroupProps.Item>({
Expand All @@ -103,16 +111,11 @@ export function BreadcrumbItem<T extends BreadcrumbGroupProps.Item>({
onClick: isLast ? preventDefault : onClickHandler,
tabIndex: 0,
};

if (isGhost) {
anchorAttributes.tabIndex = -1;
}

if (isLast) {
anchorAttributes.role = 'link';
anchorAttributes['aria-disabled'] = true;
anchorAttributes['aria-current'] = 'page';
}

const breadcrumbItem = (
<FunnelBreadcrumbItem
className={styles.text}
Expand Down

0 comments on commit 9342646

Please sign in to comment.