Skip to content

Commit f460c76

Browse files
committed
docs(material/card): add card harness example
1 parent 0b2d150 commit f460c76

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ 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",
17+
"//src/material/button/testing",
1518
"//src/material/card",
19+
"//src/material/card/testing",
20+
"@npm//@angular/platform-browser",
21+
"@npm//@angular/platform-browser-dynamic",
22+
"@npm//@types/jasmine",
1623
],
1724
)
1825

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatButtonHarness} from '@angular/material/button/testing';
4+
import {MatCardHarness} from '@angular/material/card/testing';
5+
import {HarnessLoader} from '@angular/cdk/testing';
6+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
7+
from '@angular/platform-browser-dynamic/testing';
8+
import {MatCardModule} from '@angular/material/card';
9+
import {CardHarnessExample} from './card-harness-example';
10+
11+
describe('CardHarnessExample', () => {
12+
let fixture: ComponentFixture<CardHarnessExample>;
13+
let loader: HarnessLoader;
14+
beforeAll(() => {
15+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
16+
});
17+
beforeEach(
18+
waitForAsync(() => {
19+
TestBed.configureTestingModule({
20+
imports: [MatCardModule],
21+
declarations: [CardHarnessExample]
22+
}).compileComponents();
23+
}));
24+
25+
beforeEach(() => {
26+
fixture = TestBed.createComponent(CardHarnessExample);
27+
fixture.detectChanges();
28+
loader = TestbedHarnessEnvironment.loader(fixture);
29+
});
30+
31+
it('should find card with text', async () => {
32+
const cards = await loader.getAllHarnesses(MatCardHarness.with({text: /spitz breed/}));
33+
expect(cards.length).toBe(1);
34+
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
35+
});
36+
37+
it('should get subtitle text', async () => {
38+
const cards = await loader.getAllHarnesses(MatCardHarness);
39+
expect(await Promise.all(cards.map(c => c.getSubtitleText()))).toEqual([
40+
'',
41+
'Dog Breed'
42+
]);
43+
});
44+
45+
it('should act as a harness loader for user content', async () => {
46+
const card = await loader.getHarness(MatCardHarness.with({title: 'Shiba Inu'}));
47+
const footerSubcomponents = await card.getAllHarnesses(MatButtonHarness) ?? [];
48+
expect(footerSubcomponents.length).toBe(2);
49+
});
50+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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 {}

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)