Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {async, ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing';
  2.  
  3. import { WeatherComponent } from './weather.component';
  4. import {WeatherService} from './weather.service';
  5. import {Weather} from '../shared/models/weather';
  6. import {Observable, of, Subject} from 'rxjs';
  7.  
  8. class WeatherServiceStub {
  9.   public weatherChanged: Subject<Weather[]> = new Subject<Weather[]>();
  10. }
  11.  
  12.  
  13. describe('WeatherComponent', () => {
  14.   let component: WeatherComponent;
  15.   let fixture: ComponentFixture<WeatherComponent>;
  16.   let weatherService: WeatherService;
  17.  
  18.   beforeEach(async(() => {
  19.     TestBed.configureTestingModule({
  20.       declarations: [ WeatherComponent ],
  21.       providers: [
  22.         {provide: WeatherService, useClass: WeatherServiceStub}
  23.       ]
  24.     })
  25.     .compileComponents();
  26.   }));
  27.  
  28.   beforeEach(() => {
  29.     fixture = TestBed.createComponent(WeatherComponent);
  30.     component = fixture.componentInstance;
  31.     weatherService = TestBed.get(WeatherService);
  32.     fixture.detectChanges();
  33.   });
  34.  
  35.   it('should create', () => {
  36.     expect(component).toBeTruthy();
  37.   });
  38.  
  39.   fit('should do something with weather array', fakeAsync(() => {
  40.     console.log(component.weather);
  41.     fixture.detectChanges();
  42.     // weatherService.weatherChanged is subject, how is it works?!?!?!
  43.     spyOn(weatherService, 'weatherChanged').and.returnValues(
  44.       of([{id: 1, name: 'heheszki'}]),
  45.       of([{id: 1, name: 'heheszki'}, {id: 1, name: 'heheszki'}])
  46.     );
  47.     fixture.detectChanges();
  48.     component.ngOnInit();
  49.     fixture.detectChanges();
  50.     console.log(component.weather);
  51.     component.ngOnInit();
  52.     console.log(component.weather);
  53.   }));
  54.  
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement