-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
shared.spec.ts
143 lines (125 loc) · 5.6 KB
/
shared.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {ComponentHarness, HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {MatCardModule} from '@angular/material/card';
import {MatCardHarness, MatCardSection} from '@angular/material/card/testing/card-harness';
/** Shared tests to run on both the original and MDC-based cards. */
export function runHarnessTests(
cardModule: typeof MatCardModule,
cardHarness: typeof MatCardHarness,
contentSelectors: {header: string, content: string, actions: string, footer: string}) {
let fixture: ComponentFixture<CardHarnessTest>;
let loader: HarnessLoader;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [cardModule],
declarations: [CardHarnessTest],
}).compileComponents();
fixture = TestBed.createComponent(CardHarnessTest);
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);
});
it('should find all cards', async () => {
const cards = await loader.getAllHarnesses(cardHarness);
expect(cards.length).toBe(2);
});
it('should find card with text', async () => {
const cards = await loader.getAllHarnesses(cardHarness.with({text: /spitz breed/}));
expect(cards.length).toBe(1);
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
});
it('should find card with title', async () => {
const cards = await loader.getAllHarnesses(cardHarness.with({title: 'Shiba Inu'}));
expect(cards.length).toBe(1);
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
});
it('should find card with subtitle', async () => {
const cards = await loader.getAllHarnesses(cardHarness.with({subtitle: 'Dog Breed'}));
expect(cards.length).toBe(1);
expect(await cards[0].getTitleText()).toBe('Shiba Inu');
});
it('should get card text', async () => {
const cards = await loader.getAllHarnesses(cardHarness);
expect(await Promise.all(cards.map(c => c.getText()))).toEqual([
'',
'Shiba InuDog Breed 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. LIKESHAREWoof woof!'
]);
});
it('should get title text', async () => {
const cards = await loader.getAllHarnesses(cardHarness);
expect(await Promise.all(cards.map(c => c.getTitleText()))).toEqual([
'',
'Shiba Inu'
]);
});
it('should get subtitle text', async () => {
const cards = await loader.getAllHarnesses(cardHarness);
expect(await Promise.all(cards.map(c => c.getSubtitleText()))).toEqual([
'',
'Dog Breed'
]);
});
it('should get a harness loader for the card header', async () => {
const card = await loader.getHarness(cardHarness.with({title: 'Shiba Inu'}));
const headerLoader = await card.getChildLoader(contentSelectors.header as MatCardSection);
const headerSubcomponents = await headerLoader?.getAllHarnesses(DummyHarness) ?? [];
expect(headerSubcomponents.length).toBe(2);
});
it('should get a harness loader for the card content', async () => {
const card = await loader.getHarness(cardHarness.with({title: 'Shiba Inu'}));
const contentLoader = await card.getChildLoader(contentSelectors.content as MatCardSection);
const contentSubcomponents = await contentLoader?.getAllHarnesses(DummyHarness) ?? [];
expect(contentSubcomponents.length).toBe(1);
});
it('should get a harness loader for the card actions', async () => {
const card = await loader.getHarness(cardHarness.with({title: 'Shiba Inu'}));
const actionLoader = await card.getChildLoader(contentSelectors.actions as MatCardSection);
const actionSubcomponents = await actionLoader?.getAllHarnesses(DummyHarness) ?? [];
expect(actionSubcomponents.length).toBe(2);
});
it('should get a harness loader for the card footer', async () => {
const card = await loader.getHarness(cardHarness.with({title: 'Shiba Inu'}));
const footerLoader = await card.getChildLoader(contentSelectors.footer as MatCardSection);
const footerSubcomponents = await footerLoader?.getAllHarnesses(DummyHarness) ?? [];
expect(footerSubcomponents.length).toBe(1);
});
it('should act as a harness loader for user content', async () => {
const card = await loader.getHarness(cardHarness.with({title: 'Shiba Inu'}));
const footerSubcomponents = await card.getAllHarnesses(DummyHarness) ?? [];
expect(footerSubcomponents.length).toBe(7);
});
}
@Component({
template: `
<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-footer>
<div>Woof woof!</div>
</mat-card-footer>
</mat-card>
`
})
class CardHarnessTest {}
export class DummyHarness extends ComponentHarness {
static hostSelector = 'div, p, button';
}