Skip to content

Commit ee7c8bf

Browse files
feat(components/indicators): update alert test harness with accessibility functions (#500)
1 parent 0d44f00 commit ee7c8bf

File tree

3 files changed

+133
-9
lines changed

3 files changed

+133
-9
lines changed

libs/components/indicators/testing/src/alert/alert-harness.spec.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
22
import { Component } from '@angular/core';
33
import { ComponentFixture, TestBed } from '@angular/core/testing';
4-
import { SkyAlertModule, SkyIndicatorIconType } from '@skyux/indicators';
4+
import {
5+
SkyAlertModule,
6+
SkyIndicatorDescriptionType,
7+
SkyIndicatorIconType,
8+
} from '@skyux/indicators';
59

610
import { SkyAlertHarness } from './alert-harness';
711

@@ -13,6 +17,8 @@ import { SkyAlertHarness } from './alert-harness';
1317
[alertType]="alertType"
1418
[closeable]="closeable"
1519
[closed]="closed"
20+
[customDescription]="customDescription"
21+
[descriptionType]="descriptionType"
1622
(closedChange)="closedChange()"
1723
data-sky-id="test-alert"
1824
>
@@ -28,6 +34,10 @@ class TestComponent {
2834

2935
public closed = false;
3036

37+
public customDescription: string;
38+
39+
public descriptionType: SkyIndicatorDescriptionType;
40+
3141
public closedChange() {
3242
// Only exists for the spy.
3343
}
@@ -44,6 +54,21 @@ async function validateAlertType(
4454
await expectAsync(alertHarness.getAlertType()).toBeResolvedTo(alertType);
4555
}
4656

57+
async function validateDescriptionType(
58+
alertHarness: SkyAlertHarness,
59+
fixture: ComponentFixture<TestComponent>,
60+
descriptionType: SkyIndicatorDescriptionType,
61+
customDescription?: string
62+
) {
63+
fixture.componentInstance.descriptionType = descriptionType;
64+
fixture.componentInstance.customDescription = customDescription;
65+
fixture.detectChanges();
66+
67+
const componentDesciptionType = await alertHarness.getDescriptionType();
68+
69+
expect(componentDesciptionType).toEqual(descriptionType);
70+
}
71+
4772
describe('Alert harness', () => {
4873
async function setupTest(options: { dataSkyId?: string } = {}) {
4974
await TestBed.configureTestingModule({
@@ -116,6 +141,52 @@ describe('Alert harness', () => {
116141
await validateAlertType(alertHarness, fixture, 'warning');
117142
});
118143

144+
it('should return the expected description type', async () => {
145+
const { alertHarness, fixture } = await setupTest();
146+
await validateDescriptionType(alertHarness, fixture, 'attention');
147+
await validateDescriptionType(alertHarness, fixture, 'caution');
148+
await validateDescriptionType(alertHarness, fixture, 'completed');
149+
await validateDescriptionType(alertHarness, fixture, 'danger');
150+
await validateDescriptionType(alertHarness, fixture, 'error');
151+
await validateDescriptionType(alertHarness, fixture, 'important-info');
152+
await validateDescriptionType(alertHarness, fixture, 'important-warning');
153+
await validateDescriptionType(alertHarness, fixture, 'success');
154+
await validateDescriptionType(alertHarness, fixture, 'warning');
155+
await validateDescriptionType(alertHarness, fixture, 'none');
156+
await validateDescriptionType(
157+
alertHarness,
158+
fixture,
159+
'custom',
160+
'custom text'
161+
);
162+
});
163+
164+
it('should return the custom description when `descriptionType` is custom', async () => {
165+
const { fixture, alertHarness } = await setupTest();
166+
const description = 'Custom description:';
167+
168+
fixture.componentInstance.descriptionType = 'custom';
169+
fixture.componentInstance.customDescription = description;
170+
171+
fixture.detectChanges();
172+
173+
const componentDesciption = await alertHarness.getCustomDescription();
174+
175+
expect(componentDesciption).toEqual(description);
176+
});
177+
178+
it('should return an empty string when `descriptionType` is not custom', async () => {
179+
const { fixture, alertHarness } = await setupTest();
180+
181+
fixture.componentInstance.descriptionType = 'attention';
182+
183+
fixture.detectChanges();
184+
185+
const componentDesciption = await alertHarness.getCustomDescription();
186+
187+
expect(componentDesciption).toEqual('');
188+
});
189+
119190
it('should return the expected text', async () => {
120191
const { alertHarness } = await setupTest();
121192
await expectAsync(alertHarness.getText()).toBeResolvedTo(

libs/components/indicators/testing/src/alert/alert-harness.ts

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { HarnessPredicate } from '@angular/cdk/testing';
22
import { SkyComponentHarness } from '@skyux/core/testing';
3-
import { SkyIndicatorIconType } from '@skyux/indicators';
3+
import type {
4+
SkyIndicatorDescriptionType,
5+
SkyIndicatorIconType,
6+
} from '@skyux/indicators';
47

58
import { SkyAlertHarnessFilters } from './alert-harness-filters';
69

@@ -13,6 +16,7 @@ export class SkyAlertHarness extends SkyComponentHarness {
1316
#getAlert = this.locatorFor('.sky-alert');
1417
#getContent = this.locatorFor('.sky-alert-content');
1518
#getCloseButton = this.locatorFor('.sky-alert-close');
19+
#getScreenReaderTextEl = this.locatorForOptional('.sky-screen-reader-only');
1620

1721
/**
1822
* Gets a `HarnessPredicate` that can be used to search for a
@@ -42,11 +46,7 @@ export class SkyAlertHarness extends SkyComponentHarness {
4246
return 'success';
4347
}
4448

45-
if (await alert.hasClass('sky-alert-warning')) {
46-
return 'warning';
47-
}
48-
49-
return undefined;
49+
return 'warning';
5050
}
5151

5252
/**
@@ -83,4 +83,57 @@ export class SkyAlertHarness extends SkyComponentHarness {
8383
const closeBtn = await this.#getCloseButton();
8484
return !(await closeBtn.getProperty('hidden'));
8585
}
86+
87+
/**
88+
* Gets the `descriptionType` of the label component.
89+
*/
90+
public async getDescriptionType(): Promise<SkyIndicatorDescriptionType> {
91+
const srEl = await this.#getScreenReaderTextEl();
92+
93+
if (!srEl) {
94+
return 'none';
95+
}
96+
97+
const srText = await srEl.text();
98+
99+
switch (srText) {
100+
case 'Attention:':
101+
return 'attention';
102+
case 'Caution:':
103+
return 'caution';
104+
case 'Completed:':
105+
return 'completed';
106+
case 'Danger:':
107+
return 'danger';
108+
case 'Error:':
109+
return 'error';
110+
case 'Important information:':
111+
return 'important-info';
112+
case 'Important warning:':
113+
return 'important-warning';
114+
case 'Success:':
115+
return 'success';
116+
case 'Warning:':
117+
return 'warning';
118+
default:
119+
return 'custom';
120+
}
121+
}
122+
123+
/**
124+
* Gets the custom text used for the screen reader description of the label component icon.
125+
*/
126+
public async getCustomDescription(): Promise<string> {
127+
const descriptionType = await this.getDescriptionType();
128+
129+
if (descriptionType === 'custom') {
130+
const srEl = await this.#getScreenReaderTextEl();
131+
132+
if (srEl) {
133+
return await srEl.text();
134+
}
135+
}
136+
137+
return '';
138+
}
86139
}

libs/components/indicators/testing/src/label/label-harness.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ describe('Label harness', () => {
124124

125125
fixture.detectChanges();
126126

127-
const componentDesciptionType = await labelHarness.getCustomDescription();
127+
const componentDesciption = await labelHarness.getCustomDescription();
128128

129-
expect(componentDesciptionType).toEqual(description);
129+
expect(componentDesciption).toEqual(description);
130130
});
131131

132132
it('should return an empty string when `descriptionType` is not custom', async () => {

0 commit comments

Comments
 (0)