Unit Testing in Angular 5 with Component, Data Source and Service











up vote
0
down vote

favorite












I am like a born baby for writing Angular Test cases but trying to learn and I am using Sonar Scanner for checking code coverage.



Am sharing my code, please go have a look:



`// test.component.ts 

dataSource: TestDataSource;
..
// method I am trying to test

loadDataPage() {
if (this.code === undefined || this.code === 'Test') {
this.pageNo = this.paginator.pageIndex + 1;
this.dataSource.loadDatas(
this.paginator.pageIndex,
this.paginator.pageSize,
this.sort.active,
this.sort.direction,
this.input.nativeElement.value,
this.locationSelectedValue,
this.testticketSelectedValue,
this.teststatusSelectedValue,
this.styleOptionSelectedValue
);
} else {
this.getUserByName(this.Name);
}
}

...


Below is Datasource file:



// testDatasource.ts

export class TestDataSource implements DataSource<any> {

..
loadDatas(pageIndex: number, pageSize: number, sortLabel: string,
sortDirection: string, search: any, location: string,
testticketCount: string, teststatus: string, styleOption: string) {
this.loadingSubject.next(true);
this.testService.getData(pageIndex, pageSize, sortLabel,
sortDirection, search, location,
ticketCount, status, styleOption).pipe(catchError(() => of()), finalize(() => this.loadingSubject.next(false)))
.subscribe(responses => {
this.countValue = responses['count'];
this.dataSubject.next(responses['response']);
});
}

..


Below is service file:



//test.service.ts
export class TestService {
getData(
pageNumber, pageSize, sortLabel, sortDirection, search,
location, ticketCount, status, styleOption): Observable<any> {
const object = {
pageNumber: pageNumber,
pageSize: pageSize,
sortLabel: sortLabel,
sortDirection: sortDirection,
search: search,
location: location,
ticketCount: ticketCount,
status: status,
styleOption: styleOption
};

return this.apiService.post(this.config.api_url +
'/testOrder/getall', object).pipe(
map(res => res)
);
}


The above there file is my code which I need to test using Karma and Jasmine



The way I tried to test the method am sharing:



// test.component.spec.ts

it('should call and run #loaddata()', () => {
spyOn(comp, 'loadDataPage');
comp.testcode = 'TEST';
comp.paginator.pageIndex = 0;
comp.paginator.pageSize = 25;
comp.sort.active = null;
comp.sort.direction = 'desc';
comp.input.nativeElement.value = null;
comp.locationSelectedValue = ;
comp.testticketSelectedValue = ;
comp.teststatusSelectedValue = ;
comp.styleOptionSelectedValue = ;

if (!expect(comp.companycode).toEqual('TEST')) {
expect(comp.paginator.pageIndex + 1).toBe(1);
expect(comp.dataSource.loadDatas).toHaveBeenCalledWith(
comp.paginator.pageIndex,
comp.paginator.pageSize,
comp.sort.active,
comp.sort.direction,
comp.input.nativeElement.value,
comp.locationSelectedValue,
comp.testticketSelectedValue,
comp.teststatusSelectedValue,
comp.styleOptionSelectedValue);
} else {
expect(comp.paginator.pageIndex + 1).toBe(1);
}
});









share|improve this question
























  • If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 19 at 19:54










  • I have attended it and my code I am editing with my test file.
    – Rahul Tibrewal
    Nov 19 at 19:55






  • 1




    Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
    – dmcgrandle
    Nov 19 at 20:30

















up vote
0
down vote

favorite












I am like a born baby for writing Angular Test cases but trying to learn and I am using Sonar Scanner for checking code coverage.



Am sharing my code, please go have a look:



`// test.component.ts 

dataSource: TestDataSource;
..
// method I am trying to test

loadDataPage() {
if (this.code === undefined || this.code === 'Test') {
this.pageNo = this.paginator.pageIndex + 1;
this.dataSource.loadDatas(
this.paginator.pageIndex,
this.paginator.pageSize,
this.sort.active,
this.sort.direction,
this.input.nativeElement.value,
this.locationSelectedValue,
this.testticketSelectedValue,
this.teststatusSelectedValue,
this.styleOptionSelectedValue
);
} else {
this.getUserByName(this.Name);
}
}

...


Below is Datasource file:



// testDatasource.ts

