spectator: All instances of a MockComponent share the same event emitter, therefore I cannot emit events on individual instances.
Hi!
First of, thanks for an awesome testing framework 😃
I’m currently mocking some outputs using MockComponent. There seems to be an issue where all instances of the mock component share the same event emitter. Calling emit on a single instance will therefore call the bound method multiple times, once for each instance.
Here’s a test to demonstrate this:
import { Component, EventEmitter, Output } from "@angular/core";
import { createTestComponentFactory, MockComponent } from "@netbasal/spectator";
@Component({
selector: "app-subcomponent-component",
template: ``,
})
class SubcomponentDemoComponent {
@Output()
public emitter = new EventEmitter();
}
@Component({
selector: "app-demo-component",
template: `
<app-subcomponent-demo (emitter)="boundMethod()"></app-subcomponent-demo>
<app-subcomponent-demo (emitter)="boundMethod()"></app-subcomponent-demo>
`,
})
class DemoComponent {
public boundMethod() {}
}
describe("spectator", () => {
const create = createTestComponentFactory({
component: DemoComponent,
shallow: true,
declarations: [
MockComponent({
selector: "app-subcomponent-demo",
identifier: SubcomponentDemoComponent,
outputs: ["emitter"],
}),
],
});
it("should differentiate between instances of event emitters", () => {
const spectator = create({}, false);
spyOn(spectator.component, "boundMethod");
const instances = spectator.queryAll<SubcomponentDemoComponent>(SubcomponentDemoComponent);
instances[0].emitter.emit();
expect(spectator.component.boundMethod).toHaveBeenCalledTimes(1);
});
});
The function, boundMethod should only be called once because only one value is emitted, however, it is called twice.
Could you have a look into this?
Thanks and cheers, Oliver
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (10 by maintainers)
Commits related to this issue
- docs: mock components with ng-mocks Related to ngneat/spectator#59 — committed to maxime1992/spectator by maxime1992 5 years ago
- docs: mock components with ng-mocks Related to ngneat/spectator#59 — committed to maxime1992/spectator by maxime1992 5 years ago
- docs: mock components with ng-mocks (#206) Related to ngneat/spectator#59 — committed to ngneat/spectator by maxime1992 5 years ago
All right, maybe it’s good to update the docs accordingly (and deprecate the Spectator one, and update the existing unit tests to properly demonstrate this compatibility).
@ike18t Yes, we are using ng-mocks. Spectator and ng-mocks together are a win-win 😃
I came across this lib after reading @NetanelBasal 's medium article. I’m stoked that you guys are going to pull in ng-mocks. Let me know if there is anything you guys need from me.
Hey all, thanks for the quick responses and the swift resolution and clarification! 🥇
New doc proposal for: https://netbasal.gitbook.io/spectator/components/untitled-1 (Mock Components).