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

140 lines
6.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { IEntity } from '@/infrastructure/Entity/IEntity';
import { parseCategoryCollection } from '@/application/Parser/CategoryCollectionParser';
import { parseCategory } from '@/application/Parser/CategoryParser';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { RecommendationLevel } from '@/domain/RecommendationLevel';
import { ScriptingDefinitionParser } from '@/application/Parser/ScriptingDefinition/ScriptingDefinitionParser';
import { EnumParserStub } from '@tests/unit/shared/Stubs/EnumParserStub';
import { ProjectInformationStub } from '@tests/unit/shared/Stubs/ProjectInformationStub';
import { getCategoryStub, CollectionDataStub } from '@tests/unit/shared/Stubs/CollectionDataStub';
import { CategoryCollectionParseContextStub } from '@tests/unit/shared/Stubs/CategoryCollectionParseContextStub';
import { CategoryDataStub } from '@tests/unit/shared/Stubs/CategoryDataStub';
import { ScriptDataStub } from '@tests/unit/shared/Stubs/ScriptDataStub';
import { FunctionDataStub } from '@tests/unit/shared/Stubs/FunctionDataStub';
import { FunctionCallDataStub } from '@tests/unit/shared/Stubs/FunctionCallDataStub';
import { itEachAbsentObjectValue } from '@tests/unit/shared/TestCases/AbsentTests';
describe('CategoryCollectionParser', () => {
describe('parseCategoryCollection', () => {
describe('throws with absent content', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'missing content';
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(absentValue, info);
// assert
expect(act).to.throw(expectedError);
});
});
describe('actions', () => {
describe('throws with absent actions', () => {
itEachAbsentObjectValue((absentValue) => {
// arrange
const expectedError = 'content does not define any action';
const collection = new CollectionDataStub()
.withActions(absentValue);
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(collection, info);
// assert
expect(act).to.throw(expectedError);
});
});
it('throws when has no actions', () => {
// arrange
const expectedError = 'content does not define any action';
const collection = new CollectionDataStub()
.withActions([]);
const info = new ProjectInformationStub();
// act
const act = () => parseCategoryCollection(collection, info);
// assert
expect(act).to.throw(expectedError);
});
it('parses actions', () => {
// arrange
const actions = [getCategoryStub('test1'), getCategoryStub('test2')];
const context = new CategoryCollectionParseContextStub();
const expected = [parseCategory(actions[0], context), parseCategory(actions[1], context)];
const collection = new CollectionDataStub()
.withActions(actions);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info).actions;
// assert
expect(excludingId(actual)).to.be.deep.equal(excludingId(expected));
function excludingId<TId>(array: ReadonlyArray<IEntity<TId>>) {
return array.map((obj) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { id: omitted, ...rest } = obj;
return rest;
});
}
});
});
describe('scripting definition', () => {
it('parses scripting definition as expected', () => {
// arrange
const collection = new CollectionDataStub();
const information = new ProjectInformationStub();
const expected = new ScriptingDefinitionParser()
.parse(collection.scripting, information);
// act
const actual = parseCategoryCollection(collection, information).scripting;
// assert
expect(expected).to.deep.equal(actual);
});
});
describe('os', () => {
it('parses as expected', () => {
// arrange
const expectedOs = OperatingSystem.macOS;
const osText = 'macos';
const expectedName = 'os';
const collection = new CollectionDataStub()
.withOs(osText);
const parserMock = new EnumParserStub<OperatingSystem>()
.setup(expectedName, osText, expectedOs);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info, parserMock);
// assert
expect(actual.os).to.equal(expectedOs);
});
});
describe('functions', () => {
it('compiles script call with given function', () => {
// arrange
const expectedCode = 'code-from-the-function';
const functionName = 'function-name';
const scriptName = 'script-name';
const script = ScriptDataStub.createWithCall()
.withCall(new FunctionCallDataStub().withName(functionName).withParameters({}))
.withName(scriptName);
const func = FunctionDataStub.createWithCode().withParametersObject([])
.withName(functionName)
.withCode(expectedCode);
const category = new CategoryDataStub()
.withChildren([script,
ScriptDataStub.createWithCode().withName('2')
.withRecommendationLevel(RecommendationLevel.Standard),
ScriptDataStub.createWithCode()
.withName('3').withRecommendationLevel(RecommendationLevel.Strict),
]);
const collection = new CollectionDataStub()
.withActions([category])
.withFunctions([func]);
const info = new ProjectInformationStub();
// act
const actual = parseCategoryCollection(collection, info);
// assert
const actualScript = actual.findScript(scriptName);
const actualCode = actualScript.code.execute;
expect(actualCode).to.equal(expectedCode);
});
});
});
});