From fb4b97dcdcebed947f1273ea4fa8e0ee2a0e681b Mon Sep 17 00:00:00 2001 From: Isaac Datlof Date: Mon, 27 Nov 2017 13:10:21 -0500 Subject: [PATCH] fix: instantiate event emitters in component constructor --- lib/mock_component.spec.ts | 7 +++++++ lib/mock_component.ts | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/mock_component.spec.ts b/lib/mock_component.spec.ts index 45650cfdb1..f7d41f1938 100644 --- a/lib/mock_component.spec.ts +++ b/lib/mock_component.spec.ts @@ -60,6 +60,13 @@ describe('MockComponent', () => { expect(annotations.template).toEqual(''); }); + // make pass when testbed is brought in + xit('each instance of a mocked component should have its own event emitter', () => { + const mockedComponent1 = MockComponent(ExampleComponent); + const mockedComponent2 = MockComponent(ExampleComponent); + expect((mockedComponent1 as any).someOutput).not.toEqual((mockedComponent2 as any).someOutput); + }); + describe('sometimes components are built like this, not sure why', () => { it('the mock should have the same selector as the passed in component', () => { const mockedComponent = MockComponent(exampleComponent); diff --git a/lib/mock_component.ts b/lib/mock_component.ts index fe53e2725c..441d7f34c5 100644 --- a/lib/mock_component.ts +++ b/lib/mock_component.ts @@ -15,11 +15,15 @@ export function MockComponent(component: Type): Type isInput(propertyMetadata[meta])); options.outputs = Object.keys(propertyMetadata).filter((meta) => isOutput(propertyMetadata[meta])); - class ComponentMock {} - - options.outputs.forEach((output) => { - (ComponentMock as any).prototype[output] = new EventEmitter(); - }); + /* tslint:disable:no-unnecessary-class */ + class ComponentMock { + constructor() { + options.outputs.forEach((output) => { + (ComponentMock as any).prototype[output] = new EventEmitter(); + }); + } + } + /* tslint:enable:no-unnecessary-class */ return Component(options as Component)(ComponentMock as Type); }