Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add target #69

Merged
merged 8 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { afterAll, beforeAll, describe, expect, it, jest } from '@jest/globals';

jest.mock('@inversifyjs/reflect-metadata-utils');

import {
getReflectMetadata,
updateReflectMetadata,
} from '@inversifyjs/reflect-metadata-utils';

import { getTargetId } from './getTargetId';

describe(getTargetId.name, () => {
describe('when called, and getReflectMetadata() returns undefined', () => {
let result: unknown;

beforeAll(() => {
(
getReflectMetadata as jest.Mock<typeof getReflectMetadata>
).mockReturnValueOnce(0);

result = getTargetId();
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getReflectMetadata()', () => {
expect(getReflectMetadata).toHaveBeenCalledTimes(1);
expect(getReflectMetadata).toHaveBeenCalledWith(
Object,
'@inversifyjs/core/targetId',
);
});

it('should call updateReflectMetadata()', () => {
expect(updateReflectMetadata).toHaveBeenCalledTimes(1);
expect(updateReflectMetadata).toHaveBeenCalledWith(
Object,
'@inversifyjs/core/targetId',
0,
expect.any(Function),
);
});

it('should return default id', () => {
expect(result).toBe(0);
});
});

describe('when called, and getReflectMetadata() returns Number.MAX_SAFE_INTEGER', () => {
let result: unknown;

beforeAll(() => {
(
getReflectMetadata as jest.Mock<typeof getReflectMetadata>
).mockReturnValueOnce(Number.MAX_SAFE_INTEGER);

result = getTargetId();
});

afterAll(() => {
jest.clearAllMocks();
});

it('should call getReflectMetadata()', () => {
expect(getReflectMetadata).toHaveBeenCalledTimes(1);
expect(getReflectMetadata).toHaveBeenCalledWith(
Object,
'@inversifyjs/core/targetId',
);
});

it('should call updateReflectMetadata()', () => {
expect(updateReflectMetadata).toHaveBeenCalledTimes(1);
expect(updateReflectMetadata).toHaveBeenCalledWith(
Object,
'@inversifyjs/core/targetId',
Number.MAX_SAFE_INTEGER,
expect.any(Function),
);
});

it('should return default id', () => {
expect(result).toBe(Number.MAX_SAFE_INTEGER);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
getReflectMetadata,
updateReflectMetadata,
} from '@inversifyjs/reflect-metadata-utils';

const ID_METADATA: string = '@inversifyjs/core/targetId';

export function getTargetId(): number {
const targetId: number = getReflectMetadata<number>(Object, ID_METADATA) ?? 0;

if (targetId === Number.MAX_SAFE_INTEGER) {
updateReflectMetadata(
Object,
ID_METADATA,
targetId,
() => Number.MIN_SAFE_INTEGER,
);
} else {
updateReflectMetadata(
Object,
ID_METADATA,
targetId,
(id: number) => id + 1,
);
}

return targetId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { LazyServiceIdentifier, ServiceIdentifier } from '@inversifyjs/common';

import { LegacyMetadata } from '../../metadata/models/LegacyMetadata';
import { MetadataName } from '../../metadata/models/MetadataName';
import { MetadataTag } from '../../metadata/models/MetadataTag';
import { LegacyQueryableString } from '../../string/models/LegacyQueryableString';
import { LegacyTargetType } from './LegacyTargetType';

export interface LegacyTarget {
id: number;
serviceIdentifier: ServiceIdentifier | LazyServiceIdentifier;
type: LegacyTargetType;
name: LegacyQueryableString;
identifier: string | symbol;
metadata: LegacyMetadata[];
getNamedTag(): LegacyMetadata<MetadataName> | null;
getCustomTags(): LegacyMetadata[] | null;
hasTag(key: MetadataTag): boolean;
isArray(): boolean;
matchesArray(name: ServiceIdentifier): boolean;
isNamed(): boolean;
isTagged(): boolean;
isOptional(): boolean;
matchesNamedTag(name: MetadataName): boolean;
matchesTag(key: MetadataTag): (value: unknown) => boolean;
}
Loading