Skip to content

Commit 36a5663

Browse files
committed
test(aria/toolbar): generate additional tests for Toolbar directives, harnesses and patterns
1 parent deadf7f commit 36a5663

6 files changed

Lines changed: 88 additions & 1 deletion

File tree

goldens/aria/toolbar/testing/index.api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ export class ToolbarWidgetHarness extends ContentContainerComponentHarness<strin
4444
static hostSelector: string;
4545
isActive(): Promise<boolean>;
4646
isDisabled(): Promise<boolean>;
47+
isSelected(): Promise<boolean>;
4748
static with(options?: ToolbarWidgetHarnessFilters): HarnessPredicate<ToolbarWidgetHarness>;
4849
}
4950

5051
// @public
5152
export interface ToolbarWidgetHarnessFilters extends BaseHarnessFilters {
5253
active?: boolean;
54+
selected?: boolean;
5355
text?: string | RegExp;
5456
}
5557

src/aria/private/toolbar/toolbar.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,20 @@ describe('Toolbar Pattern', () => {
163163
return toolbar.inputs.items().find(item => item.value() === value)!;
164164
}
165165

166+
describe('Tabindex', () => {
167+
it('should expose tabIndex signals', () => {
168+
const {toolbar, items} = getPatterns();
169+
170+
expect(items[0].tabIndex()).toBe(0);
171+
expect(items[1].tabIndex()).toBe(-1);
172+
173+
toolbar.onKeydown(right()); // Move focus to item 1
174+
175+
expect(items[0].tabIndex()).toBe(-1);
176+
expect(items[1].tabIndex()).toBe(0);
177+
});
178+
});
179+
166180
describe('Navigation', () => {
167181
describe('with horizontal orientation', () => {
168182
it('should navigate on click (horizontal, ltr)', () => {

src/aria/toolbar/testing/toolbar-harness-filters.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ export interface ToolbarWidgetHarnessFilters extends BaseHarnessFilters {
2121

2222
/** Active state that the widget should match. */
2323
active?: boolean;
24+
25+
/** Selected state that the widget should match. */
26+
selected?: boolean;
2427
}

src/aria/toolbar/testing/toolbar-harness.spec.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,20 @@ describe('ToolbarHarness', () => {
9393
fixture.componentInstance.undoDisabled.set(true);
9494
expect(await widget.isDisabled()).toBe(true);
9595
});
96+
97+
it('should be able to get whether a widget is selected', async () => {
98+
const widget = await loader.getHarness(ToolbarWidgetHarness.with({text: 'Undo'}));
99+
expect(await widget.isSelected()).toBe(false);
100+
101+
await widget.click();
102+
expect(await widget.isSelected()).toBe(true);
103+
});
96104
});
97105

98106
@Component({
99107
template: `
100108
<div ngToolbar [orientation]="orientation()" [disabled]="toolbarDisabled()">
101-
<button ngToolbarWidget value="undo" [disabled]="undoDisabled()">Undo</button>
109+
<button ngToolbarWidget #u="ngToolbarWidget" value="undo" [disabled]="undoDisabled()" [aria-pressed]="u.selected()">Undo</button>
102110
<button ngToolbarWidget value="redo">Redo</button>
103111
104112
<div ngToolbarWidgetGroup>

src/aria/toolbar/testing/toolbar-widget-harness.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export class ToolbarWidgetHarness extends ContentContainerComponentHarness<strin
2828
'active',
2929
options.active,
3030
async (harness, active) => (await harness.isActive()) === active,
31+
)
32+
.addOption(
33+
'selected',
34+
options.selected,
35+
async (harness, selected) => (await harness.isSelected()) === selected,
3136
);
3237
}
3338

@@ -52,4 +57,10 @@ export class ToolbarWidgetHarness extends ContentContainerComponentHarness<strin
5257
const host = await this.host();
5358
return (await host.getAttribute('aria-disabled')) === 'true';
5459
}
60+
61+
/** Gets whether the widget is selected. */
62+
async isSelected(): Promise<boolean> {
63+
const host = await this.host();
64+
return (await host.getAttribute('aria-pressed')) === 'true';
65+
}
5566
}

src/aria/toolbar/toolbar.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,55 @@ describe('Toolbar', () => {
626626
expect(item2.getAttribute('aria-pressed')).toBe('false');
627627
});
628628
});
629+
630+
describe('ARIA attributes and roles', () => {
631+
beforeEach(() => setupToolbar());
632+
633+
it('should have role="toolbar"', () => {
634+
expect(toolbarElement.getAttribute('role')).toBe('toolbar');
635+
});
636+
637+
it('should set aria-orientation based on input', () => {
638+
expect(toolbarElement.getAttribute('aria-orientation')).toBe('horizontal');
639+
fixture.componentInstance.orientation.set('vertical');
640+
fixture.detectChanges();
641+
expect(toolbarElement.getAttribute('aria-orientation')).toBe('vertical');
642+
});
643+
644+
it('should set aria-disabled based on input', () => {
645+
expect(toolbarElement.getAttribute('aria-disabled')).toBe('false');
646+
fixture.componentInstance.disabled.set(true);
647+
fixture.detectChanges();
648+
expect(toolbarElement.getAttribute('aria-disabled')).toBe('true');
649+
});
650+
});
651+
652+
describe('Focus management', () => {
653+
beforeEach(() => setupToolbar());
654+
655+
it('should have tabindex on widgets set by active state', () => {
656+
const widgets = getWidgetEls();
657+
expect(widgets[0].getAttribute('tabindex')).toBe('0');
658+
expect(widgets[1].getAttribute('tabindex')).toBe('-1');
659+
660+
click(widgets[1]);
661+
expect(widgets[0].getAttribute('tabindex')).toBe('-1');
662+
expect(widgets[1].getAttribute('tabindex')).toBe('0');
663+
});
664+
});
665+
666+
describe('Hard disabled state attributes', () => {
667+
beforeEach(() => setupToolbar({softDisabled: false}));
668+
669+
it('should set inert and disabled attributes on hard-disabled widgets', () => {
670+
fixture.componentInstance.widgets[0].disabled.set(true);
671+
fixture.detectChanges();
672+
673+
const widgets = getWidgetEls();
674+
expect(widgets[0].hasAttribute('inert')).toBe(true);
675+
expect(widgets[0].getAttribute('disabled')).toBe('true');
676+
});
677+
});
629678
});
630679

631680
@Component({

0 commit comments

Comments
 (0)