export class TestDataSource implements DataSource<any> {

..
loadDatas(pageIndex: number, pageSize: number, sortLabel: string,
sortDirection: string, search: any, location: string,
testticketCount: string, teststatus: string, styleOption: string) {
this.loadingSubject.next(true);
this.testService.getData(pageIndex, pageSize, sortLabel,
sortDirection, search, location,
ticketCount, status, styleOption).pipe(catchError(() => of()), finalize(() => this.loadingSubject.next(false)))
.subscribe(responses => {
this.countValue = responses['count'];
this.dataSubject.next(responses['response']);
});
}

..


Below is service file:



//test.service.ts
export class TestService {
getData(
pageNumber, pageSize, sortLabel, sortDirection, search,
location, ticketCount, status, styleOption): Observable<any> {
const object = {
pageNumber: pageNumber,
pageSize: pageSize,
sortLabel: sortLabel,
sortDirection: sortDirection,
search: search,
location: location,
ticketCount: ticketCount,
status: status,
styleOption: styleOption
};

return this.apiService.post(this.config.api_url +
'/testOrder/getall', object).pipe(
map(res => res)
);
}


The above there file is my code which I need to test using Karma and Jasmine



The way I tried to test the method am sharing:



// test.component.spec.ts

it('should call and run #loaddata()', () => {
spyOn(comp, 'loadDataPage');
comp.testcode = 'TEST';
comp.paginator.pageIndex = 0;
comp.paginator.pageSize = 25;
comp.sort.active = null;
comp.sort.direction = 'desc';
comp.input.nativeElement.value = null;
comp.locationSelectedValue = ;
comp.testticketSelectedValue = ;
comp.teststatusSelectedValue = ;
comp.styleOptionSelectedValue = ;

if (!expect(comp.companycode).toEqual('TEST')) {
expect(comp.paginator.pageIndex + 1).toBe(1);
expect(comp.dataSource.loadDatas).toHaveBeenCalledWith(
comp.paginator.pageIndex,
comp.paginator.pageSize,
comp.sort.active,
comp.sort.direction,
comp.input.nativeElement.value,
comp.locationSelectedValue,
comp.testticketSelectedValue,
comp.teststatusSelectedValue,
comp.styleOptionSelectedValue);
} else {
expect(comp.paginator.pageIndex + 1).toBe(1);
}
});









share|improve this question
























  • If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 19 at 19:54










  • I have attended it and my code I am editing with my test file.
    – Rahul Tibrewal
    Nov 19 at 19:55






