website refactor
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { describe, test, expect, vi } from 'vitest';
|
||||
import { createContainer, createTestContainer } from '../container';
|
||||
import { LEAGUE_SERVICE_TOKEN, LOGGER_TOKEN } from '../tokens';
|
||||
import { ContainerProvider } from '../providers/ContainerProvider';
|
||||
@@ -18,32 +19,29 @@ describe('DI System', () => {
|
||||
|
||||
test('createTestContainer allows mocking', async () => {
|
||||
const mockLeagueService = {
|
||||
getAllLeagues: jest.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
|
||||
getAllLeagues: vi.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
|
||||
};
|
||||
|
||||
const overrides = new Map([
|
||||
[LEAGUE_SERVICE_TOKEN, mockLeagueService],
|
||||
]);
|
||||
|
||||
const container = createTestContainer(overrides);
|
||||
|
||||
// Wait for async rebind to complete
|
||||
await new Promise(resolve => setTimeout(resolve, 10));
|
||||
const container = await createTestContainer(overrides);
|
||||
|
||||
const service = container.get(LEAGUE_SERVICE_TOKEN);
|
||||
expect(service.getAllLeagues).toBeDefined();
|
||||
});
|
||||
|
||||
test('useInject hook works with ContainerProvider', () => {
|
||||
test('useInject hook works with ContainerProvider', async () => {
|
||||
const mockLeagueService = {
|
||||
getAllLeagues: jest.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
|
||||
getAllLeagues: vi.fn().mockResolvedValue([{ id: '1', name: 'Test League' }]),
|
||||
};
|
||||
|
||||
const overrides = new Map([
|
||||
[LEAGUE_SERVICE_TOKEN, mockLeagueService],
|
||||
]);
|
||||
|
||||
const container = createTestContainer(overrides);
|
||||
const container = await createTestContainer(overrides);
|
||||
|
||||
const { result } = renderHook(() => useInject(LEAGUE_SERVICE_TOKEN), {
|
||||
wrapper: ({ children }) => (
|
||||
@@ -41,16 +41,16 @@ export function createContainer(): Container {
|
||||
/**
|
||||
* Creates a container for testing with mock overrides
|
||||
*/
|
||||
export function createTestContainer(overrides: Map<symbol, any> = new Map()): Container {
|
||||
export async function createTestContainer(overrides: Map<symbol, any> = new Map()): Promise<Container> {
|
||||
const container = createContainer();
|
||||
|
||||
// Apply mock overrides using rebind
|
||||
Array.from(overrides.entries()).forEach(([token, mockInstance]) => {
|
||||
container.rebind(token).then(bind => bind.toConstantValue(mockInstance));
|
||||
const promises = Array.from(overrides.entries()).map(([token, mockInstance]) => {
|
||||
return container.rebind(token).then(bind => bind.toConstantValue(mockInstance));
|
||||
});
|
||||
|
||||
// Return container immediately, mocks will be available after promises resolve
|
||||
// For synchronous testing, users can bind directly before loading modules
|
||||
await Promise.all(promises);
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user