Files
privacy.sexy/tests/unit/application/Parser/ScriptingDefinition/CodeSubstituter.spec.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

111 lines
3.6 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { CodeSubstituter } from '@/application/Parser/ScriptingDefinition/CodeSubstituter';
import { IExpressionsCompiler } from '@/application/Parser/Script/Compiler/Expressions/IExpressionsCompiler';
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
import { ExpressionsCompilerStub } from '@tests/unit/shared/Stubs/ExpressionsCompilerStub';
import { AbsentObjectTestCases, AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CodeSubstituter', () => {
describe('throws with invalid parameters', () => {
// arrange
const testCases = [
...AbsentStringTestCases.map((testCase) => ({
name: `given code: ${testCase.valueName}`,
expectedError: 'missing code',
parameters: {
code: testCase.absentValue,
info: new ProjectInformationStub(),
},
})),
...AbsentObjectTestCases.map((testCase) => ({
name: `given info: ${testCase.valueName}`,
expectedError: 'missing info',
parameters: {
code: 'non empty code',
info: testCase.absentValue,
},
})),
];
for (const testCase of testCases) {
it(`${testCase.name} throws "${testCase.expectedError}"`, () => {
// arrange
const sut = new CodeSubstituterBuilder().build();
const { code, info } = testCase.parameters;
// act
const act = () => sut.substitute(code, info);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
describe('substitutes parameters as expected values', () => {
// arrange
const info = new ProjectInformationStub();
const date = new Date();
const testCases: Array<{ parameter: string, argument: string }> = [
{
parameter: 'homepage',
argument: info.homepage,
},
{
parameter: 'version',
argument: info.version.toString(),
},
{
parameter: 'date',
argument: date.toUTCString(),
},
];
for (const testCase of testCases) {
it(`substitutes ${testCase.parameter} as expected`, () => {
const compilerStub = new ExpressionsCompilerStub();
const sut = new CodeSubstituterBuilder()
.withCompiler(compilerStub)
.withDate(date)
.build();
// act
sut.substitute('non empty code', info);
// assert
expect(compilerStub.callHistory).to.have.lengthOf(1);
const { parameters } = compilerStub.callHistory[0];
expect(parameters.hasArgument(testCase.parameter));
const { argumentValue } = parameters.getArgument(testCase.parameter);
expect(argumentValue).to.equal(testCase.argument);
});
}
});
it('returns code as it is', () => {
// arrange
const expected = 'expected-code';
const compilerStub = new ExpressionsCompilerStub();
const sut = new CodeSubstituterBuilder()
.withCompiler(compilerStub)
.build();
// act
sut.substitute(expected, new ProjectInformationStub());
// assert
expect(compilerStub.callHistory).to.have.lengthOf(1);
expect(compilerStub.callHistory[0].code).to.equal(expected);
});
});
class CodeSubstituterBuilder {
private compiler: IExpressionsCompiler = new ExpressionsCompilerStub();
private date = new Date();
public withCompiler(compiler: IExpressionsCompiler) {
this.compiler = compiler;
return this;
}
public withDate(date: Date) {
this.date = date;
return this;
}
public build() {
return new CodeSubstituter(this.compiler, this.date);
}
}