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

107 lines
3.2 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import {
getEnumNames, getEnumValues, createEnumParser, assertInRange,
} from '@/application/Common/Enum';
import { scrambledEqual } from '@/application/Common/Array';
import { AbsentStringTestCases } from '@tests/unit/shared/TestCases/AbsentTests';
import { EnumRangeTestRunner } from './EnumRangeTestRunner';
describe('Enum', () => {
describe('createEnumParser', () => {
enum ParsableEnum { Value1, value2 }
describe('parses as expected', () => {
// arrange
const testCases = [
{
name: 'case insensitive',
value: 'vALuE1',
expected: ParsableEnum.Value1,
},
{
name: 'exact match',
value: 'value2',
expected: ParsableEnum.value2,
},
];
// act
for (const testCase of testCases) {
it(testCase.name, () => {
const parser = createEnumParser(ParsableEnum);
const actual = parser.parseEnum(testCase.value, 'non-important');
// assert
expect(actual).to.equal(testCase.expected);
});
}
});
describe('throws as expected', () => {
// arrange
const enumName = 'ParsableEnum';
const testCases = [
...AbsentStringTestCases.map((test) => ({
name: test.valueName,
value: test.absentValue,
expectedError: `missing ${enumName}`,
})),
{
name: 'out of range',
value: 'value3',
expectedError: `unknown ${enumName}: "value3"`,
},
{
name: 'out of range',
value: 'value3',
expectedError: `unknown ${enumName}: "value3"`,
},
{
name: 'unexpected type',
value: 55 as never,
expectedError: `unexpected type of ${enumName}: "number"`,
},
];
// act
for (const testCase of testCases) {
it(testCase.name, () => {
const parser = createEnumParser(ParsableEnum);
const act = () => parser.parseEnum(testCase.value, enumName);
// assert
expect(act).to.throw(testCase.expectedError);
});
}
});
});
describe('getEnumNames', () => {
it('parses as expected', () => {
// arrange
enum TestEnum { TestValue1, testValue2, testvalue3, TESTVALUE4 }
const expected = ['TestValue1', 'testValue2', 'testvalue3', 'TESTVALUE4'];
// act
const actual = getEnumNames(TestEnum);
// assert
expect(scrambledEqual(expected, actual));
});
});
describe('getEnumValues', () => {
it('parses as expected', () => {
// arrange
enum TestEnum { Red, Green, Blue }
const expected = [TestEnum.Red, TestEnum.Green, TestEnum.Blue];
// act
const actual = getEnumValues(TestEnum);
// assert
expect(scrambledEqual(expected, actual));
});
});
describe('assertInRange', () => {
// arrange
enum TestEnum { Red, Green, Blue }
const validValue = TestEnum.Red;
// act
const act = (value: TestEnum) => assertInRange(value, TestEnum);
// assert
new EnumRangeTestRunner(act)
.testOutOfRangeThrows()
.testAbsentValueThrows()
.testValidValueDoesNotThrow(validValue);
});
});