[英]Angular Integration test: testing a function that is called when event from other component is emitted, how do I mock the event data?
In my component AI 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. 在我的组件AI中,有一个功能可以根据从组件B发出的数据来更新视图。我不想集成组件B并进行实际操作,即使此测试太复杂了。
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:
问题是,将数据作为“事件”发送到组件A中的函数似乎不起作用:
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.
我也尝试过使它成为一个名为'event'的javascript对象,该对象内部具有键-值对,但也没有产生任何运气。
component.updateEvent(data) code: component.updateEvent(data)代码:
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.`);
}
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. 我已经看过DebugElement.triggerEvent,但是不幸的是文档已经过时,而且我自己没有办法确定如何做到这一点。 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: 我最终集成了这两个组件,并使用来自第二个组件的标准JSON对象触发了它,如下所示:
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. 您可以看到我刚刚将其命名为component2和Fixture2,并在相同的1个测试床上添加了两者的依赖关系。
I'll probably name them something more relevant instead of component and component2. 我可能会给它们命名更相关的名称,而不是component和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();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.