  • 1




    Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
    – dmcgrandle
    Nov 19 at 20:30















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am like a born baby for writing Angular Test cases but trying to learn and I am using Sonar Scanner for checking code coverage.



Am sharing my code, please go have a look:



`// test.component.ts 

dataSource: TestDataSource;
..
// method I am trying to test

loadDataPage() {
if (this.code === undefined || this.code === 'Test') {
this.pageNo = this.paginator.pageIndex + 1;
this.dataSource.loadDatas(
this.paginator.pageIndex,
this.paginator.pageSize,
this.sort.active,
this.sort.direction,
this.input.nativeElement.value,
this.locationSelectedValue,
this.testticketSelectedValue,
this.teststatusSelectedValue,
this.styleOptionSelectedValue
);
} else {
this.getUserByName(this.Name);
}
}

...


Below is Datasource file:



// testDatasource.ts

export class TestDataSource implements DataSource<any> {

..
loadDatas(pageIndex: number, pageSize: number, sortLabel: string,
sortDirection: string, search: any, location: string,
testticketCount: string, teststatus: string, styleOption: string) {
this.loadingSubject.next(true);
this.testService.getData(pageIndex, pageSize, sortLabel,
sortDirection, search, location,
ticketCount, status, styleOption).pipe(catchError(() => of()), finalize(() => this.loadingSubject.next(false)))
.subscribe(responses => {
this.countValue = responses['count'];
this.dataSubject.next(responses['response']);
});
}

..


Below is service file:



//test.service.ts
export class TestService {
getData(
pageNumber, pageSize, sortLabel, sortDirection, search,
location, ticketCount, status, styleOption): Observable<any> {
const object = {
pageNumber: pageNumber,
pageSize: pageSize,
sortLabel: sortLabel,
sortDirection: sortDirection,
search: search,
location: location,
ticketCount: ticketCount,
status: status,
styleOption: styleOption
};

return this.apiService.post(this.config.api_url +
'/testOrder/getall', object).pipe(
map(res => res)
);
}


The above there file is my code which I need to test using Karma and Jasmine



The way I tried to test the method am sharing:



// test.component.spec.ts

it('should call and run #loaddata()', () => {
spyOn(comp, 'loadDataPage');
comp.testcode = 'TEST';
comp.paginator.pageIndex = 0;
comp.paginator.pageSize = 25;
comp.sort.active = null;
comp.sort.direction = 'desc';
comp.input.nativeElement.value = null;
comp.locationSelectedValue = ;
comp.testticketSelectedValue = ;
comp.teststatusSelectedValue = ;
comp.styleOptionSelectedValue = ;

if (!expect(comp.companycode).toEqual('TEST')) {
expect(comp.paginator.pageIndex + 1).toBe(1);
expect(comp.dataSource.loadDatas).toHaveBeenCalledWith(
comp.paginator.pageIndex,
comp.paginator.pageSize,
comp.sort.active,
comp.sort.direction,
comp.input.nativeElement.value,
comp.locationSelectedValue,
comp.testticketSelectedValue,
comp.teststatusSelectedValue,
comp.styleOptionSelectedValue);
} else {
expect(comp.paginator.pageIndex + 1).toBe(1);
}
});









share|improve this question















I am like a born baby for writing Angular Test cases but trying to learn and I am using Sonar Scanner for checking code coverage.



Am sharing my code, please go have a look:



`// test.component.ts 

dataSource: TestDataSource;
..
// method I am trying to test

loadDataPage() {
if (this.code === undefined || this.code === 'Test') {
this.pageNo = this.paginator.pageIndex + 1;
this.dataSource.loadDatas(
this.paginator.pageIndex,
this.paginator.pageSize,
this.sort.active,
this.sort.direction,
this.input.nativeElement.value,
this.locationSelectedValue,
this.testticketSelectedValue,
this.teststatusSelectedValue,
this.styleOptionSelectedValue
);
} else {
this.getUserByName(this.Name);
}
}

...


Below is Datasource file:



// testDatasource.ts

export class TestDataSource implements DataSource<any> {

..
loadDatas(pageIndex: number, pageSize: number, sortLabel: string,
sortDirection: string, search: any, location: string,
testticketCount: string, teststatus: string, styleOption: string) {
this.loadingSubject.next(true);
this.testService.getData(pageIndex, pageSize, sortLabel,
sortDirection, search, location,
ticketCount, status, styleOption).pipe(catchError(() => of()), finalize(() => this.loadingSubject.next(false)))
.subscribe(responses => {
this.countValue = responses['count'];
this.dataSubject.next(responses['response']);
});
}

..


Below is service file:



//test.service.ts
export class TestService {
getData(
pageNumber, pageSize, sortLabel, sortDirection, search,
location, ticketCount, status, styleOption): Observable<any> {
const object = {
pageNumber: pageNumber,
pageSize: pageSize,
sortLabel: sortLabel,
sortDirection: sortDirection,
search: search,
location: location,
ticketCount: ticketCount,
status: status,
styleOption: styleOption
};

return this.apiService.post(this.config.api_url +
'/testOrder/getall', object).pipe(
map(res => res)
);
}


The above there file is my code which I need to test using Karma and Jasmine



The way I tried to test the method am sharing:



// test.component.spec.ts

it('should call and run #loaddata()', () => {
spyOn(comp, 'loadDataPage');
comp.testcode = 'TEST';
comp.paginator.pageIndex = 0;
comp.paginator.pageSize = 25;
comp.sort.active = null;
comp.sort.direction = 'desc';
comp.input.nativeElement.value = null;
comp.locationSelectedValue = ;
comp.testticketSelectedValue = ;
comp.teststatusSelectedValue = ;
comp.styleOptionSelectedValue = ;

if (!expect(comp.companycode).toEqual('TEST')) {
expect(comp.paginator.pageIndex + 1).toBe(1);
expect(comp.dataSource.loadDatas).toHaveBeenCalledWith(
comp.paginator.pageIndex,
comp.paginator.pageSize,
comp.sort.active,
comp.sort.direction,
comp.input.nativeElement.value,
comp.locationSelectedValue,
comp.testticketSelectedValue,
comp.teststatusSelectedValue,
comp.styleOptionSelectedValue);
} else {
expect(comp.paginator.pageIndex + 1).toBe(1);
}
});






angular jasmine angular5 karma-jasmine karma-coverage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:56

























asked Nov 19 at 19:51









Rahul Tibrewal

444




444












  • If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 19 at 19:54










  • I have attended it and my code I am editing with my test file.
    – Rahul Tibrewal
    Nov 19 at 19:55






  • 1




    Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
    – dmcgrandle
    Nov 19 at 20:30




















  • If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
    – dmcgrandle
    Nov 19 at 19:54










  • I have attended it and my code I am editing with my test file.
    – Rahul Tibrewal
    Nov 19 at 19:55






  • 1




    Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
    – dmcgrandle
    Nov 19 at 20:30


















If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
– dmcgrandle
Nov 19 at 19:54




If you haven't attempted this yourself yet, please do so. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question and include your attempted .spec file. For more information, please see How to Ask and take the tour.
– dmcgrandle
Nov 19 at 19:54












I have attended it and my code I am editing with my test file.
– Rahul Tibrewal
Nov 19 at 19:55




I have attended it and my code I am editing with my test file.
– Rahul Tibrewal
Nov 19 at 19:55




1




1




Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
– dmcgrandle
Nov 19 at 20:30






Are you trying to unit test only the file test.component.ts? Do you wish to test it in isolation from it's injected service(s)? I notice you are spying on the component method itself, this will prevent that method from being executed (and tested) which is likely not what you want. Above all this, it is still not clear to me what you are asking - are you encountering some sort of error? I suggest you put all the relevant code into a stackblitz to show it in action. Feel free to fork this one.
– dmcgrandle
Nov 19 at 20:30



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381688%2funit-testing-in-angular-5-with-component-data-source-and-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381688%2funit-testing-in-angular-5-with-component-data-source-and-service%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]