Files
privacy.sexy/tests/unit/infrastructure/RuntimeSanity/Validators/FactoryValidatorConcreteTestRunner.ts
undergroundwires 949fac1a7c Refactor to enforce strictNullChecks
This commit applies `strictNullChecks` to the entire codebase to improve
maintainability and type safety. Key changes include:

- Remove some explicit null-checks where unnecessary.
- Add necessary null-checks.
- Refactor static factory functions for a more functional approach.
- Improve some test names and contexts for better debugging.
- Add unit tests for any additional logic introduced.
- Refactor `createPositionFromRegexFullMatch` to its own function as the
  logic is reused.
- Prefer `find` prefix on functions that may return `undefined` and
  `get` prefix for those that always return a value.
2023-11-12 22:54:00 +01:00

58 lines
2.1 KiB
TypeScript

import { PropertyKeys } from '@/TypeHelpers';
import { FactoryFunction, FactoryValidator } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
import { ISanityCheckOptions } from '@/infrastructure/RuntimeSanity/Common/ISanityCheckOptions';
import { SanityCheckOptionsStub } from '@tests/unit/shared/Stubs/SanityCheckOptionsStub';
interface ITestOptions<T> {
createValidator: (factory?: FactoryFunction<T>) => FactoryValidator<T>;
enablingOptionProperty: PropertyKeys<ISanityCheckOptions>;
factoryFunctionStub: FactoryFunction<T>;
expectedValidatorName: string;
}
export function runFactoryValidatorTests<T>(
testOptions: ITestOptions<T>,
) {
describe('shouldValidate', () => {
it('returns true when option is true', () => {
// arrange
const expectedValue = true;
const options: ISanityCheckOptions = {
...new SanityCheckOptionsStub(),
[testOptions.enablingOptionProperty]: true,
};
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
// act
const actualValue = validatorUnderTest.shouldValidate(options);
// assert
expect(actualValue).to.equal(expectedValue);
});
it('returns false when option is false', () => {
// arrange
const expectedValue = false;
const options: ISanityCheckOptions = {
...new SanityCheckOptionsStub(),
[testOptions.enablingOptionProperty]: false,
};
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
// act
const actualValue = validatorUnderTest.shouldValidate(options);
// assert
expect(actualValue).to.equal(expectedValue);
});
});
describe('name', () => {
it('returns as expected', () => {
// arrange
const expectedName = testOptions.expectedValidatorName;
// act
const validatorUnderTest = testOptions.createValidator(testOptions.factoryFunctionStub);
// assert
const actualName = validatorUnderTest.name;
expect(actualName).to.equal(expectedName);
});
});
}