Files
privacy.sexy/tests/unit/infrastructure/RuntimeSanity/Common/FactoryValidator.spec.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

103 lines
3.1 KiB
TypeScript

import { describe } from 'vitest';
import { FactoryValidator, FactoryFunction } from '@/infrastructure/RuntimeSanity/Common/FactoryValidator';
describe('FactoryValidator', () => {
describe('collectErrors', () => {
it('reports error thrown by factory function', () => {
// arrange
const errorFromFactory = 'Error from factory function';
const expectedError = `Error in factory creation: ${errorFromFactory}`;
const factory: FactoryFunction<number | undefined> = () => {
throw new Error(errorFromFactory);
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(1);
expect(errors[0]).to.equal(expectedError);
});
describe('reports when factory returns falsy values', () => {
const falsyValueTestCases = [
{
name: '`false` boolean',
value: false,
},
{
name: 'number zero',
value: 0,
},
{
name: 'empty string',
value: '',
},
{
name: 'null',
value: null,
},
{
name: 'undefined',
value: undefined,
},
{
name: 'NaN (Not-a-Number)',
value: Number.NaN,
},
];
falsyValueTestCases.forEach(({ name, value }) => {
it(`reports for value: ${name}`, () => {
// arrange
const errorFromFactory = 'Factory resulted in a falsy value';
const factory: FactoryFunction<number | undefined> = () => {
return value as never;
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(1);
expect(errors[0]).to.equal(errorFromFactory);
});
});
});
it('does not report when factory returns a truthy value', () => {
// arrange
const factory: FactoryFunction<number | undefined> = () => {
return 35;
};
const sut = new TestableFactoryValidator(factory);
// act
const errors = [...sut.collectErrors()];
// assert
expect(errors).to.have.lengthOf(0);
});
it('executes factory for each method call', () => {
// arrange
let forceFalsyValue = false;
const complexFactory: FactoryFunction<number | undefined> = () => {
return forceFalsyValue ? undefined : 42;
};
const sut = new TestableFactoryValidator(complexFactory);
// act
const firstErrors = [...sut.collectErrors()];
forceFalsyValue = true;
const secondErrors = [...sut.collectErrors()];
// assert
expect(firstErrors).to.have.lengthOf(0);
expect(secondErrors).to.have.lengthOf(1);
});
});
});
class TestableFactoryValidator extends FactoryValidator<number | undefined> {
public constructor(factory: FactoryFunction<number | undefined>) {
super(factory);
}
public name = 'test';
public shouldValidate(): boolean {
return true;
}
}