Skip to content

Commit 8447636

Browse files
committed
nested components tests added
1 parent bfd10b1 commit 8447636

2 files changed

Lines changed: 105 additions & 10 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Component inside a test host
2+
3+
`DashboardComponent` hosts `DashboardQuoteComponent` collection. In the following test we test that a collection is render as we expected.
4+
5+
Update `dashboard/dashboard.component.spec.ts`
6+
7+
```ts
8+
import { ComponentFixture, TestBed } from "@angular/core/testing";
9+
10+
import { DashboardComponent } from "./dashboard.component";
11+
import { DashboardQuoteComponent } from "./dashboard-quote.component";
12+
import { TwainService } from "../twain/twain.service";
13+
import { provideHttpClient } from "@angular/common/http";
14+
import { Quote } from "../twain/quote";
15+
import { asyncData } from "../async-observable-helpers";
16+
17+
describe("DashboardComponent", () => {
18+
let component: DashboardComponent;
19+
let fixture: ComponentFixture<DashboardComponent>;
20+
let testQuotes: Quote[];
21+
let getQuotesSpy: jasmine.Spy;
22+
23+
beforeEach(async () => {
24+
await TestBed.configureTestingModule({
25+
imports: [DashboardComponent, DashboardQuoteComponent],
26+
providers: [TwainService, provideHttpClient()],
27+
}).compileComponents();
28+
testQuotes = [
29+
{ id: 1, quote: "Foo quote" },
30+
{ id: 2, quote: "Boo quote" },
31+
{ id: 3, quote: "Other quote" },
32+
];
33+
34+
// Create a fake TwainService (async) object
35+
const twainService = TestBed.inject(TwainService);
36+
getQuotesSpy = spyOn(twainService, "getQuotes").and.returnValue(
37+
asyncData(testQuotes)
38+
);
39+
40+
fixture = TestBed.createComponent(DashboardComponent);
41+
component = fixture.componentInstance;
42+
});
43+
44+
it("should display a collection of DashboardQuoteComponents", async () => {
45+
fixture.detectChanges(); // ngOnInit
46+
// expect no elements
47+
await fixture.whenStable();
48+
fixture.detectChanges(); // update view with quote
49+
const quoteElements = fixture.nativeElement.querySelectorAll(".quote");
50+
expect(quoteElements.length).toBe(2);
51+
});
52+
53+
it("should display a title with that contains Top 2", async () => {
54+
fixture.detectChanges(); // ngOnInit
55+
// expect no elements
56+
await fixture.whenStable();
57+
fixture.detectChanges(); // update view with quote
58+
expect(component.title).toContain("Top 2 quotes");
59+
});
60+
});
61+
```
62+
63+
```bash
64+
npx ng test --include app/dashboard/dashboard.component.spec.ts
65+
```
Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
22

3-
import { DashboardComponent } from './dashboard.component';
3+
import { DashboardComponent } from "./dashboard.component";
4+
import { DashboardQuoteComponent } from "./dashboard-quote.component";
5+
import { TwainService } from "../twain/twain.service";
6+
import { provideHttpClient } from "@angular/common/http";
7+
import { Quote } from "../twain/quote";
8+
import { asyncData } from "../async-observable-helpers";
49

5-
describe('DashboardComponent', () => {
10+
describe("DashboardComponent", () => {
611
let component: DashboardComponent;
712
let fixture: ComponentFixture<DashboardComponent>;
13+
let testQuotes: Quote[];
14+
let getQuotesSpy: jasmine.Spy;
815

916
beforeEach(async () => {
1017
await TestBed.configureTestingModule({
11-
imports: [DashboardComponent]
12-
})
13-
.compileComponents();
18+
imports: [DashboardComponent, DashboardQuoteComponent],
19+
providers: [TwainService, provideHttpClient()],
20+
}).compileComponents();
21+
testQuotes = [
22+
{ id: 1, quote: "Foo quote" },
23+
{ id: 2, quote: "Boo quote" },
24+
{ id: 3, quote: "Other quote" },
25+
];
26+
27+
// Create a fake TwainService (async) object
28+
const twainService = TestBed.inject(TwainService);
29+
getQuotesSpy = spyOn(twainService, "getQuotes").and.returnValue(
30+
asyncData(testQuotes)
31+
);
1432

1533
fixture = TestBed.createComponent(DashboardComponent);
1634
component = fixture.componentInstance;
17-
fixture.detectChanges();
1835
});
1936

20-
it('should create', () => {
21-
expect(component).toBeTruthy();
37+
it("should display a collection of DashboardQuoteComponents", async () => {
38+
fixture.detectChanges(); // ngOnInit
39+
// expect no elements
40+
await fixture.whenStable();
41+
fixture.detectChanges(); // update view with quote
42+
const quoteElements = fixture.nativeElement.querySelectorAll(".quote");
43+
expect(quoteElements.length).toBe(2);
44+
});
45+
46+
it("should display a title with that contains Top 2", async () => {
47+
fixture.detectChanges(); // ngOnInit
48+
// expect no elements
49+
await fixture.whenStable();
50+
fixture.detectChanges(); // update view with quote
51+
expect(component.title).toContain("Top 2 quotes");
2252
});
23-
});
53+
});

0 commit comments

Comments
 (0)