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

@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest';
import { BrowserClipboard, NavigatorClipboard } from '@/presentation/components/Shared/Hooks/Clipboard/BrowserClipboard';
import { StubWithObservableMethodCalls } from '@tests/unit/shared/Stubs/StubWithObservableMethodCalls';
import { expectThrowsAsync } from '@tests/shared/Assertions/ExpectThrowsAsync';
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
describe('BrowserClipboard', () => {
describe('writeText', () => {
@@ -16,7 +17,7 @@ describe('BrowserClipboard', () => {
const calls = navigatorClipboard.callHistory;
expect(calls).to.have.lengthOf(1);
const call = calls.find((c) => c.methodName === 'writeText');
expect(call).toBeDefined();
expectExists(call);
const [actualText] = call.args;
expect(actualText).to.equal(expectedText);
});

View File

@@ -3,6 +3,7 @@ import { useClipboard } from '@/presentation/components/Shared/Hooks/Clipboard/U
import { BrowserClipboard } from '@/presentation/components/Shared/Hooks/Clipboard/BrowserClipboard';
import { ClipboardStub } from '@tests/unit/shared/Stubs/ClipboardStub';
import { FunctionKeys } from '@/TypeHelpers';
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
describe('useClipboard', () => {
it(`returns an instance of ${BrowserClipboard.name}`, () => {
@@ -41,7 +42,7 @@ describe('useClipboard', () => {
// assert
testFunction(...expectedArgs);
const call = clipboardStub.callHistory.find((c) => c.methodName === functionName);
expect(call).toBeDefined();
expectExists(call);
expect(call.args).to.deep.equal(expectedArgs);
});
it('ensures method retains the clipboard instance context', () => {

View File

@@ -2,21 +2,8 @@ import { describe, it, expect } from 'vitest';
import { useApplication } from '@/presentation/components/Shared/Hooks/UseApplication';
import { ApplicationStub } from '@tests/unit/shared/Stubs/ApplicationStub';
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('UseApplication', () => {
describe('application is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing application';
const applicationValue = absentValue;
// act
const act = () => useApplication(applicationValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('should return expected info', () => {
// arrange
const expectedInfo = new ProjectInformationStub()

View File

@@ -5,41 +5,11 @@ import { ApplicationContextStub } from '@tests/unit/shared/Stubs/ApplicationCont
import { IReadOnlyCategoryCollectionState } from '@/application/Context/State/ICategoryCollectionState';
import { ApplicationContextChangedEventStub } from '@tests/unit/shared/Stubs/ApplicationContextChangedEventStub';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { IApplicationContext } from '@/application/Context/IApplicationContext';
import { IEventSubscriptionCollection } from '@/infrastructure/Events/IEventSubscriptionCollection';
import { EventSubscriptionCollectionStub } from '@tests/unit/shared/Stubs/EventSubscriptionCollectionStub';
describe('UseCollectionState', () => {
describe('parameter validation', () => {
describe('absent context', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing context';
const contextValue = absentValue;
// act
const act = () => new UseCollectionStateBuilder()
.withContext(contextValue)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
describe('absent events', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing events';
const eventsValue = absentValue;
// act
const act = () => new UseCollectionStateBuilder()
.withEvents(eventsValue)
.build();
// assert
expect(act).to.throw(expectedError);
});
});
});
describe('listens to contextChanged event', () => {
it('registers new event listener', () => {
// arrange
@@ -67,12 +37,13 @@ describe('UseCollectionState', () => {
.withEvents(events)
.build();
const stateModifierEvent = events.mostRecentSubscription;
stateModifierEvent.unsubscribe();
stateModifierEvent?.unsubscribe();
context.dispatchContextChange(
new ApplicationContextChangedEventStub().withNewState(newState),
);
// assert
expect(stateModifierEvent).toBeDefined();
expect(currentState.value).to.equal(oldState);
});
});
@@ -126,17 +97,6 @@ describe('UseCollectionState', () => {
});
describe('onStateChange', () => {
describe('throws when callback is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing state handler';
const { onStateChange } = new UseCollectionStateBuilder().build();
// act
const act = () => onStateChange(absentValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('call handler when context state changes', () => {
// arrange
const expected = true;
@@ -206,7 +166,7 @@ describe('UseCollectionState', () => {
it('call handler with new state after state changes', () => {
// arrange
const expected = new CategoryCollectionStateStub();
let actual: IReadOnlyCategoryCollectionState;
let actual: IReadOnlyCategoryCollectionState | undefined;
const context = new ApplicationContextStub();
const { onStateChange } = new UseCollectionStateBuilder()
.withContext(context)
@@ -226,7 +186,7 @@ describe('UseCollectionState', () => {
it('call handler with old state after state changes', () => {
// arrange
const expectedState = new CategoryCollectionStateStub();
let actualState: IReadOnlyCategoryCollectionState;
let actualState: IReadOnlyCategoryCollectionState | undefined;
const context = new ApplicationContextStub();
const { onStateChange } = new UseCollectionStateBuilder()
.withContext(context)
@@ -271,31 +231,16 @@ describe('UseCollectionState', () => {
// act
onStateChange(callback);
const stateChangeEvent = events.mostRecentSubscription;
stateChangeEvent.unsubscribe();
stateChangeEvent?.unsubscribe();
context.dispatchContextChange();
// assert
expect(stateChangeEvent).toBeDefined();
expect(isCallbackCalled).to.equal(false);
});
});
});
describe('modifyCurrentState', () => {
describe('throws when callback is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing state mutator';
const context = new ApplicationContextStub();
const { modifyCurrentState } = new UseCollectionStateBuilder()
.withContext(context)
.build();
// act
const act = () => modifyCurrentState(absentValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('modifies current collection state', () => {
// arrange
const oldOs = OperatingSystem.Windows;
@@ -321,17 +266,6 @@ describe('UseCollectionState', () => {
});
describe('modifyCurrentContext', () => {
describe('throws when callback is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing context mutator';
const { modifyCurrentContext } = new UseCollectionStateBuilder().build();
// act
const act = () => modifyCurrentContext(absentValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('modifies the current context', () => {
// arrange
const oldState = new CategoryCollectionStateStub()

View File

@@ -1,21 +1,8 @@
import { describe, it, expect } from 'vitest';
import { useRuntimeEnvironment } from '@/presentation/components/Shared/Hooks/UseRuntimeEnvironment';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { RuntimeEnvironmentStub } from '@tests/unit/shared/Stubs/RuntimeEnvironmentStub';
describe('UseRuntimeEnvironment', () => {
describe('environment is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing environment';
const environmentValue = absentValue;
// act
const act = () => useRuntimeEnvironment(environmentValue);
// assert
expect(act).to.throw(expectedError);
});
});
it('returns expected environment', () => {
// arrange
const expectedEnvironment = new RuntimeEnvironmentStub();

View File

@@ -1,24 +1,12 @@
import { describe, it, expect } from 'vitest';
import { throttle, ITimer, TimeoutType } from '@/presentation/components/Shared/Throttle';
import { throttle, ITimer, Timeout } from '@/presentation/components/Shared/Throttle';
import { EventSource } from '@/infrastructure/Events/EventSource';
import { IEventSubscription } from '@/infrastructure/Events/IEventSource';
import { getAbsentObjectTestCases, itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
import { createMockTimeout } from '@tests/unit/shared/Stubs/TimeoutStub';
describe('throttle', () => {
describe('validates parameters', () => {
describe('throws if callback is absent', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing callback';
const callback = absentValue;
// act
const act = () => throttle(callback, 500);
// assert
expect(act).to.throw(expectedError);
});
});
describe('throws if waitInMs is negative or zero', () => {
describe('throws if waitInMs is invalid', () => {
// arrange
const testCases = [
{
@@ -31,11 +19,6 @@ describe('throttle', () => {
value: -2,
expectedError: 'negative delay',
},
...getAbsentObjectTestCases().map((testCase) => ({
name: `when absent (given ${testCase.valueName})`,
value: testCase.absentValue,
expectedError: 'missing delay',
})),
];
const noopCallback = () => { /* do nothing */ };
for (const testCase of testCases) {
@@ -48,17 +31,6 @@ describe('throttle', () => {
});
}
});
it('throws if timer is null', () => {
// arrange
const expectedError = 'missing timer';
const timer = null;
const noopCallback = () => { /* do nothing */ };
const waitInMs = 1;
// act
const act = () => throttle(noopCallback, waitInMs, timer);
// assert
expect(act).to.throw(expectedError);
});
});
it('should call the callback immediately', () => {
// arrange
@@ -144,7 +116,7 @@ class TimerMock implements ITimer {
private currentTime = 0;
public setTimeout(callback: () => void, ms: number): TimeoutType {
public setTimeout(callback: () => void, ms: number): Timeout {
const runTime = this.currentTime + ms;
const subscription = this.timeChanged.on((time) => {
if (time >= runTime) {
@@ -157,7 +129,7 @@ class TimerMock implements ITimer {
return createMockTimeout(id);
}
public clearTimeout(timeoutId: TimeoutType): void {
public clearTimeout(timeoutId: Timeout): void {
this.subscriptions[+timeoutId].unsubscribe();
}