This commit improves the dependency injection mechanism by introducing a custom `injectKey` function. Key improvements are: - Enforced type consistency during dependency registration and instantiation. - Simplified injection process, abstracting away the complexity with a uniform API, regardless of the dependency's lifetime. - Eliminated the possibility of `undefined` returns during dependency injection, promoting fail-fast behavior. - Removed the necessity for type casting to `symbol` for injection keys in unit tests by using existing types. - Consalidated imports, combining keys and injection functions in one `import` statement.
54 lines
1.6 KiB
TypeScript
54 lines
1.6 KiB
TypeScript
import { it, describe, expect } from 'vitest';
|
|
import { shallowMount } from '@vue/test-utils';
|
|
import { defineComponent, inject } from 'vue';
|
|
import { InjectionKeySelector, InjectionKeys, injectKey } from '@/presentation/injectionSymbols';
|
|
import { provideDependencies } from '@/presentation/bootstrapping/DependencyProvider';
|
|
import { buildContext } from '@/application/Context/ApplicationContextFactory';
|
|
import { IApplicationContext } from '@/application/Context/IApplicationContext';
|
|
|
|
describe('DependencyResolution', () => {
|
|
describe('all dependencies can be injected', async () => {
|
|
// arrange
|
|
const context = await buildContext();
|
|
const dependencies = collectProvidedKeys(context);
|
|
Object.values(InjectionKeys).forEach((key) => {
|
|
it(`"${key.key.description}"`, () => {
|
|
// act
|
|
const resolvedDependency = resolve(() => key, dependencies);
|
|
// assert
|
|
expect(resolvedDependency).to.toBeDefined();
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
type ProvidedKeys = Record<symbol, unknown>;
|
|
|
|
function collectProvidedKeys(context: IApplicationContext): ProvidedKeys {
|
|
const providedKeys: ProvidedKeys = {};
|
|
provideDependencies(context, {
|
|
inject,
|
|
provide: (key, value) => {
|
|
providedKeys[key as symbol] = value;
|
|
},
|
|
});
|
|
return providedKeys;
|
|
}
|
|
|
|
function resolve<T>(
|
|
selector: InjectionKeySelector<T>,
|
|
providedKeys: ProvidedKeys,
|
|
): T | undefined {
|
|
let injectedDependency: T | undefined;
|
|
shallowMount(defineComponent({
|
|
setup() {
|
|
injectedDependency = injectKey(selector);
|
|
},
|
|
}), {
|
|
global: {
|
|
provide: providedKeys,
|
|
},
|
|
});
|
|
return injectedDependency;
|
|
}
|