Files
privacy.sexy/tests/unit/shared/Stubs/ScriptDataStub.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

83 lines
2.0 KiB
TypeScript

import type {
FunctionCallData, CallScriptData, CodeScriptData,
} from '@/application/collections/';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { FunctionCallDataStub } from '@tests/unit/shared/Stubs/FunctionCallDataStub';
export function createScriptDataWithCode(): ScriptDataStub & CodeScriptData {
return new ScriptDataStub()
.withCode('stub-code')
.withRevertCode('stub-revert-code');
}
export function createScriptDataWithCall(
call?: FunctionCallData,
): ScriptDataStub & CallScriptData {
let instance = new ScriptDataStub();
if (call) {
instance = instance.withCall(call);
} else {
instance = instance.withMockCall();
}
return instance;
}
export function createScriptDataWithoutCallOrCodes(): ScriptDataStub {
return new ScriptDataStub();
}
class ScriptDataStub implements CallScriptData, CodeScriptData {
public name = 'valid-name';
public code: string;
public revertCode: string | undefined = undefined;
public call: FunctionCallData | undefined = undefined;
public recommend:
string | undefined = RecommendationLevel[RecommendationLevel.Standard].toLowerCase();
public docs?: readonly string[] = ['hello.com'];
public withName(name: string): this {
this.name = name;
return this;
}
public withDocs(docs: readonly string[]): this {
this.docs = docs;
return this;
}
public withCode(code: string): this {
this.code = code;
return this;
}
public withRevertCode(revertCode: string | undefined): this {
this.revertCode = revertCode;
return this;
}
public withMockCall(): this {
this.call = new FunctionCallDataStub();
return this;
}
public withCall(call: FunctionCallData | undefined): this {
this.call = call;
return this;
}
public withRecommend(recommend: string | undefined): this {
this.recommend = recommend;
return this;
}
public withRecommendationLevel(level: RecommendationLevel): this {
this.recommend = RecommendationLevel[level].toLowerCase();
return this;
}
}