Last active
January 15, 2018 09:47
-
-
Save zladuric/3901246b0b6d2bc8a422fb443126ee7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, Injectable, Directive, Input, OnInit } from '@angular/core'; | |
import { FormsModule } from '@angular/forms'; | |
import { ActivatedRoute, Router } from '@angular/router'; | |
import { RouterTestingModule } from '@angular/router/testing'; | |
import { | |
async, | |
TestBed, | |
ComponentFixture | |
} from '@angular/core/testing'; | |
import { SharedModule } from './shared/shared.module'; | |
import { MaterialModule } from '@angular/material'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/of'; | |
@Injectable() | |
class MockService { | |
getThing(id: any) { | |
console.log('called with id:', id); | |
return Observable.of({ hi: 'there' }); | |
} | |
} | |
@Component({ | |
selector: 'test-component', | |
template: '<div> this is a test </div>' | |
}) | |
class TestComponent implements OnInit { | |
constructor(private activatedRoute: ActivatedRoute, private service: MockService) {} | |
ngOnInit() { | |
this.activatedRoute.params.subscribe((params: any) => { | |
console.log('Hey there, param', params); | |
this.service.getThing(params.id); | |
}); | |
} | |
} | |
let mockAR: any = { | |
params: { | |
subscribe: function() { | |
console.log('Subscribed.'); | |
Observable.of({ id: 123 }); | |
} | |
} | |
} | |
export function main() { | |
xdescribe('My Test Component', () => { | |
let fixture: ComponentFixture<TestComponent>; | |
let instance: TestComponent; | |
let domEl: Element; | |
let routeSpy; | |
const routes = [ | |
{ path: 'test/:id', component: TestComponent } | |
]; | |
beforeEach(async(() => { | |
routeSpy = spyOn(mockAR.params, 'subscribe').and.callThrough(); | |
TestBed.configureTestingModule({ | |
imports: [FormsModule, SharedModule, MaterialModule, RouterTestingModule.withRoutes(routes)], | |
declarations: [TestComponent], | |
providers: [ | |
MockService, | |
{ | |
provide: ActivatedRoute, useValue: mockAR, | |
} | |
] | |
}); | |
TestBed | |
.compileComponents() | |
.then(() => { | |
fixture = TestBed.createComponent(TestComponent); | |
instance = fixture.debugElement.componentInstance; | |
domEl = fixture.debugElement.nativeElement; | |
}); | |
})); | |
it('should work', | |
async(() => { | |
let mockService = | |
fixture.debugElement.injector.get<any>(MockService) as MockService; | |
// let serviceSpy = spyOn(mockService, 'getThing').and.callThrough(); | |
fixture.detectChanges(); | |
expect(routeSpy.calls.count()).toBe(1); | |
// expect(serviceSpy.calls.count()).toBe(1); | |
})); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you help me?