Files
privacy.sexy/tests/unit/application/Common/ScriptingLanguage/ScriptingLanguageFactoryTestRunner.ts
undergroundwires 5f11c8d98f Migrate unit/integration tests to Vitest with Vite
As part of transition to Vue 3.0 and Vite (#230), this commit
facilitates the shift towards building rest of the application using
Vite. By doing so, it eliminates reliance on outdated Electron building
system that offered limited control, blocking desktop builds (#233).

Changes include:

- Introduce Vite with Vue 2.0 plugin for test execution.
- Remove `mocha`, `chai` and other related dependencies.
- Adjust test to Vitest syntax.
- Revise and update `tests.md` to document the changes.
- Add `@modyfi/vite-plugin-yaml` plugin to be able to use yaml file
  depended logic on test files, replacing previous webpack behavior.
- Fix failing tests that are revealed by Vitest due to unhandled errors
  and lack of assertments.
- Remove the test that depends on Vue CLI populating `process.env`.
- Use `jsdom` for unit test environment, adding it to dependency to
  `package.json` as project now depends on it and it was not specified
  even though `package-lock.json` included it.
2023-08-22 14:02:35 +02:00

90 lines
2.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { IScriptingLanguageFactory } from '@/application/Common/ScriptingLanguage/IScriptingLanguageFactory';
import { ScriptingLanguage } from '@/domain/ScriptingLanguage';
import { EnumRangeTestRunner } from '@tests/unit/application/Common/EnumRangeTestRunner';
type Constructible<T = object> = new (...args: unknown[]) => T;
export class ScriptingLanguageFactoryTestRunner<T> {
private expectedLanguageTypes = new Map<ScriptingLanguage, Constructible<T>>();
private expectedValues = new Map<ScriptingLanguage, T>();
public expectInstance(language: ScriptingLanguage, resultType: Constructible<T>) {
this.expectedLanguageTypes.set(language, resultType);
return this;
}
public expectValue(language: ScriptingLanguage, resultType: T) {
this.expectedValues.set(language, resultType);
return this;
}
public testCreateMethod(sut: IScriptingLanguageFactory<T>) {
if (!sut) { throw new Error('missing sut'); }
testLanguageValidation(sut);
if (this.expectedLanguageTypes.size) {
testExpectedInstanceTypes(sut, this.expectedLanguageTypes);
}
if (this.expectedValues.size) {
testExpectedValues(sut, this.expectedValues);
}
}
}
function testExpectedInstanceTypes<T>(
sut: IScriptingLanguageFactory<T>,
expectedTypes: Map<ScriptingLanguage, Constructible<T>>,
) {
if (!expectedTypes?.size) {
throw new Error('No expected types provided.');
}
describe('`create` creates expected instances', () => {
// arrange
for (const language of expectedTypes.keys()) {
it(ScriptingLanguage[language], () => {
// act
const expected = expectedTypes.get(language);
const result = sut.create(language);
// assert
expect(result).to.be.instanceOf(expected, `Actual was: ${result.constructor.name}`);
});
}
});
}
function testExpectedValues<T>(
sut: IScriptingLanguageFactory<T>,
expectedValues: Map<ScriptingLanguage, T>,
) {
if (!expectedValues?.size) {
throw new Error('No expected values provided.');
}
describe('`create` creates expected values', () => {
// arrange
for (const language of expectedValues.keys()) {
it(ScriptingLanguage[language], () => {
// act
const expected = expectedValues.get(language);
const result = sut.create(language);
// assert
expect(result).to.equal(expected);
});
}
});
}
function testLanguageValidation<T>(sut: IScriptingLanguageFactory<T>) {
describe('`create` validates language selection', () => {
// arrange
const validValue = ScriptingLanguage.batchfile;
// act
const act = (value: ScriptingLanguage) => sut.create(value);
// assert
new EnumRangeTestRunner(act)
.testOutOfRangeThrows()
.testAbsentValueThrows()
.testValidValueDoesNotThrow(validValue);
});
}