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.
This commit is contained in:
undergroundwires
2023-11-12 22:54:00 +01:00
parent 7ab16ecccb
commit 949fac1a7c
294 changed files with 2477 additions and 2738 deletions

View File

@@ -7,7 +7,6 @@ import { IApplication } from '@/domain/IApplication';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
import { ApplicationStub } from '@tests/unit/shared/Stubs/ApplicationStub';
import { CategoryCollectionStub } from '@tests/unit/shared/Stubs/CategoryCollectionStub';
import { expectThrowsAsync } from '@tests/shared/Assertions/ExpectThrowsAsync';
describe('ApplicationContextFactory', () => {
describe('buildContext', () => {
@@ -23,19 +22,10 @@ describe('ApplicationContextFactory', () => {
// assert
expect(expected).to.equal(context.app);
});
it('throws when null', async () => {
// arrange
const expectedError = 'missing factory';
const factory = null;
// act
const act = async () => { await buildContext(factory); };
// assert
expectThrowsAsync(act, expectedError);
});
});
describe('environment', () => {
describe('sets initial OS as expected', () => {
it('returns currentOs if it is supported', async () => {
it('returns current OS if it is supported', async () => {
// arrange
const expected = OperatingSystem.Windows;
const environment = new RuntimeEnvironmentStub().withOs(expected);
@@ -47,7 +37,7 @@ describe('ApplicationContextFactory', () => {
const actual = context.state.os;
expect(expected).to.equal(actual);
});
it('fallbacks to other os if OS in environment is not supported', async () => {
it('fallbacks to other OS if OS in environment is not supported', async () => {
// arrange
const expected = OperatingSystem.Windows;
const currentOs = OperatingSystem.macOS;
@@ -60,15 +50,34 @@ describe('ApplicationContextFactory', () => {
const actual = context.state.os;
expect(expected).to.equal(actual);
});
it('fallbacks to most supported os if current os is not supported', async () => {
it('fallbacks to most supported OS if current OS is not supported', async () => {
// arrange
const runtimeOs = OperatingSystem.macOS;
const expectedOs = OperatingSystem.Android;
const allCollections = [
new CategoryCollectionStub().withOs(OperatingSystem.Linux).withTotalScripts(3),
new CategoryCollectionStub().withOs(expectedOs).withTotalScripts(5),
new CategoryCollectionStub().withOs(OperatingSystem.Windows).withTotalScripts(4),
];
const environment = new RuntimeEnvironmentStub().withOs(OperatingSystem.macOS);
const environment = new RuntimeEnvironmentStub().withOs(runtimeOs);
const app = new ApplicationStub().withCollections(...allCollections);
const factoryMock = mockFactoryWithApp(app);
// act
const context = await buildContext(factoryMock, environment);
// assert
const actual = context.state.os;
expect(expectedOs).to.equal(actual, `Expected: ${OperatingSystem[expectedOs]}, actual: ${OperatingSystem[actual]}`);
});
it('fallbacks to most supported OS if current OS is undefined', async () => {
// arrange
const runtimeOs = OperatingSystem.macOS;
const expectedOs = OperatingSystem.Android;
const allCollections = [
new CategoryCollectionStub().withOs(OperatingSystem.Linux).withTotalScripts(3),
new CategoryCollectionStub().withOs(expectedOs).withTotalScripts(5),
new CategoryCollectionStub().withOs(OperatingSystem.Windows).withTotalScripts(4),
];
const environment = new RuntimeEnvironmentStub().withOs(runtimeOs);
const app = new ApplicationStub().withCollections(...allCollections);
const factoryMock = mockFactoryWithApp(app);
// act
@@ -77,16 +86,6 @@ describe('ApplicationContextFactory', () => {
const actual = context.state.os;
expect(expectedOs).to.equal(actual, `Expected: ${OperatingSystem[expectedOs]}, actual: ${OperatingSystem[actual]}`);
});
});
it('throws when null', async () => {
// arrange
const expectedError = 'missing environment';
const factory = mockFactoryWithApp(undefined);
const environment = null;
// act
const act = async () => { await buildContext(factory, environment); };
// assert
expectThrowsAsync(act, expectedError);
});
});
});