Skip to content

Commit

Permalink
test(transactional): add tests for proxying metadata and properties t…
Browse files Browse the repository at this point in the history
…hrough decorator
  • Loading branch information
nicobuzeta committed May 12, 2024
1 parent 19ff48d commit 93ddabe
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/transactional/test/mock-decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export function MetadataDefiningDecorator(): MethodDecorator {
return (
_target: any,
_propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => {
Reflect.defineMetadata('testproperty', 'testvalue', descriptor.value);
return descriptor;
};
}

export function PropertyDefiningDecorator(): MethodDecorator {
return (
_target: any,
_propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<any>,
) => {
descriptor.value.testproperty = 'testvalue';
return descriptor;
};
}
23 changes: 23 additions & 0 deletions packages/transactional/test/transactional.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {
MockDbConnection,
TransactionAdapterMock,
} from './transaction-adapter-mock';
import {
MetadataDefiningDecorator,
PropertyDefiningDecorator,
} from './mock-decorators';

@Injectable()
class CalledService {
Expand All @@ -30,6 +34,8 @@ class CallingService {
) {}

@Transactional()
@MetadataDefiningDecorator()
@PropertyDefiningDecorator()
async transactionWithDecorator() {
const q1 = await this.calledService.doWork(1);
const q2 = await this.calledService.doOtherWork(2);
Expand Down Expand Up @@ -210,4 +216,21 @@ describe('Transactional', () => {
expect(queries).toEqual([['SELECT 5'], ['SELECT 6']]);
});
});

describe('should mantain method properties set by other decorators', () => {
it('should mantain metadata', () => {
expect(
Reflect.getMetadata(
'testproperty',
callingService.transactionWithDecorator,
),
).toEqual('testvalue');
});

it('should mantain property', () => {
expect(
callingService.transactionWithDecorator['testproperty'],
).toEqual('testvalue');
});
});
});

0 comments on commit 93ddabe

Please sign in to comment.