Skip to content

Commit

Permalink
fix: static derive() function
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Dec 15, 2023
1 parent e0b8489 commit b84f7d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,22 @@ describe('integration test', () => {
}

const EnvironmentBase = JestEnvironmentEmit(OriginalEnvironment as any, undefined, 'Base');
const Environment = EnvironmentBase.derive(fnSubscription, 'WithMocks');
class EnvironmentMiddle1 extends EnvironmentBase {
extraMethod() {
return 1;
}
}
class EnvironmentMiddle2 extends EnvironmentMiddle1 {
extraMethod() {
return super.extraMethod() + 1;
}

extraMethod2() {
return 2;
}
}

const Environment = EnvironmentMiddle2.derive(fnSubscription, 'WithMocks');

const config = {
globalConfig: {},
Expand All @@ -36,6 +51,8 @@ describe('integration test', () => {
};
const context = {};
const env = new Environment(config, context);
expect((env as any).extraMethod()).toBe(2);
expect((env as any).extraMethod2()).toBe(2);
const onAddHook = jest.fn();
env.testEvents.on('add_hook', onAddHook);

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ export default function WithEmitter<E extends JestEnvironment>(
callback: EnvironmentListenerFn<E>,
DerivedMixinName = MixinName,
): WithEmitterClass<E> {
const CurrentClass = this as unknown as WithEmitterClass<E>;
const derivedName = `${DerivedMixinName}(${BaseClassName})`;
const resultClass = {
[`${derivedName}`]: class extends ClassWithEmitter {},
[`${derivedName}`]: class extends CurrentClass {},
}[derivedName];
registerSubscription(resultClass, callback);
return resultClass;
Expand Down

0 comments on commit b84f7d0

Please sign in to comment.