Angular 2 Unit testing: Can't resolve all parameters for Router - unit-testing

I use unit testing with karma with angular 2. I added Router in my service and but when I write test case for that it through me following error.
Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).
here is code
spec.ts
import 'rxjs/add/operator/map';
import { Http, BaseRequestOptions, Response, ResponseOptions,ConnectionBackend } from '#angular/http';
import { MockBackend, MockConnection } from '#angular/http/testing';
import { GlobalUtils } from './global.utils';
import { Router} from '#angular/router';
import { TestBed, inject } from '#angular/core/testing';
describe('Global Utils : Meta urls API', () => {
let emptyMetaUrl = {};
let metaUrl = {
LOGIN: '/operator/login',
META_API: '/meta-api'
};
beforeEach(()=>TestBed.configureTestingModule({
providers: [
Router,
GlobalUtils,
BaseRequestOptions,
MockBackend,
{
provide: Http,
useFactory: function (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
]
}));
let subject:GlobalUtils = null;
let backend:MockBackend = null;
let http:Http = null;
beforeEach(inject([GlobalUtils, MockBackend, Http], (_globalUtils:GlobalUtils, mockBackend:MockBackend,_http:Http) => {
subject = _globalUtils;
backend = mockBackend;
http = _http;
}));
it('Should have get Meta urls', (done) => {
backend.connections.subscribe((connection:MockConnection)=> {
let options = new ResponseOptions({
body: JSON.stringify(metaUrl)
});
connection.mockRespond(new Response(options));
});
http
.get(subject.metaUrl)
.subscribe((response) => {
subject.getMetaUrls(); // in this routing is use
expect(GlobalUtils.saveUrl).toEqual(metaUrl);
done();
});
});
});
I used Routing in this service to navigate path.
service.ts
import {Injectable} from '#angular/core';
import { Router} from '#angular/router';
import {Http, Headers,Response} from '#angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
#Injectable()
export class GlobalUtils {
static saveUrl = { };
head:Headers = new Headers();
hostUrl:string = 'http://192.168.0.103:8000/';
metaUrl:string = 'urls-metadata/';
// constructor declare
constructor(public _http:Http,public _router:Router) {
this.head.append('Content-Type', 'application/json');
}
/*
* this function is used for Http GET Request
* */
urlGet(url:string) {
let headers = this.head;
return this._http.get(this.hostUrl + url, {headers: headers})
.map(res => res.json())
.map((res) => {
return res;
});
}
/*
* this function is used to GET all API url
* */
getMetaUrls():any{
let url = this.metaUrl;
this.urlGet(url).subscribe(
(result) => {
console.log('result = '+JSON.stringify(result));
if (result) {
GlobalUtils.saveUrl = result;
console.log('get Meta urls response = '+ result.status)
}
},
(error)=> {
this._router.navigate(['page-error']);
console.log('get Meta urls error = '+ error.status + " data =" +JSON.stringify(error))
},
()=>{console.log('get Meta url is successfully')}
);
}
}
when I run karma start this test case failed with this error.
✖ Should have get Meta urls
Chrome 52.0.2743 (Linux 0.0.0)
Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).
at CompileMetadataResolver.getDependenciesMetadata (webpack:///~/#angular/compiler/bundles/compiler.umd.js:14404:0 <- config/spec-bundle.js:53297:22)
at CompileMetadataResolver.getTypeMetadata (webpack:///~/#angular/compiler/bundles/compiler.umd.js:14301:0 <- config/spec-bundle.js:53194:29)
at webpack:///~/#angular/compiler/bundles/compiler.umd.js:14448:0 <- config/spec-bundle.js:53341:44
at Array.forEach (native)
at CompileMetadataResolver.getProvidersMetadata (webpack:///~/#angular/compiler/bundles/compiler.umd.js:14428:0 <- config/spec-bundle.js:53321:22)
at CompileMetadataResolver.getNgModuleMetadata (webpack:///~/#angular/compiler/bundles/compiler.umd.js:14181:0 <- config/spec-bundle.js:53074:61)
at RuntimeCompiler._compileComponents (webpack:///~/#angular/compiler/bundles/compiler.umd.js:16803:0 <- config/spec-bundle.js:55696:50)
at RuntimeCompiler._compileModuleAndAllComponents (webpack:///~/#angular/compiler/bundles/compiler.umd.js:16747:0 <- config/spec-bundle.js:55640:40)
at RuntimeCompiler.compileModuleAndAllComponentsSync (webpack:///~/#angular/compiler/bundles/compiler.umd.js:16735:0 <- config/spec-bundle.js:55628:24)
at TestingCompilerImpl.compileModuleAndAllComponentsSync (webpack:///~/#angular/compiler/bundles/compiler-testing.umd.js:758:0 <- config/spec-bundle.js:38833:36)

If you don't care to do any actual routing in the test, you can just create a mock for it
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
TestBed.configureTestingModule({
providers: [
{ provide: Router, useValue: mockRouter }
]
})
Then in your test maybe you just want to make sure that the navigate method is called. You can do
expect(mockRouter.navigate).toHaveBeenCalledWith(['/router']);
If you do want to test real navigation, then you should use the RouterTestingModule to add all the router providers and directives.
import { RouterTestingModule } from '#angular/router/testing';
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([{path: '', component: BlankCmp}, {path: 'simple', component: SimpleCmp}])
]
})

Related

Jasmine/karma test case are not running in an orderly manner using Angular5

When running the test case using jasmine/karma test cases. I am facing n issue which is before starting with the Login spec test case the other spec files are all called before completing the Login. It need to happen in an orderly manner which is like
1.Login
2.Dashboard
3.Order
etc.
Is there a way to do this.
login.spec.ts file
import { async, ComponentFixture, TestBed } from '#angular/core/testing';
import { RouterModule, Router } from '#angular/router';
import { LoginComponent } from './login.component';
import { DebugElement } from '#angular/core';
import { FormsModule, ReactiveFormsModule } from '#angular/forms';
import { BrowserModule, By } from '#angular/platform-browser';
import { Ng2Bs3ModalModule } from 'ng2-bs3-modal/ng2-bs3-modal';
import { RouterTestingModule } from '#angular/router/testing';
import { APP_BASE_HREF } from '#angular/common';
import { LoginService } from './login.service';
import { HttpClientModule } from '#angular/common/http';
import { ApiService } from '../config/api.service';
import { ConfigService } from '../config/config.service';
import { Constants } from '../config/Constant';
import { SharedService } from '../shared/shared.service';
import { Http, BaseRequestOptions, ResponseOptions, Response, RequestMethod } from '#angular/http';
import { inject } from '#angular/core/testing';
import { MockBackend, MockConnection } from '#angular/http/testing';
describe('LoginComponent', () => {
let comp: LoginComponent;
let fixture: ComponentFixture<LoginComponent>;
let de: DebugElement;
let el: HTMLElement;
let userNameEl: DebugElement;
let passwordEl: DebugElement;
let submitEl: DebugElement;
let loginService: LoginService = null;
let backend: MockBackend = null;
TestBed.overrideComponent(LoginComponent, {
set: {
providers: [
{
provide: LoginService,
useValue: loginService
},
{
provide: Router,
useClass: class { navigate = jasmine.createSpy('navigate'); }
}
]
}
});
beforeEach(async(() => {
// loginService = loginService;
// backend = mockBackend;
TestBed.configureTestingModule({
declarations: [
LoginComponent
],
imports: [
RouterModule.forRoot([{
path: '',
component: LoginComponent
}]),
BrowserModule,
FormsModule,
ReactiveFormsModule,
Ng2Bs3ModalModule,
RouterTestingModule,
HttpClientModule
],
providers: [
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backendInstance, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
LoginService,
ApiService,
ConfigService,
Constants,
SharedService,
{ provide: APP_BASE_HREF, useValue: '/' }
]
}).compileComponents().then(() => {
fixture = TestBed.createComponent(LoginComponent);
comp = fixture.componentInstance;
de = fixture.debugElement.query(By.css('form'));
el = de.nativeElement;
userNameEl = fixture.debugElement.query(By.css('input[id=InputEmail1]'));
passwordEl = fixture.debugElement.query(By.css('input[id=InputPassword1]'));
submitEl = fixture.debugElement.query(By.css('.login-btn'));
});
}));
beforeEach(inject([LoginService, MockBackend], (Service: LoginService, mockBackend: MockBackend) => {
loginService = Service;
backend = mockBackend;
}));
it('should create', () => {
expect(comp).toBeTruthy();
});
it('To check the initial value', () => {
expect(comp.submitted).toBe(false);
expect(comp.spinnerlogo).toBeFalsy();
expect(comp.data).toEqual({});
});
it(`entering value in username and password input controls`, () => {
userNameEl.nativeElement.value = 'admin';
passwordEl.nativeElement.value = 'admin';
fixture.detectChanges();
});
it('after entering value the button should enabled and click Action should happen', () => {
expect(submitEl.nativeElement.disabled).toBeFalsy();
const loginButtonSpy = spyOn(comp, 'onSubmit');
submitEl.triggerEventHandler('click', null);
expect(loginButtonSpy).toHaveBeenCalled();
});
it('calling onSubmit method after clicked the login button', () => {
comp.submitted = true;
comp.spinnerlogo = true;
comp.errorDiagnostic = null;
comp.mailerrorDiagnostic = null;
expect(comp.submitted).toBeTruthy();
expect(comp.spinnerlogo).toBeTruthy();
expect(comp.errorDiagnostic).toBeNull();
expect(comp.mailerrorDiagnostic).toBeNull();
});
it('#login should call endpoint and return it\'s result', (done) => {
backend.connections.subscribe((connection: MockConnection) => {
const options = new ResponseOptions({
body: JSON.stringify({ success: true })
});
connection.mockRespond(new Response(options));
// Check the request method
expect(connection.request.method).toEqual(RequestMethod.Post);
// Check the url
expect(connection.request.url).toEqual('/auth/login');
// Check the body
// expect(connection.request.text())
expect(connection.request.text()).toEqual(JSON.stringify({ username: 'admin', password: 'admin' }));
// Check the request headers
expect(connection.request.headers.get('Content-Type')).toEqual('application/json');
});
loginService.login('admin', 'admin')
.subscribe((response) => {
console.log('response values are ---####------------ ', response);
// Check the response
expect(response.user.username).toEqual('admin');
expect(response.user.password).toEqual('admin');
// set value in sessionStorage
sessionStorage.setItem('currentUser', JSON.stringify(response));
sessionStorage.setItem('token', JSON.stringify(response.token));
sessionStorage.setItem('dismissOrders', 'false');
done();
},
(error) => {
expect(error).toThrowError();
});
});
});
the main problem is before executing the above file . The other spec file are executed
Thanks,
Kishan

jasmine test cases dependency in angular 2 with stub

Here I'm new to angular 2 test cases with jasmine + karma and I'm following this testing guide I tried to write test case but unable to write correctly and getting routing error as test cases are going to server while running, although it should not.
There is a question similar to this but didn't help me to solve the problem. Please guide me.
Here is my component
import { Component, OnInit, ViewChild } from '#angular/core';
import { Router, ActivatedRoute } from '#angular/router';
import { Checklist } from './checklist';
import { Job } from '../+job/job';
import { OriginalService } from './original.service';
import { UserService } from '../core/user/user.service';
import { ModalDirective } from 'ng2-bootstrap/ng2-bootstrap';
#Component({
selector: 'app-selector',
templateUrl: './original.component.html',
providers: [OriginalService]
})
export class OriginalComponent implements OnInit {
items = []
job: Job;
checklist: Checklist;
user: any;
shopId: any;
jobId: any;
constructor ( private orgService: OriginalService, private route: ActivatedRoute, private router: Router, userService: UserService) {
this.user = userService.user();
}
ngOnInit() {
this.route.params
.map(params => params['job_id'])
.subscribe(
job_id => this.jobId = job_id
);
this.getChecklist();
this.getJob();
}
getChecklist() {
this.orgService.getChecklists({
jobId: this.jobId
})
.then(checklist=> {
this.gotJobdata = true;
this.checklist = checklist.response})
.catch(error => this.error = error);
}
getJob(){
this.orgService.getJob({
jobId: this.jobId
})
.then(job => this.job = job.response)
.catch(error => this.error = error);
}
}
Here's my service
import { Injectable, ViewChildren } from '#angular/core';
import { Http, Response, Headers, RequestOptions } from '#angular/http';
import { environment } from '../../environments/environment'
import 'rxjs/add/operator/toPromise';
import { Checklist } from './checklist';
import { Job } from '../+job/job';
import { UserService } from '../core/user/user.service';
#Injectable()
export class OriginalService {
shopId: any;
constructor(private http: Http, private userService: UserService ) {
this.shopId = userService.shopId();
}
getChecklists(svc): Promise<Checklist> {
return this.http.get(environment.apiUrl + this.shopId + '/jobs/' + svc.jobId + '/checklists_path/' + 'check_item.json')
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
getJob(svc): Promise<Job> {
return this.http.get(return environment.apiUrl + this.shopId + '/jobs/' + svc.jobId + '.json')
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}
}
Here's the spec what I've tried:
import { async, ComponentFixture, TestBed } from '#angular/core/testing';
import { SharedModule } from '../shared/shared.module';
import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';
import { HttpModule } from '#angular/http';
import { Router, ActivatedRoute, Params } from '#angular/router';
import { ComponentLoaderFactory } from 'ng2-bootstrap/component-loader';
import { Subject } from 'rxjs/Subject';
import { UserService } from '../core/user/user.service';
import { OriginalService } from './original.service';
import { OriginalComponent } from './original.component';
describe('OriginalComponent', () => {
let component: OriginalComponent;
let fixture: ComponentFixture<OriginalComponent>;
let params: Subject<Params>;
let userService, orgService;
let userServiceStub = {
isLoggedIn: true,
user: { name: 'Test User'}
};
beforeEach(async(() => {
params = new Subject<Params>();
TestBed.configureTestingModule({
imports: [ ModalModule.forRoot(), SharedModule, HttpModule ],
declarations: [ OriginalComponent ],
providers: [ OriginalService, UserService, ComponentLoaderFactory,
{ provide: Router, useValue: userServiceStub }, {provide: ActivatedRoute, useValue: { params: params }} ]
})
.compileComponents();
})
);
beforeEach(() => {
fixture = TestBed.createComponent(ChecklistComponent);
component = fixture.componentInstance;
fixture.detectChanges();
it('should create', () => {
expect(component).toBeTruthy();
});
});
Please correct me how to properly use routing in test cases or stubs where I'm doing wrong.

unit testing a component which is injected by a service which is using angular2-jwt AuthHttp call instead of http call

I have been using HTTP to call API to do my stuff, later introduced angular2-jwt to intercept the http calls and pass the jwt tockens. Unit testing was working fine until AuthHttp was used. I have created 'mockuserdata' model to return the mock values
Every unit test cases where failing until I added the providers for http and AuthHttp as follows
{provide: Http, useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) =>
{ return new Http(backendInstance, defaultOptions);},
deps: [MockBackend, BaseRequestOptions] },
{ provide: AuthHttp, useExisting: Http, deps: [Http] }
After introducing above providers 'should instantiate UsersComponent' got passed but 'It checks loadUserList() is binding datasource' ís failing
/* User Operations Service*/
import {Headers,RequestOptionsArgs,Response,URLSearchParams, RequestMethod,RequestOptions }from '#angular/http';
import { Injectable, Optional }from '#angular/core';
import { Observable}from 'rxjs/Observable';
import * as models from '../models';
import { AuthHttp }from 'angular2-jwt';
import 'rxjs/Rx';
# Injectable()
export class UserOperationsApi {
protected basePath = '/api';
public defaultHeaders: Headers = new Headers();
constructor(protected http: AuthHttp, # Optional()basePath: string) {
if (basePath) {
this.basePath = basePath;
}
}
/**
* Returns all users
* Returns list of all users
*/
public getAllUsers(extraHttpRequestParams ? : any): Observable < Array < models.User >> {
const path = this.basePath + '/users';
let queryParameters = new URLSearchParams();
let headerParams = this.defaultHeaders;
let requestOptions: RequestOptionsArgs = {
method: 'GET',
headers: headerParams,
search: queryParameters
};
return this.http.request(path, requestOptions)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
return response.json();
}
});
}
}
/* User List Component*/
import {Component,OnInit}from '#angular/core';
import {Router } from '#angular/router';
import {UserOperationsApi }from '../services';
import {DialogService }from '../services';
import { Subscription }from 'rxjs';
# Component({
selector: 'my-app',
templateUrl: './list-users.component.html',
providers: [UserOperationsApi]
})
export class UsersComponent implements OnInit {
public userData: any;
constructor(private api: UserOperationsApi, private router: Router) {
this.loadUserList();
}
// loading user list
loadUserList() {
// To get data from api
this.busy = this.api.getAllUsers()
.subscribe(
data => this.handleConfiguredUsers(data),
error => {
throw ({
message: 'Error occured! while retreiving data from server.'
});
});
}
// users list to be loaded
handleConfiguredUsers(data) {
this.userData = data;
}
}
** * User component unit testing ** ** **
import {async,TestBed,inject}from '#angular/core/testing';
import {Response,ResponseOptions, XHRBackend } from '#angular/http';
import { MockBackend, MockConnection}from '#angular/http/testing';
import { HttpModule}from '#angular/http';
import { Router, ActivatedRoute}from '#angular/router';
import { MockRouter}from '../mock/router.mock';
import { usersData}from '../data/mockData/users';
import { UsersComponent}from './list-users.component';
describe('list-users.component.ts', () => {
beforeEach(() => {
TestBed.configureTestingModule(
{
declarations: [UsersComponent],
imports: [HttpModule, DevExtremeModule],
providers: [MockBackend, BaseRequestOptions,
{ provide: XHRBackend, useClass: MockBackend },
{ provide: Router, useClass: MockRouter },
{ provide: ActivatedRoute, useClass: MockRouter },
{
provide: Http, useFactory: (backendInstance: MockBackend, defaultOptions: BaseRequestOptions) => {
return new Http(backendInstance, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
{ provide: AuthHttp, useExisting: Http, deps: [Http] }
]
});
});
it('should instantiate UsersComponent', () => {
const fixture = TestBed.createComponent(UsersComponent);
let compInstance: UsersComponent = fixture.componentInstance;
expect(compInstance instanceof UsersComponent).toBe(true, 'should create UsersComponent');
});
it('It checks loadUserList() is binding datasource',
async(inject([XHRBackend],
(mockBackend: MockBackend) => {
mockBackend.connections.subscribe(
(connection: MockConnection) => {
connection.mockRespond(new Response(
new ResponseOptions({
body: usersData
})));
});
const fixture = TestBed.createComponent(UsersComponent);
let instance: UsersComponent = fixture.componentInstance;
instance.instance.loadUserList();;
expect(instance.userData.length).toEqual(usersData.length);
expect(instance.userData).toBe(usersData);
})));
}
1) Whats wrong with the unit test which is returning null values.
The following worked for me. instruct to use Http instead of AuthHttp using
{ provide: AuthHttp, useExisting: Http }
so my unit testing file has the following change.
describe('list-users.component.ts', () => {
beforeEach(() => {
TestBed.configureTestingModule(
{
declarations: [UsersComponent],
imports: [HttpModule, DevExtremeModule],
providers: [MockBackend, BaseRequestOptions,
{ provide: XHRBackend, useClass: MockBackend },
{ provide: Router, useClass: MockRouter },
{ provide: ActivatedRoute, useClass: MockRouter },
{ provide: AuthHttp, useExisting: Http }
]
});
});
Hope it will be useful for someone someday, Cheers :)

Angular 2 Observable Service Karma Jasmine Unit Test not working

I am a newbie to Angular 2 and Karma + Jasmine unit tests. I cannot figure out what semantic error I have made in order to make this unit test use the mocked response. In the console, when "expect(items[0].itemId).toBe(2);" is run, it says items[0].itemId is undefined.
Would someone be able to help me out or point me in the right direction? Please let me know if you need any additional information. Thanks!
item.ts
export class Item {
itemId: number;
itemName: string;
itemDescription: string;
}
item.service.ts
import { Injectable, Inject } from '#angular/core';
import { Headers, Http } from '#angular/http';
import { Observable } from 'rxjs/Rx';
import { Item } from './item';
#Injectable()
export class ItemService {
private headers = new Headers({'Content-Type': 'application/json'});
constructor(
private http: Http)
{
}
getItems(listOptions: Object): Observable<Item[]> {
return this.http.post('/listItems', listOptions, {headers:this.headers})
.map(response => response.json() as Item[])
}
}
item.service.spec.ts
import { TestBed, fakeAsync, inject, tick } from '#angular/core/testing';
import { MockBackend } from '#angular/http/testing';
import { Http, BaseRequestOptions, Response, ResponseOptions } from '#angular/http';
import { Observable } from 'rxjs/Rx';
import { ItemService } from './item.service';
import { Item } from './item';
describe('ItemService', () => {
let mockResponse, matchingItem, connection;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
ItemService,
MockBackend,
BaseRequestOptions,
{
provide: Http,
useFactory: (backend, defaultOptions) => new Http(backend, defaultOptions),
deps: [MockBackend, BaseRequestOptions]
},
// { provide: XHRBackend, useClass: MockBackend }
]
});
const items = [
{
"itemId":2,
"itemName":"test item1",
"itemDescription":"hello hello"
},
{
"itemId":1,
"itemName":"name2124111121",
"itemDescription":"description212412112"
}
];
mockResponse = new Response(new ResponseOptions({body: {data: items}, status: 200}));
});
describe('getItems', () => {
//Subscribing to the connection and storing it for later
it('should return all the items',inject([ItemService, MockBackend], (service: ItemService, backend: MockBackend) => {
backend.connections.subscribe(connection => {
connection.mockRespond(mockResponse);
});
service.getItems({isActive: true, sortColumn: "lastModifiedDateUtc", sortOrder: "desc"})
.subscribe((items: Item[]) => {
expect(items.length).toBe(2);
});
}));
});
});
Plunkr: https://plnkr.co/edit/m7In2eVh6oXu8VNYFf9l?p=preview
(There are some errors with the Plunkr I need help with as well but the main files are there)
The mockResponse body did not match the actual response body, that is why I was getting the error.
mockResponse = new Response(new ResponseOptions({body: {data: items}, status: 200})); should be mockResponse = new Response(new ResponseOptions({body: items, status: 200}));

