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