Angular Integration test: testing a function that is called when event from other component is emitted, how...
up vote
2
down vote
favorite
In my component A I have a function that updates the view based on data emitted from component B. I don't want to integrate component B and make an actual even as that's too complex for this test.
I just want to call the function and pass the data to the function. The problem is, sending the data as an 'event' to the function in component A does not seem to work:
it('should update the video with the data from the edit component', () => {
let event;
event.title = 'New Title';
event.description = 'New Description';
event.link = 'New Link';
event.videoCategory = 'New Category';
event.categories = '2';
event.a14Only = 0;
component.updateVideoCard(event);
fixture.detectChanges();
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBe('0');
expect(component.editDisabled).toBeTruthy();
});
and that event ends up as 'undefined'. I have also tried making it a javascript object called 'event' that has the key-value pairs inside it but that has yielded no luck either.
component.updateEvent(data) code:
updateVideoCard(event) {
this.videoTitle = event.title;
this.videoDescription = event.description;
this.videoLink = event.link;
this.videoCategory = event.category;
this.categoryID = event.categories;
if (event.a14Only === 1) {
this.a14Only = true;
} else {
this.a14Only = false;
}
this.enableEditor = false;
this.notification.done(`${this.videoTitle} updated successfully.`);
}
angular jasmine angular-testing angular-event-emitter
|
show 2 more comments
up vote
2
down vote
favorite
In my component A I have a function that updates the view based on data emitted from component B. I don't want to integrate component B and make an actual even as that's too complex for this test.
I just want to call the function and pass the data to the function. The problem is, sending the data as an 'event' to the function in component A does not seem to work:
it('should update the video with the data from the edit component', () => {
let event;
event.title = 'New Title';
event.description = 'New Description';
event.link = 'New Link';
event.videoCategory = 'New Category';
event.categories = '2';
event.a14Only = 0;
component.updateVideoCard(event);
fixture.detectChanges();
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBe('0');
expect(component.editDisabled).toBeTruthy();
});
and that event ends up as 'undefined'. I have also tried making it a javascript object called 'event' that has the key-value pairs inside it but that has yielded no luck either.
component.updateEvent(data) code:
updateVideoCard(event) {
this.videoTitle = event.title;
this.videoDescription = event.description;
this.videoLink = event.link;
this.videoCategory = event.category;
this.categoryID = event.categories;
if (event.a14Only === 1) {
this.a14Only = true;
} else {
this.a14Only = false;
}
this.enableEditor = false;
this.notification.done(`${this.videoTitle} updated successfully.`);
}
angular jasmine angular-testing angular-event-emitter
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
1
Could you sharecomponent.updateVideoCard(event)
with us?
– mixth
Nov 20 at 7:18
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04
|
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In my component A I have a function that updates the view based on data emitted from component B. I don't want to integrate component B and make an actual even as that's too complex for this test.
I just want to call the function and pass the data to the function. The problem is, sending the data as an 'event' to the function in component A does not seem to work:
it('should update the video with the data from the edit component', () => {
let event;
event.title = 'New Title';
event.description = 'New Description';
event.link = 'New Link';
event.videoCategory = 'New Category';
event.categories = '2';
event.a14Only = 0;
component.updateVideoCard(event);
fixture.detectChanges();
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBe('0');
expect(component.editDisabled).toBeTruthy();
});
and that event ends up as 'undefined'. I have also tried making it a javascript object called 'event' that has the key-value pairs inside it but that has yielded no luck either.
component.updateEvent(data) code:
updateVideoCard(event) {
this.videoTitle = event.title;
this.videoDescription = event.description;
this.videoLink = event.link;
this.videoCategory = event.category;
this.categoryID = event.categories;
if (event.a14Only === 1) {
this.a14Only = true;
} else {
this.a14Only = false;
}
this.enableEditor = false;
this.notification.done(`${this.videoTitle} updated successfully.`);
}
angular jasmine angular-testing angular-event-emitter
In my component A I have a function that updates the view based on data emitted from component B. I don't want to integrate component B and make an actual even as that's too complex for this test.
I just want to call the function and pass the data to the function. The problem is, sending the data as an 'event' to the function in component A does not seem to work:
it('should update the video with the data from the edit component', () => {
let event;
event.title = 'New Title';
event.description = 'New Description';
event.link = 'New Link';
event.videoCategory = 'New Category';
event.categories = '2';
event.a14Only = 0;
component.updateVideoCard(event);
fixture.detectChanges();
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBe('0');
expect(component.editDisabled).toBeTruthy();
});
and that event ends up as 'undefined'. I have also tried making it a javascript object called 'event' that has the key-value pairs inside it but that has yielded no luck either.
component.updateEvent(data) code:
updateVideoCard(event) {
this.videoTitle = event.title;
this.videoDescription = event.description;
this.videoLink = event.link;
this.videoCategory = event.category;
this.categoryID = event.categories;
if (event.a14Only === 1) {
this.a14Only = true;
} else {
this.a14Only = false;
}
this.enableEditor = false;
this.notification.done(`${this.videoTitle} updated successfully.`);
}
angular jasmine angular-testing angular-event-emitter
angular jasmine angular-testing angular-event-emitter
edited Nov 20 at 8:48
asked Nov 20 at 1:07
SebastianG
706115
706115
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
1
Could you sharecomponent.updateVideoCard(event)
with us?
– mixth
Nov 20 at 7:18
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04
|
show 2 more comments
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
1
Could you sharecomponent.updateVideoCard(event)
with us?
– mixth
Nov 20 at 7:18
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
1
1
Could you share
component.updateVideoCard(event)
with us?– mixth
Nov 20 at 7:18
Could you share
component.updateVideoCard(event)
with us?– mixth
Nov 20 at 7:18
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
I've looked at the DebugElement.triggerEvent but unfortunately the documentation was outdated and haven't had a lot of luck figuring it out by myself how to do it. It also seemed to require integrating the second component anyway.
I ended up integrating the 2 components and just triggering it with a standard JSON object from the second component like this:
describe('VideoCardComponent', () => {
let component: VideoCardComponent;
let fixture: ComponentFixture<VideoCardComponent>;
let fixture2: ComponentFixture<EditVideoComponent>;
let component2: EditVideoComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
FontAwesomeModule,
BrowserModule,
FlexLayoutModule,
RouterTestingModule,
ReactiveFormsModule,
FormsModule,
MatSelectModule,
MatOptionModule,
MatInputModule,
MatSlideToggleModule
],
declarations: [VideoCardComponent, SafepipePipe, EditVideoComponent],
providers: [
{ provide: RestService, useClass: RestStub },
{ provide: NotificationService, useClass: NotificationStub }
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture2 = TestBed.createComponent(EditVideoComponent);
fixture = TestBed.createComponent(VideoCardComponent);
component = fixture.componentInstance;
component2 = fixture2.componentInstance;
fixture2.detectChanges();
fixture.detectChanges();
});
}));
You can see I just named it component2 and fixture2 and added the dependecies for both in the same 1 test bed.
I'll probably name them something more relevant instead of component and component2.
The fixed test:
it('should update the video with the data from the edit component', () => {
const data = [
{
title: 'New Title',
description: 'New Description',
link: 'New Link',
category: 'New Category',
categories: '2',
a14Only: 0
}
];
component2.updateVideoCard.subscribe(newVideo => {
component.updateVideoCard(newVideo);
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBeFalsy();
expect(component.editDisabled).toBeFalsy();
});
component2.updateLocalVideoData(data);
fixture2.detectChanges();
fixture.detectChanges();
});
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I've looked at the DebugElement.triggerEvent but unfortunately the documentation was outdated and haven't had a lot of luck figuring it out by myself how to do it. It also seemed to require integrating the second component anyway.
I ended up integrating the 2 components and just triggering it with a standard JSON object from the second component like this:
describe('VideoCardComponent', () => {
let component: VideoCardComponent;
let fixture: ComponentFixture<VideoCardComponent>;
let fixture2: ComponentFixture<EditVideoComponent>;
let component2: EditVideoComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
FontAwesomeModule,
BrowserModule,
FlexLayoutModule,
RouterTestingModule,
ReactiveFormsModule,
FormsModule,
MatSelectModule,
MatOptionModule,
MatInputModule,
MatSlideToggleModule
],
declarations: [VideoCardComponent, SafepipePipe, EditVideoComponent],
providers: [
{ provide: RestService, useClass: RestStub },
{ provide: NotificationService, useClass: NotificationStub }
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture2 = TestBed.createComponent(EditVideoComponent);
fixture = TestBed.createComponent(VideoCardComponent);
component = fixture.componentInstance;
component2 = fixture2.componentInstance;
fixture2.detectChanges();
fixture.detectChanges();
});
}));
You can see I just named it component2 and fixture2 and added the dependecies for both in the same 1 test bed.
I'll probably name them something more relevant instead of component and component2.
The fixed test:
it('should update the video with the data from the edit component', () => {
const data = [
{
title: 'New Title',
description: 'New Description',
link: 'New Link',
category: 'New Category',
categories: '2',
a14Only: 0
}
];
component2.updateVideoCard.subscribe(newVideo => {
component.updateVideoCard(newVideo);
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBeFalsy();
expect(component.editDisabled).toBeFalsy();
});
component2.updateLocalVideoData(data);
fixture2.detectChanges();
fixture.detectChanges();
});
add a comment |
up vote
0
down vote
I've looked at the DebugElement.triggerEvent but unfortunately the documentation was outdated and haven't had a lot of luck figuring it out by myself how to do it. It also seemed to require integrating the second component anyway.
I ended up integrating the 2 components and just triggering it with a standard JSON object from the second component like this:
describe('VideoCardComponent', () => {
let component: VideoCardComponent;
let fixture: ComponentFixture<VideoCardComponent>;
let fixture2: ComponentFixture<EditVideoComponent>;
let component2: EditVideoComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
FontAwesomeModule,
BrowserModule,
FlexLayoutModule,
RouterTestingModule,
ReactiveFormsModule,
FormsModule,
MatSelectModule,
MatOptionModule,
MatInputModule,
MatSlideToggleModule
],
declarations: [VideoCardComponent, SafepipePipe, EditVideoComponent],
providers: [
{ provide: RestService, useClass: RestStub },
{ provide: NotificationService, useClass: NotificationStub }
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture2 = TestBed.createComponent(EditVideoComponent);
fixture = TestBed.createComponent(VideoCardComponent);
component = fixture.componentInstance;
component2 = fixture2.componentInstance;
fixture2.detectChanges();
fixture.detectChanges();
});
}));
You can see I just named it component2 and fixture2 and added the dependecies for both in the same 1 test bed.
I'll probably name them something more relevant instead of component and component2.
The fixed test:
it('should update the video with the data from the edit component', () => {
const data = [
{
title: 'New Title',
description: 'New Description',
link: 'New Link',
category: 'New Category',
categories: '2',
a14Only: 0
}
];
component2.updateVideoCard.subscribe(newVideo => {
component.updateVideoCard(newVideo);
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBeFalsy();
expect(component.editDisabled).toBeFalsy();
});
component2.updateLocalVideoData(data);
fixture2.detectChanges();
fixture.detectChanges();
});
add a comment |
up vote
0
down vote
up vote
0
down vote
I've looked at the DebugElement.triggerEvent but unfortunately the documentation was outdated and haven't had a lot of luck figuring it out by myself how to do it. It also seemed to require integrating the second component anyway.
I ended up integrating the 2 components and just triggering it with a standard JSON object from the second component like this:
describe('VideoCardComponent', () => {
let component: VideoCardComponent;
let fixture: ComponentFixture<VideoCardComponent>;
let fixture2: ComponentFixture<EditVideoComponent>;
let component2: EditVideoComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
FontAwesomeModule,
BrowserModule,
FlexLayoutModule,
RouterTestingModule,
ReactiveFormsModule,
FormsModule,
MatSelectModule,
MatOptionModule,
MatInputModule,
MatSlideToggleModule
],
declarations: [VideoCardComponent, SafepipePipe, EditVideoComponent],
providers: [
{ provide: RestService, useClass: RestStub },
{ provide: NotificationService, useClass: NotificationStub }
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture2 = TestBed.createComponent(EditVideoComponent);
fixture = TestBed.createComponent(VideoCardComponent);
component = fixture.componentInstance;
component2 = fixture2.componentInstance;
fixture2.detectChanges();
fixture.detectChanges();
});
}));
You can see I just named it component2 and fixture2 and added the dependecies for both in the same 1 test bed.
I'll probably name them something more relevant instead of component and component2.
The fixed test:
it('should update the video with the data from the edit component', () => {
const data = [
{
title: 'New Title',
description: 'New Description',
link: 'New Link',
category: 'New Category',
categories: '2',
a14Only: 0
}
];
component2.updateVideoCard.subscribe(newVideo => {
component.updateVideoCard(newVideo);
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBeFalsy();
expect(component.editDisabled).toBeFalsy();
});
component2.updateLocalVideoData(data);
fixture2.detectChanges();
fixture.detectChanges();
});
I've looked at the DebugElement.triggerEvent but unfortunately the documentation was outdated and haven't had a lot of luck figuring it out by myself how to do it. It also seemed to require integrating the second component anyway.
I ended up integrating the 2 components and just triggering it with a standard JSON object from the second component like this:
describe('VideoCardComponent', () => {
let component: VideoCardComponent;
let fixture: ComponentFixture<VideoCardComponent>;
let fixture2: ComponentFixture<EditVideoComponent>;
let component2: EditVideoComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
MatCardModule,
MatButtonModule,
BrowserAnimationsModule,
FontAwesomeModule,
BrowserModule,
FlexLayoutModule,
RouterTestingModule,
ReactiveFormsModule,
FormsModule,
MatSelectModule,
MatOptionModule,
MatInputModule,
MatSlideToggleModule
],
declarations: [VideoCardComponent, SafepipePipe, EditVideoComponent],
providers: [
{ provide: RestService, useClass: RestStub },
{ provide: NotificationService, useClass: NotificationStub }
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents()
.then(() => {
fixture2 = TestBed.createComponent(EditVideoComponent);
fixture = TestBed.createComponent(VideoCardComponent);
component = fixture.componentInstance;
component2 = fixture2.componentInstance;
fixture2.detectChanges();
fixture.detectChanges();
});
}));
You can see I just named it component2 and fixture2 and added the dependecies for both in the same 1 test bed.
I'll probably name them something more relevant instead of component and component2.
The fixed test:
it('should update the video with the data from the edit component', () => {
const data = [
{
title: 'New Title',
description: 'New Description',
link: 'New Link',
category: 'New Category',
categories: '2',
a14Only: 0
}
];
component2.updateVideoCard.subscribe(newVideo => {
component.updateVideoCard(newVideo);
expect(component.videoTitle).toBe('New Title');
expect(component.videoLink).toBe('New Link');
expect(component.videoDescription).toBe('New Description');
expect(component.videoCategory).toBe('New Category');
expect(component.categoryID).toBe('2');
expect(component.a14Only).toBeFalsy();
expect(component.editDisabled).toBeFalsy();
});
component2.updateLocalVideoData(data);
fixture2.detectChanges();
fixture.detectChanges();
});
edited Nov 20 at 10:19
answered Nov 20 at 9:49
SebastianG
706115
706115
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384814%2fangular-integration-test-testing-a-function-that-is-called-when-event-from-othe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
This looks like a job for triggerEventHandler in the class DebugElement: angular.io/api/core/DebugElement#triggerEventHandler if you post the code of your component I can give you a code example.
– fmontes
Nov 20 at 1:16
1
Could you share
component.updateVideoCard(event)
with us?– mixth
Nov 20 at 7:18
@fmontes thank you. I will give this a try right away! sure! I have edited my post with this code. Note that this is where we consume the triggered event and data.
– SebastianG
Nov 20 at 8:49
Hi @SebastianG, what we need to see is the code of the component itself no the tests on them. Can you provide that code?
– fmontes
Nov 20 at 11:57
@fmontes it's right there in the thread above.
– SebastianG
Nov 20 at 13:04