unit testing - angular 2 - jasmine- test methods in typescript -
i have written simple mock services test method(changed()) in typescript file (edituser.ts)
i getting following error when try execute edituser.spec.ts
typeerror: cannot read property 'changed' of undefined @ object.eval (http://localhost:3000/app/tracker/edituseradmin/edituser.spec.js:94:12) @ attemptsync (http://localhost:3000/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1886:24) @ queuerunner.run (http://localhost:3000/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1874:9) @ http://localhost:3000/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1898:16 @ http://localhost:3000/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:1842:9 @ http://localhost:3000/node_modules/angular2/bundles/testing.dev.js:2116:15 @ zonedelegate.invoke (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:390:29) @ zone.run (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:283:44) @ http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:635:58 @ zonedelegate.invoketask (http://localhost:3000/node_modules/angular2/bundles/angular2-polyfills.js:423:38)
edituser.spec.ts
import {edituseradmin} "./edituser.component"; import { it, inject, injectasync, describe, beforeeachproviders, beforeeach, testcomponentbuilder, setbasetestproviders, componentfixture, expect } 'angular2/testing'; import {provide} 'angular2/core'; import { component, oninit, router_directives, routeparams, router, http, http_providers, headers, crudservice, apiservice } '../index'; import { test_browser_platform_providers, test_browser_application_providers } 'angular2/platform/testing/browser'; class mockapiservice { getapi() { return { teamdrplist: "trackeruser/getusercreateteammember", userdrplist: "trackergroup/getalllist" } } } class mockcrud { } class mockrouter { } setbasetestproviders(test_browser_platform_providers, test_browser_application_providers); describe('edituseradmin', () => { var app : edituseradmin; beforeeach(injectasync([testcomponentbuilder], (tcb: testcomponentbuilder) => { return tcb .overrideproviders(edituseradmin, [ provide(crudservice, { useclass: crudservice }), provide(apiservice, { useclass: mockapiservice }), provide(routeparams, { usevalue: new routeparams({ userid: '1' }) }), provide(router, { useclass: mockrouter }) ]) .createasync(edituseradmin) .then((componentfixture: any) => { this.component = componentfixture; }); })); it('change event method', () => { app.changed("india"); expect(app.selectcontrol).toequal("india"); }); });
edituser.ts
import { component, oninit, router_directives, routeparams, router, http, http_providers, headers, crudservice, apiservice } '../index'; import "rxjs/rx"; @component({ templateurl: 'app/tracker/edituseradmin/edituser.component.html', directives: [router_directives], providers: [http_providers, crudservice, apiservice] }) export class edituseradmin implements oninit { selectedcity: string; items: any[]; //select dropdown array teams: any[]; groupvalue: any[] = []; teamvalue: any[] = []; optionavailable: boolean = false; profiledetails: any; assignedgroup: any[]; assignedteam: any[]; get_apiservice: any; getuserurl: string; groupurl: string; getuserid: string; selectcontrol: string; //group assignment change event selectteam: string; insertgroupid: any; querystring: string; isbrokerhidden: boolean; errormsg: string; successmsg: string; constructor(private crudservice: crudservice, private apiservice: apiservice, params: routeparams, private router: router) { //to invoke http service this.get_apiservice = this.apiservice.getapi(); this.getuserid = params.get("userid"); this.profiledetails = { lastname: "", firstname: "", loginname: "", email: "", isbrokeruser: false } this.querystring = params.get("userid"); this.isbrokerhidden = true; } ngoninit() { this.groupurl = this.get_apiservice.gettrackergroup + "?newtrackeruserid=" + this.getuserid; this.crudservice.getserverdata(this.get_apiservice.teamdrplist + "?newtrackeruserid=" + this.getuserid, "") .subscribe( data => { this.teams = data.teammembersavailable; // this.items = data.groupavailable; this.assignedteam = data.teammembersassigned.map(function (a) { return { groupname: a.displayname, assignedid: a.userid }; }); this.teamvalue.push.apply(this.teamvalue, this.assignedteam); }, error => this.crudservice.logerror(error) ); this.crudservice.getserverdata(this.groupurl, "") .subscribe( data => { this.items = data.groupavailable; this.assignedgroup = data.groupassigned.map(function (a) { return { groupname: a.name, assignedid: a.groupid }; }); this.groupvalue.push.apply(this.groupvalue, this.assignedgroup); }, error => this.crudservice.logerror(error) ); if (this.querystring != "0") { this.getuserurl = this.get_apiservice.getuser + this.getuserid; this.crudservice.getserverdata(this.getuserurl, "") .subscribe( data => { this.profiledetails = data; }, error => this.crudservice.logerror(error) ); settimeout(() => { this.crudservice.getserverdata(this.get_apiservice.brokoerurl, "") .subscribe( data => { if (this.groupvalue.length > 0) { var groupary = this.groupvalue.map(function (a) { return a.assignedid; }); // this.profiledetails = data; this.isbrokerhidden = !(groupary.indexof(data.groupid) > -1); } }, error => this.crudservice.logerror(error) ); }, 100); } } //select dropdown change event-ie 11 support changed(selectedvalue: string) { this.selectcontrol = selectedvalue; } changeteam(value: string) { this.selectteam = value; } //event add group addgroup(value) { var a, b; var selectedname: string; var selectedid: number; let ind = this.items.map(function (obj, index) { if (obj.groupid == value) { = index; selectedname = obj.name; selectedid = obj.groupid; return a; } }); if (a == "undefiened" || == null) { alert("select group assignment"); return false; } else { this.groupvalue.push({ groupname: selectedname, assignedid: selectedid }); } b = this.items.splice(a, 1); this.items.sort(function (x, y) { return (x.groupid > y.groupid) ? 1 : ((y.groupid > x.groupid) ? -1 : 0); }); } addteam(value) { var a, b; var selectedname: string; var selectedid: number; let ind = this.teams.map(function (obj, index) { if (obj.userid == value) { = index; selectedname = obj.displayname; selectedid = obj.userid; return a; } }); if (value == "undefiened" || value == null) { alert("select team member"); return false; } else { var headers = new headers(); headers.append('content-type', 'application/json'); this.crudservice.postserverdata(this.get_apiservice.addteammember + "?trackeruserid=" + this.getuserid + "&newmemberid=" + value, "", headers) .subscribe( data => { if (data == true) { b = this.teams.splice(a, 1); this.teamvalue.push({ groupname: selectedname, assignedid: selectedid }); } else { } }, error => this.crudservice.logerror(error) ); this.teams.sort(function (x, y) { return (x.userid > y.userid) ? 1 : ((y.userid > x.userid) ? -1 : 0); }); } } //event unassign group unassigngroup(a: any) { var selectobject = <htmlselectelement>document.getelementbyid("group-select"); (var = 0; < selectobject.length; i++) { if ((<htmloptionelement>selectobject.options[i]).value == a.groupname) { this.optionavailable = true; } } if (this.optionavailable == false) { this.items.push({ name: a.groupname, groupid: a.assignedid }); var index = this.groupvalue.indexof(a); if (index > -1) { this.groupvalue.splice(index, 1); this.items.sort(function (a, b) { return (a.groupid > b.groupid) ? 1 : ((b.groupid > a.groupid) ? -1 : 0); }); } } } unassignteam(a: any) { var selectobject = <htmlselectelement>document.getelementbyid("team-select"); (var = 0; < selectobject.length; i++) { if ((<htmloptionelement>selectobject.options[i]).value == a.groupname) { this.optionavailable = true; } } if (this.optionavailable == false) { var headers = new headers(); headers.append('content-type', 'application/json'); this.crudservice.postserverdata(this.get_apiservice.removeteammember + "?trackeruserid=" + this.getuserid + "&teammemberid=" + a.assignedid, "", headers) .subscribe( data => { if (data == true) { // b = this.teams.splice(a, 1); this.teams.push({ displayname: a.groupname, userid: a.assignedid }); } else { // nothing } var index = this.teamvalue.indexof(a); if (index > -1) { this.teamvalue.splice(index, 1); this.teams.sort(function (a, b) { return (a.userid > b.userid) ? 1 : ((b.userid > a.userid) ? -1 : 0); }); } }, error => this.crudservice.logerror(error) ); } } edituser() { var headers = new headers(); if (this.querystring != "0") { this.insertgroupid = this.groupvalue.map(function (a) { return a.assignedid; }); var creds: string = json.stringify({ lastname: this.profiledetails.lastname, firstname: this.profiledetails.firstname, loginname: this.profiledetails.loginname, email: this.profiledetails.email, isbrokeruser: this.profiledetails.isbrokeruser, groupid: this.insertgroupid, userid: this.getuserid }); headers.append('content-type', 'application/json'); this.crudservice.postserverdata(this.get_apiservice.saveuser, creds, headers) .subscribe( data => { if (data == true) { ///alert("user saved"); document.getelementbyid("errorstatus").style.display = "none"; document.getelementbyid("sucessstatus").style.display = ""; //document.getelementbyid("sucessstatus").innerhtml = ""; this.successmsg = "tracker user information saved successfully."; scrollto(0, 0); } else { // nothing } }, error => { if (error.status == 500) { var errormsg = json.parse(error._body); // alert(); document.getelementbyid("sucessstatus").style.display = "none"; document.getelementbyid("errorstatus").style.display = ""; //document.getelementbyid("errorstatus").innerhtml = "tracker user information saved successfully."; this.errormsg = errormsg.exceptionmessage; scrollto(0, 0); } this.crudservice.logerror(error);; } ); } else { this.insertgroupid = this.groupvalue.map(function (a) { return a.assignedid; }); var creds: string = json.stringify({ lastname: this.profiledetails.lastname, firstname: this.profiledetails.firstname, loginname: this.profiledetails.loginname, email: this.profiledetails.email, isbrokeruser: this.profiledetails.isbrokeruser, groupid: this.insertgroupid }); headers.append('content-type', 'application/json'); this.crudservice.postserverdata(this.get_apiservice.saveuser, creds, headers) .subscribe( data => { if (data == true) { document.getelementbyid("errorstatus").style.display = "none"; document.getelementbyid("sucessstatus").style.display = ""; this.successmsg = "tracker user information saved successfully."; scrollto(0, 0); } else { // nothing } }, error => { if (error.status == 500) { var errormsg = json.parse(error._body); document.getelementbyid("sucessstatus").style.display = "none"; document.getelementbyid("errorstatus").style.display = ""; this.errormsg = errormsg.exceptionmessage; scrollto(0, 0); } this.crudservice.logerror(error); } ); } } canceledituser() { this.router.navigate(['trackeruseradmin']); } }
f.componentinstance gets methods properties test application using jasmine
beforeeach(injectasync([testcomponentbuilder], (tcb: testcomponentbuilder) => { return tcb .overrideproviders(edituseradmin, [ provide(edituserservice, { useclass: editusermock }), provide(apiserviceconstants, { useclass: apiserviceconstants }), provide(routeparams, { usevalue: new routeparams({ userid: "48288" }) }), provide(router, { useclass: mockrouter }) ]) .createasync(edituseradmin) .then((f) => { f.detectchanges(); app = f.componentinstance; fixture = f; }); }));
Comments
Post a Comment