Skip to content

Commit 5c4d473

Browse files
committed
docs(material/card): add card harness example
1 parent e03a4d6 commit 5c4d473

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

src/components-examples/material/card/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/card",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/button",
1517
"//src/material/card",
18+
"//src/material/card/testing",
19+
"@npm//@angular/platform-browser",
20+
"@npm//@angular/platform-browser-dynamic",
21+
"@npm//@types/jasmine",
1622
],
1723
)
1824

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<mat-card>
2+
</mat-card>
3+
<mat-card>
4+
<mat-card-header>
5+
<div mat-card-avatar></div>
6+
<mat-card-title>Shiba Inu</mat-card-title>
7+
<mat-card-subtitle>Dog Breed</mat-card-subtitle>
8+
</mat-card-header>
9+
<div mat-card-image></div>
10+
<mat-card-content>
11+
<p>
12+
The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from
13+
Japan. A small, agile dog that copes very well with mountainous terrain, the Shiba Inu
14+
was originally bred for hunting.
15+
</p>
16+
</mat-card-content>
17+
<mat-card-actions>
18+
<button mat-button>LIKE</button>
19+
<button mat-button>SHARE</button>
20+
</mat-card-actions>
21+
<mat-card-footer>
22+
<div>Woof woof!</div>
23+
</mat-card-footer>
24+
</mat-card>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatCardHarness} from '@angular/material/card/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatCardModule} from '@angular/material/card';
8+
import {CardHarnessExample, DummyHarness} from './card-harness-example';
9+
10+
describe('CardHarnessExample', () => {
11+
let fixture: ComponentFixture<CardHarnessExample>;
12+
let loader: HarnessLoader;
13+
beforeAll(() => {
14+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
15+
});
16+
beforeEach(
17+
waitForAsync(() => {
18+
TestBed.configureTestingModule({
19+
imports: [MatCardModule],
20+
declarations: [CardHarnessExample]
21+
}).compileComponents();
22+
}));
23+
24+
beforeEach(() => {
25+
fixture = TestBed.createComponent(CardHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
});
29+
30+
it('should find card with text', async () => {
31+
const cards = await loader.getAllHarnesses(MatCardHarness.with({text: /spitz breed/}));
32+
expect(cards.length).toBe(1);
33+
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
34+
});
35+
36+
it('should get subtitle text', async () => {
37+
const cards = await loader.getAllHarnesses(MatCardHarness);
38+
expect(await Promise.all(cards.map(c => c.getSubtitleText()))).toEqual([
39+
'',
40+
'Dog Breed'
41+
]);
42+
});
43+
44+
it('should act as a harness loader for user content', async () => {
45+
const card = await loader.getHarness(MatCardHarness.with({title: 'Shiba Inu'}));
46+
const footerSubcomponents = await card.getAllHarnesses(DummyHarness) ?? [];
47+
expect(footerSubcomponents.length).toBe(7);
48+
});
49+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Component} from '@angular/core';
2+
import {ComponentHarness} from '@angular/cdk/testing';
3+
4+
/**
5+
* @title Testing with MatCardHarness
6+
*/
7+
@Component({
8+
selector: 'card-harness-example',
9+
templateUrl: 'card-harness-example.html',
10+
})
11+
export class CardHarnessExample {}
12+
13+
export class DummyHarness extends ComponentHarness {
14+
static hostSelector = 'div, p, button';
15+
}

src/components-examples/material/card/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@ import {MatButtonModule} from '@angular/material/button';
33
import {MatCardModule} from '@angular/material/card';
44
import {CardFancyExample} from './card-fancy/card-fancy-example';
55
import {CardOverviewExample} from './card-overview/card-overview-example';
6+
import {CardHarnessExample} from './card-harness/card-harness-example';
67

78
export {
89
CardFancyExample,
910
CardOverviewExample,
11+
CardHarnessExample,
1012
};
1113

1214
const EXAMPLES = [
1315
CardFancyExample,
1416
CardOverviewExample,
17+
CardHarnessExample,
1518
];
1619

1720
@NgModule({

0 commit comments

Comments
 (0)