Skip to content

Commit

Permalink
test: add tests for extending extras
Browse files Browse the repository at this point in the history
  • Loading branch information
effervescentia committed Apr 4, 2023
1 parent fd070b8 commit daa215e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ describe('@EventPattern', () => {
});

describe('decorator overloads', () => {
const additionalExtras = { foo: 'bar' };

class TestComponent1 {
@EventPattern(pattern)
public static test() {}
Expand All @@ -70,6 +72,18 @@ describe('@EventPattern', () => {
@EventPattern(pattern, Transport.TCP, extras)
public static test() {}
}
class TestComponent5 {
@EventPattern(pattern, Transport.TCP, extras)
@((
(): MethodDecorator => (_target, _key, descriptor) =>
Reflect.defineMetadata(
PATTERN_EXTRAS_METADATA,
additionalExtras,
descriptor.value,
)
)())
public static test() {}
}

it(`should enhance method with ${PATTERN_METADATA} metadata`, () => {
const [metadataArg] = Reflect.getMetadata(
Expand All @@ -86,7 +100,7 @@ describe('@EventPattern', () => {
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.undefined;
expect(extrasArg).to.be.undefined;
expect(extrasArg).to.be.eql({});
});

it(`should enhance method with ${PATTERN_METADATA}, ${TRANSPORT_METADATA} metadata`, () => {
Expand All @@ -104,7 +118,7 @@ describe('@EventPattern', () => {
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.undefined;
expect(extrasArg).to.be.eql({});
});

it(`should enhance method with ${PATTERN_METADATA}, ${PATTERN_EXTRAS_METADATA} metadata`, () => {
Expand Down Expand Up @@ -143,5 +157,26 @@ ${PATTERN_EXTRAS_METADATA} metadata`, () => {
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.eql(extras);
});

it(`should merge with existing ${PATTERN_EXTRAS_METADATA} metadata`, () => {
const [metadataArg] = Reflect.getMetadata(
PATTERN_METADATA,
TestComponent5.test,
);
const transportArg = Reflect.getMetadata(
TRANSPORT_METADATA,
TestComponent5.test,
);
const extrasArg = Reflect.getMetadata(
PATTERN_EXTRAS_METADATA,
TestComponent5.test,
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.eql({
...additionalExtras,
...extras,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ describe('@MessagePattern', () => {
});

describe('decorator overloads', () => {
const additionalExtras = { foo: 'bar' };

class TestComponent1 {
@MessagePattern(pattern)
public static test() {}
Expand All @@ -52,6 +54,18 @@ describe('@MessagePattern', () => {
@MessagePattern(pattern, Transport.TCP, extras)
public static test() {}
}
class TestComponent5 {
@MessagePattern(pattern, Transport.TCP, extras)
@((
(): MethodDecorator => (_target, _key, descriptor) =>
Reflect.defineMetadata(
PATTERN_EXTRAS_METADATA,
additionalExtras,
descriptor.value,
)
)())
public static test() {}
}

it(`should enhance method with ${PATTERN_METADATA} metadata`, () => {
const [metadataArg] = Reflect.getMetadata(
Expand All @@ -68,7 +82,7 @@ describe('@MessagePattern', () => {
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.undefined;
expect(extrasArg).to.be.undefined;
expect(extrasArg).to.be.eql({});
});

it(`should enhance method with ${PATTERN_METADATA}, ${TRANSPORT_METADATA} metadata`, () => {
Expand All @@ -86,7 +100,7 @@ describe('@MessagePattern', () => {
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.undefined;
expect(extrasArg).to.be.eql({});
});

it(`should enhance method with ${PATTERN_METADATA}, ${PATTERN_EXTRAS_METADATA} metadata`, () => {
Expand Down Expand Up @@ -125,6 +139,27 @@ ${PATTERN_EXTRAS_METADATA} metadata`, () => {
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.eql(extras);
});

it(`should merge with existing ${PATTERN_EXTRAS_METADATA} metadata`, () => {
const [metadataArg] = Reflect.getMetadata(
PATTERN_METADATA,
TestComponent5.test,
);
const transportArg = Reflect.getMetadata(
TRANSPORT_METADATA,
TestComponent5.test,
);
const extrasArg = Reflect.getMetadata(
PATTERN_EXTRAS_METADATA,
TestComponent5.test,
);
expect(metadataArg).to.be.eql(pattern);
expect(transportArg).to.be.eql(Transport.TCP);
expect(extrasArg).to.be.eql({
...additionalExtras,
...extras,
});
});
});
});

Expand Down

0 comments on commit daa215e

Please sign in to comment.