Files
privacy.sexy/tests/unit/presentation/bootstrapping/Modules/RuntimeSanityValidator.spec.ts
undergroundwires 7770a9b521 Refactor DI for simplicity and type safety
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.
2023-11-09 13:17:38 +01:00

45 lines
1.6 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
import { RuntimeSanityValidator } from '@/presentation/bootstrapping/Modules/RuntimeSanityValidator';
import { expectDoesNotThrowAsync, expectThrowsAsync } from '@tests/shared/Assertions/ExpectThrowsAsync';
describe('RuntimeSanityValidator', () => {
it('calls validator with correct options upon bootstrap', async () => {
// arrange
const expectedOptions: ISanityCheckOptions = {
validateEnvironmentVariables: true,
validateWindowVariables: true,
};
let actualOptions: ISanityCheckOptions;
const validatorMock = (options) => {
actualOptions = options;
};
const sut = new RuntimeSanityValidator(validatorMock);
// act
await sut.bootstrap();
// assert
expect(actualOptions).to.deep.equal(expectedOptions);
});
it('propagates the error if validator fails', async () => {
// arrange
const expectedMessage = 'message thrown from validator';
const validatorMock = () => {
throw new Error(expectedMessage);
};
const sut = new RuntimeSanityValidator(validatorMock);
// act
const act = async () => { await sut.bootstrap(); };
// assert
await expectThrowsAsync(act, expectedMessage);
});
it('runs successfully if validator passes', async () => {
// arrange
const validatorMock = () => { /* NOOP */ };
const sut = new RuntimeSanityValidator(validatorMock);
// act
const act = async () => { await sut.bootstrap(); };
// assert
await expectDoesNotThrowAsync(act);
});
});