Files
tools/packages/ioc/test/index.test.ts
2024-06-11 00:58:54 -05:00

134 lines
4.4 KiB
TypeScript

import { Container } from '../src/container';
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
describe('CoreContainer Tests', () => {
let coreContainer: Container;
let singletonWInit: { init: Mock<any, any>; value: string }
beforeEach(() => {
coreContainer = new Container({ autowire: false });
singletonWInit = {
value: 'singletonWithInit',
init: vi.fn()
}
});
it('Adding and getting singletons', () => {
coreContainer.addSingleton('singletonKey', { value: 'singletonValue' });
const singleton = coreContainer.get('singletonKey');
expect(singleton).toEqual({ value: 'singletonValue' });
});
it('Checking if container is ready', () => {
expect(coreContainer.isReady()).toBe(false);
coreContainer.ready().then(() => {
expect(coreContainer.isReady()).toBe(true);
});
});
it('Adding and getting singletons - async', async () => {
await coreContainer.ready();
coreContainer.addSingleton('asyncSingletonKey', { value: 'asyncSingletonValue' });
const singleton = coreContainer.get('asyncSingletonKey');
expect(singleton).toEqual({ value: 'asyncSingletonValue' });
})
it('Registering and executing hooks - init should be called once after ready', async () => {
let initCount = 0;
const singletonWithInit = {
value: 'singletonValueWithInit',
init: async () => {
initCount++;
}
};
coreContainer.addSingleton('singletonKeyWithInit', singletonWithInit);
// Call ready twice to ensure hooks are executed only once
await coreContainer.ready();
await coreContainer.ready();
expect(initCount).toBe(1);
});
it('Registering and executing hooks - ', async () => {
let initCount = 0;
const singletonWithInit = {
value: 'singletonValueWithInit',
init: async () => {
initCount++;
}
};
coreContainer.addSingleton('singletonKeyWithInit', singletonWithInit);
// Call ready twice to ensure hooks are executed only once
await coreContainer.ready();
await coreContainer.ready();
expect(initCount).toBe(1);
});
it('wired singleton', async () => {
let fn = vi.fn()
const wiredSingletonFn = (container: Container) => {
return { value: 'wiredSingletonValue', init: fn };
};
const added = coreContainer.addWiredSingleton('wiredSingletonKey', wiredSingletonFn);
expect(added).toBe(true);
const wiredSingleton = coreContainer.get('wiredSingletonKey');
expect(wiredSingleton).toEqual({ value: 'wiredSingletonValue', init: fn });
await coreContainer.ready()
await coreContainer.ready()
//@ts-ignore
expect(wiredSingleton.init).toHaveBeenCalledOnce();
})
it('dispose', async () => {
let dfn = vi.fn()
const wiredSingletonFn = { value: 'wiredSingletonValue', dispose: dfn };
coreContainer.addSingleton('sk', wiredSingletonFn);
await coreContainer.disposeAll();
expect(dfn).toHaveBeenCalledOnce()
})
it('Checking if container is ready', async () => {
expect(coreContainer.isReady()).toBe(false);
await coreContainer.ready();
expect(coreContainer.isReady()).toBe(true);
});
it('Registering and executing hooks - init should be called once after ready', async () => {
coreContainer.addSingleton('singletonKeyWithInit', singletonWInit);
// Call ready twice to ensure hooks are executed only once
await coreContainer.ready();
await coreContainer.ready();
expect(singletonWInit.init).toHaveBeenCalledOnce();
});
it('should throw because not swapping anything', () => {
expect(() => coreContainer.swap('singletonKeyWithInit', singletonWInit)).toThrow()
})
it('should swap object with another', () => {
coreContainer.addSingleton('singleton', singletonWInit)
const singletonWithInit2 = {
value: 'singletonValueWithInit2',
init: vi.fn()
};
coreContainer.swap('singleton', singletonWithInit2)
expect(coreContainer.get('singleton')).toBe(singletonWithInit2)
})
it('should swap object, calling dispose hook', () => {
coreContainer.addSingleton('singleton', singletonWInit)
})
})