Skip to content

docs(material/card): add card harness example #20834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/components-examples/material/card/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,15 @@ ng_module(
]),
module_name = "@angular/components-examples/material/card",
deps = [
"//src/cdk/testing",
"//src/cdk/testing/testbed",
"//src/material/button",
"//src/material/button/testing",
"//src/material/card",
"//src/material/card/testing",
"@npm//@angular/platform-browser",
"@npm//@angular/platform-browser-dynamic",
"@npm//@types/jasmine",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<mat-card>
</mat-card>
<mat-card>
<mat-card-header>
<div mat-card-avatar></div>
<mat-card-title>Shiba Inu</mat-card-title>
<mat-card-subtitle>Dog Breed</mat-card-subtitle>
</mat-card-header>
<div mat-card-image></div>
<mat-card-content>
<p>
The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from
Japan. A small, agile dog that copes very well with mountainous terrain, the Shiba Inu
was originally bred for hunting.
</p>
</mat-card-content>
<mat-card-actions>
<button mat-button>LIKE</button>
<button mat-button>SHARE</button>
</mat-card-actions>
</mat-card>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {MatButtonHarness} from '@angular/material/button/testing';
import {MatCardHarness} from '@angular/material/card/testing';
import {HarnessLoader} from '@angular/cdk/testing';
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
from '@angular/platform-browser-dynamic/testing';
import {MatCardModule} from '@angular/material/card';
import {CardHarnessExample} from './card-harness-example';

describe('CardHarnessExample', () => {
let fixture: ComponentFixture<CardHarnessExample>;
let loader: HarnessLoader;
beforeAll(() => {
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
});
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatCardModule],
declarations: [CardHarnessExample]
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CardHarnessExample);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});

it('should find card with text', async () => {
const cards = await loader.getAllHarnesses(MatCardHarness.with({text: /spitz breed/}));
expect(cards.length).toBe(1);
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
});

it('should get subtitle text', async () => {
const cards = await loader.getAllHarnesses(MatCardHarness);
expect(await Promise.all(cards.map(c => c.getSubtitleText()))).toEqual([
'',
'Dog Breed'
]);
});

it('should act as a harness loader for user content', async () => {
const card = await loader.getHarness(MatCardHarness.with({title: 'Shiba Inu'}));
const footerSubcomponents = await card.getAllHarnesses(MatButtonHarness) ?? [];
expect(footerSubcomponents.length).toBe(2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Component} from '@angular/core';

/**
* @title Testing with MatCardHarness
*/
@Component({
selector: 'card-harness-example',
templateUrl: 'card-harness-example.html',
})
export class CardHarnessExample {}
3 changes: 3 additions & 0 deletions src/components-examples/material/card/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ import {MatButtonModule} from '@angular/material/button';
import {MatCardModule} from '@angular/material/card';
import {CardFancyExample} from './card-fancy/card-fancy-example';
import {CardOverviewExample} from './card-overview/card-overview-example';
import {CardHarnessExample} from './card-harness/card-harness-example';

export {
CardFancyExample,
CardOverviewExample,
CardHarnessExample,
};

const EXAMPLES = [
CardFancyExample,
CardOverviewExample,
CardHarnessExample,
];

@NgModule({
Expand Down