import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { SearchComponent } from './search.component' ; import { SearchService } from '../shared' ; import { ActivatedRoute } from '@angular/router' ; import { RouterTestingModule } from '@angular/router/testing' ; import { FormsModule } from '@angular/forms' ; import { MockActivatedRoute } from '../shared/search/mocks/routes' ; import { of } from 'rxjs' ; import { HttpClientTestingModule } from '@angular/common/http/testing' ; describe ( 'SearchComponent' , ( ) => { let component ; let fixture ; let mockSearchService ; let mockActivatedRoute ; beforeEach ( ( ( ) => { mockActivatedRoute = new MockActivatedRoute ( { term : 'nikola' } ) ; TestBed . configureTestingModule ( { declarations : [ SearchComponent ] , providers : [ { provide : ActivatedRoute , useValue : mockActivatedRoute } ] , imports : [ FormsModule , RouterTestingModule , HttpClientTestingModule ] } ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { mockSearchService = TestBed . inject ( SearchService ) ; mockSearchService . search = jasmine . createSpy ( ) . and . returnValue ( of ( [ ] ) ) ; fixture = TestBed . createComponent ( SearchComponent ) ; component = fixture . componentInstance ; fixture . detectChanges ( ) ; } ) ; it ( 'should create' , ( ) => { expect ( component ) . toBeTruthy ( ) ; } ) ; it ( 'should search when a term is set and search() is called' , ( ) => { component = fixture . debugElement . componentInstance ; component . query = 'J' ; component . search ( ) ; expect ( mockSearchService . search ) . toHaveBeenCalledWith ( 'J' ) ; } ) ; it ( 'should search automatically when a term is on the URL' , ( ) => { fixture . detectChanges ( ) ; expect ( mockSearchService . search ) . toHaveBeenCalledWith ( 'nikola' ) ; } ) ; } ) ;