Angular 2 unit test: declare HTTP mocking globally

I write test cases in angular 2.I want to know how following thing done. please help me.
How to define Mocking (Http, router) globally.
How to call common beforeEach() in every test cases.
My code which i want to common.
common.ts
/* this used for route Mocking
* */
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
/*
* Response Url
* */
let metaUrl = {
LOGIN: '/operator/login',
META_API: '/meta-api'
};
let subject:GlobalUtils = null;
let backend:MockBackend = null;
let http:Http = null;
/*
* The beforeEach function is called once before each spec in the describe in which it is called
* */
beforeEach(()=>TestBed.configureTestingModule({
providers: [
GlobalUtils,
BaseRequestOptions,
MockBackend,
{
provide: Router, useValue: mockRouter
},
{
provide: Http,
useFactory: function (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
]
}));
beforeEach(inject([GlobalUtils, MockBackend, Http], (_globalUtils:GlobalUtils, mockBackend:MockBackend, _http:Http) => {
subject = _globalUtils;
backend = mockBackend;
http = _http;
}));
Reason behind this I exactly this in another spec.ts file for write test case.
here is my spec.ts files
1.spec.ts
import {Http, BaseRequestOptions, Response, ResponseOptions, ConnectionBackend} from '#angular/http';
import {MockBackend, MockConnection} from '#angular/http/testing';
import {Router} from '#angular/router';
import {TestBed, inject} from '#angular/core/testing';
import {GlobalUtils} from '../global.utils.ts';
import { Constant } from '../constant'
describe('Global Utils : Meta urls API', () => {
/* this used for route Mocking
* */
let mockRouter = {
navigate: jasmine.createSpy('navigate')
}
/*
* Response Url
* */
let metaUrl = {
LOGIN: '/operator/login',
META_API: '/meta-api'
};
let subject:GlobalUtils = null;
let backend:MockBackend = null;
let http:Http = null;
/*
* The beforeEach function is called once before each spec in the describe in which it is called
* */
beforeEach(()=>TestBed.configureTestingModule({
providers: [
GlobalUtils,
BaseRequestOptions,
MockBackend,
{
provide: Router, useValue: mockRouter
},
{
provide: Http,
useFactory: function (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) {
return new Http(backend, defaultOptions);
},
deps: [MockBackend, BaseRequestOptions]
},
]
}));
beforeEach(inject([GlobalUtils, MockBackend, Http], (_globalUtils:GlobalUtils, mockBackend:MockBackend, _http:Http) => {
subject = _globalUtils;
backend = mockBackend;
http = _http;
}));
/*
* This Test case is check Response of getMetaUrls function
* getMetaUrls must return response
* */
it('Should have get Meta urls', (done) => {
// HTTP Mocking
backend.connections.subscribe((connection:MockConnection)=> {
let options = new ResponseOptions({
body: JSON.stringify(metaUrl),status: 200
});
connection.mockRespond(new Response(options));
});
/*
* we mock http get method
* */
http
.get(Constant.metaUrl)
.subscribe((response) => {
subject.getMetaUrls();
expect(Constant.saveUrl).toEqual(metaUrl);
done();
});
});
/*
* This Test case is check empty response and redirect to error page
* getMetaUrls must return empty response
* */
it('Should have get Empty Meta urls data', (done) => {
backend.connections.subscribe((connection:MockConnection)=> {
let options = new ResponseOptions({
body: JSON.stringify(''), status: 200
});
connection.mockRespond(new Response(options));
});
http
.get(Constant.metaUrl)
.subscribe((response) => {
subject.getMetaUrls();
expect(mockRouter.navigate).toHaveBeenCalledWith(['page-error']);
done();
});
});
2.spec.ts
describe('Authentication test',()=>{
});
2.spec file blank now because I want to use common.ts function in this file and I want to know how.