Skip to content

Commit

Permalink
fix: instantiate event emitters in component constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ike18t committed Nov 27, 2017
1 parent 0c74d7a commit fb4b97d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 7 additions & 0 deletions lib/mock_component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ describe('MockComponent', () => {
expect(annotations.template).toEqual('<ng-content></ng-content>');
});

// 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);
Expand Down
14 changes: 9 additions & 5 deletions lib/mock_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ export function MockComponent<TComponent>(component: Type<TComponent>): Type<TCo
options.inputs = Object.keys(propertyMetadata).filter((meta) => 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<any>();
});
/* tslint:disable:no-unnecessary-class */
class ComponentMock {
constructor() {
options.outputs.forEach((output) => {
(ComponentMock as any).prototype[output] = new EventEmitter<any>();
});
}
}
/* tslint:enable:no-unnecessary-class */

return Component(options as Component)(ComponentMock as Type<TComponent>);
}
Expand Down

0 comments on commit fb4b97d

Please sign in to comment.