Skip to content

Commit

Permalink
fix: Component decorators would not work if Component was used more t…
Browse files Browse the repository at this point in the history
…han one time
  • Loading branch information
Miłosz Mandowski committed Oct 27, 2021
1 parent 25b9d3d commit 5564173
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 2 additions & 10 deletions packages/ovee/src/core/InstanceDecorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ export default class InstanceDecorators {

[INITIALIZE_DECORATORS](): void {
forEachStaticPrototype<typeof InstanceDecorators>(this, ctor => {
if (ctor[INSTANCE_DECORATORS]) {
ctor[INSTANCE_DECORATORS]?.forEach(fn => fn(this));

delete ctor[INSTANCE_DECORATORS];
}
ctor[INSTANCE_DECORATORS]?.forEach(fn => fn(this));
});
}

[DESTROY_DECORATORS](): void {
forEachStaticPrototype<typeof InstanceDecorators>(this, ctor => {
if (ctor[INSTANCE_DECORATORS_DESTRUCTORS]) {
ctor[INSTANCE_DECORATORS_DESTRUCTORS]!.forEach(fn => fn(this));

delete ctor[INSTANCE_DECORATORS_DESTRUCTORS];
}
ctor[INSTANCE_DECORATORS_DESTRUCTORS]?.forEach(fn => fn(this));
});
}
}
18 changes: 18 additions & 0 deletions packages/ovee/tests/unit/decorators/instanceDecorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ describe('Instance Decorators System', () => {
expect(decoratorCb1.mock.calls[0][0].instance).toBe(instance);
expect(decoratorCb2.mock.calls[0][0].instance).toBe(instance);
});

it('makes decorators reusabel between multiple instances', () => {
const decoratorCb = jest.fn();
const decorator = jest.fn(instanceDecoratorFactory(decoratorCb)());

class TestComponent extends Component {
@decorator
test: any;
}

const instance1 = createComponent(TestComponent);
const instance2 = createComponent(TestComponent);

expect(decorator).toHaveBeenCalledTimes(1);
expect(decoratorCb).toHaveBeenCalledTimes(2);
expect(decoratorCb.mock.calls[0][0].instance).toBe(instance1);
expect(decoratorCb.mock.calls[1][0].instance).toBe(instance2);
});
});

0 comments on commit 5564173

Please # to comment.