-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from zgid123/core-feat/build-simple-ioc
[core]: build simple ioc container for di
- Loading branch information
Showing
14 changed files
with
661 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
node_modules | ||
.turbo | ||
lib | ||
dist | ||
docs/.vitepress/cache | ||
docs/.vitepress/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
packages/@core/src/__tests__/decorators/injection.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { describe, expect, it, suite, afterEach } from 'vitest'; | ||
|
||
import { IoC } from '../../core/IoC'; | ||
import { INJECT, INJECTABLE } from '../../constants/metadata'; | ||
import { Inject, Injectable } from '../../decorators/injection'; | ||
|
||
describe('[decorators]: injection', () => { | ||
suite('Injectable', () => { | ||
afterEach(() => { | ||
Reflect.deleteMetadata(INJECTABLE, IoC); | ||
}); | ||
|
||
suite('without scope', () => { | ||
it('sets metadata for MyService class', () => { | ||
@Injectable() | ||
class MyService {} | ||
|
||
expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([ | ||
{ | ||
scope: 'singleton', | ||
target: MyService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
suite('with scope scoped', () => { | ||
it('sets metadata for MyService class', () => { | ||
@Injectable({ | ||
scope: 'scoped', | ||
}) | ||
class MyService {} | ||
|
||
expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([ | ||
{ | ||
scope: 'scoped', | ||
target: MyService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
suite('with scope singleton', () => { | ||
it('sets metadata for MyService class', () => { | ||
@Injectable({ | ||
scope: 'singleton', | ||
}) | ||
class MyService {} | ||
|
||
expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([ | ||
{ | ||
scope: 'singleton', | ||
target: MyService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
suite('with scope transient', () => { | ||
it('sets metadata for MyService class', () => { | ||
@Injectable({ | ||
scope: 'transient', | ||
}) | ||
class MyService {} | ||
|
||
expect(Reflect.getMetadata(INJECTABLE, IoC)).toStrictEqual([ | ||
{ | ||
scope: 'transient', | ||
target: MyService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); | ||
|
||
suite('Inject', () => { | ||
class InjectService {} | ||
|
||
class AnotherInjectService {} | ||
|
||
suite('with one param', () => { | ||
it("sets metadata for MyService class's constructor", () => { | ||
class MyService { | ||
constructor( | ||
@Inject(InjectService) | ||
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation> | ||
private readonly injectService: InjectService, | ||
) {} | ||
} | ||
|
||
expect( | ||
Reflect.getMetadata(INJECT, MyService, 'constructor'), | ||
).toStrictEqual([ | ||
{ | ||
extractor: InjectService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
suite('with two params', () => { | ||
it("sets metadata for MyService class's constructor", () => { | ||
class MyService { | ||
constructor( | ||
@Inject(InjectService) | ||
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation> | ||
private readonly injectService: InjectService, | ||
@Inject(AnotherInjectService) | ||
// biome-ignore lint/correctness/noUnusedPrivateClassMembers: <explanation> | ||
private readonly anotherInjectService: AnotherInjectService, | ||
) {} | ||
} | ||
|
||
expect( | ||
Reflect.getMetadata(INJECT, MyService, 'constructor'), | ||
).toStrictEqual([ | ||
{ | ||
extractor: InjectService, | ||
}, | ||
{ | ||
extractor: AnotherInjectService, | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.