Skip to content

Commit 4ba11a0

Browse files
feat(components/indicators): add key info component harness (#498)
1 parent 4be1e9f commit 4ba11a0

File tree

12 files changed

+187
-4
lines changed

12 files changed

+187
-4
lines changed

libs/components/indicators/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export * from './lib/modules/icon/icon.module';
1111
export * from './lib/modules/icon/types/icon-type';
1212
export * from './lib/modules/icon/types/icon-variant-type';
1313

14+
export * from './lib/modules/key-info/key-info-layout-type';
1415
export * from './lib/modules/key-info/key-info.module';
1516

1617
export * from './lib/modules/label/label-type';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type SkyKeyInfoLayoutType = 'horizontal' | 'vertical';

libs/components/indicators/src/lib/modules/key-info/key-info.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Component, Input } from '@angular/core';
22

3+
import { SkyKeyInfoLayoutType } from './key-info-layout-type';
4+
35
@Component({
46
selector: 'sky-key-info',
57
templateUrl: './key-info.component.html',
@@ -13,5 +15,5 @@ export class SkyKeyInfoComponent {
1315
*/
1416
// TODO: More strongly type this in a future breaking change.
1517
@Input()
16-
public layout: string | undefined = 'vertical';
18+
public layout: SkyKeyInfoLayoutType | string | undefined = 'vertical';
1719
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<sky-key-info data-sky-id="vertical-key-info">
2+
<sky-key-info-value>100</sky-key-info-value>
3+
<sky-key-info-label>Vertical label</sky-key-info-label>
4+
</sky-key-info>
5+
<sky-key-info [layout]="horizontalLayout" data-sky-id="horizontal-key-info">
6+
<sky-key-info-value>200</sky-key-info-value>
7+
<sky-key-info-label>Horizontal label</sky-key-info-label>
8+
</sky-key-info>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Component } from '@angular/core';
2+
import { SkyKeyInfoLayoutType } from '@skyux/indicators';
3+
4+
@Component({
5+
selector: 'sky-test-key-info',
6+
templateUrl: './key-info-harness-test.component.html',
7+
})
8+
export class KeyInfoHarnessTestComponent {
9+
public horizontalLayout: SkyKeyInfoLayoutType = 'horizontal';
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CommonModule } from '@angular/common';
2+
import { NgModule } from '@angular/core';
3+
import { SkyKeyInfoModule } from '@skyux/indicators';
4+
5+
import { KeyInfoHarnessTestComponent } from './key-info-harness-test.component';
6+
7+
@NgModule({
8+
declarations: [KeyInfoHarnessTestComponent],
9+
imports: [CommonModule, SkyKeyInfoModule],
10+
exports: [KeyInfoHarnessTestComponent],
11+
})
12+
export class KeyInfoHarnessTestModule {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { SkyHarnessFilters } from '@skyux/core/testing';
2+
3+
/**
4+
* A set of criteria that can be used to filter a list of SkyKeyInfoHarness instances.
5+
* @internal
6+
*/
7+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
8+
export interface SkyKeyInfoHarnessFilters extends SkyHarnessFilters {}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
2+
import { TestBed } from '@angular/core/testing';
3+
4+
import { KeyInfoHarnessTestComponent } from './fixtures/key-info-harness-test.component';
5+
import { KeyInfoHarnessTestModule } from './fixtures/key-info-harness-test.module';
6+
import { SkyKeyInfoHarness } from './key-info-harness';
7+
8+
describe('Key Info harness', () => {
9+
async function setupTest(options: { dataSkyId?: string } = {}) {
10+
const fixture = TestBed.createComponent(KeyInfoHarnessTestComponent);
11+
const loader = TestbedHarnessEnvironment.loader(fixture);
12+
13+
const keyInfoHarness = await loader.getHarness(
14+
SkyKeyInfoHarness.with({ dataSkyId: options.dataSkyId })
15+
);
16+
17+
return { fixture, keyInfoHarness };
18+
}
19+
20+
beforeEach(() => {
21+
TestBed.configureTestingModule({
22+
imports: [KeyInfoHarnessTestModule],
23+
});
24+
});
25+
26+
it('should return properties for a horizontal layout', async () => {
27+
const { keyInfoHarness } = await setupTest({
28+
dataSkyId: 'horizontal-key-info',
29+
});
30+
31+
await expectAsync(keyInfoHarness.getLayout()).toBeResolvedTo('horizontal');
32+
await expectAsync(keyInfoHarness.getValueText()).toBeResolvedTo('200');
33+
await expectAsync(keyInfoHarness.getLabelText()).toBeResolvedTo(
34+
'Horizontal label'
35+
);
36+
});
37+
38+
it('should return properties for a vertical layout', async () => {
39+
const { keyInfoHarness } = await setupTest({
40+
dataSkyId: 'vertical-key-info',
41+
});
42+
43+
await expectAsync(keyInfoHarness.getLayout()).toBeResolvedTo('vertical');
44+
await expectAsync(keyInfoHarness.getValueText()).toBeResolvedTo('100');
45+
await expectAsync(keyInfoHarness.getLabelText()).toBeResolvedTo(
46+
'Vertical label'
47+
);
48+
});
49+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { HarnessPredicate } from '@angular/cdk/testing';
2+
import { SkyComponentHarness } from '@skyux/core/testing';
3+
import { SkyKeyInfoLayoutType } from '@skyux/indicators';
4+
5+
import { SkyKeyInfoHarnessFilters } from './key-info-harness-filters';
6+
import { SkyKeyInfoLabelHarness } from './key-info-label-harness';
7+
import { SkyKeyInfoValueHarness } from './key-info-value-harness';
8+
9+
/**
10+
* Harness for interacting with a key info component in tests.
11+
* @internal
12+
*/
13+
export class SkyKeyInfoHarness extends SkyComponentHarness {
14+
public static hostSelector = 'sky-key-info';
15+
16+
#getLabel = this.locatorFor(SkyKeyInfoLabelHarness);
17+
#getValue = this.locatorFor(SkyKeyInfoValueHarness);
18+
#getWrapper = this.locatorFor('.sky-key-info');
19+
20+
/**
21+
* Gets a `HarnessPredicate` that can be used to search for a
22+
* `SkyKeyInfoHarness` that meets certain criteria.
23+
*/
24+
public static with(
25+
filters: SkyKeyInfoHarnessFilters
26+
): HarnessPredicate<SkyKeyInfoHarness> {
27+
return SkyKeyInfoHarness.getDataSkyIdPredicate(filters);
28+
}
29+
30+
/**
31+
* Gets the current value text.
32+
*/
33+
public async getValueText(): Promise<string> {
34+
return await (await this.#getValue()).getText();
35+
}
36+
37+
/**
38+
* Gets the current label text.
39+
*/
40+
public async getLabelText(): Promise<string> {
41+
return await (await this.#getLabel()).getText();
42+
}
43+
44+
/**
45+
* Gets the current layout type.
46+
*/
47+
public async getLayout(): Promise<SkyKeyInfoLayoutType> {
48+
return (await (
49+
await this.#getWrapper()
50+
).hasClass('sky-key-info-horizontal'))
51+
? 'horizontal'
52+
: 'vertical';
53+
}
54+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SkyComponentHarness } from '@skyux/core/testing';
2+
3+
/**
4+
* Harness for interacting with a key info label component in tests.
5+
* @internal
6+
*/
7+
export class SkyKeyInfoLabelHarness extends SkyComponentHarness {
8+
public static hostSelector = 'sky-key-info-label';
9+
10+
/**
11+
* Gets the text value of the component content.
12+
*/
13+
public async getText(): Promise<string> {
14+
return (await this.host()).text();
15+
}
16+
}

0 commit comments

Comments
 (0)