Files
privacy.sexy/tests/unit/application/Common/Enum.spec.ts
undergroundwires 5b1fbe1e2f Refactor code to comply with ESLint rules
Major refactoring using ESLint with rules from AirBnb and Vue.

Enable most of the ESLint rules and do necessary linting in the code.
Also add more information for rules that are disabled to describe what
they are and why they are disabled.

Allow logging (`console.log`) in test files, and in development mode
(e.g. when working with `npm run serve`), but disable it when
environment is production (as pre-configured by Vue). Also add flag
(`--mode production`) in `lint:eslint` command so production linting is
executed earlier in lifecycle.

Disable rules that requires a separate work. Such as ESLint rules that
are broken in TypeScript: no-useless-constructor (eslint/eslint#14118)
and no-shadow (eslint/eslint#13014).
2022-01-02 18:20:14 +01:00

112 lines
3.2 KiB
TypeScript

import 'mocha';
import { expect } from 'chai';
import {
getEnumNames, getEnumValues, createEnumParser, assertInRange,
} from '@/application/Common/Enum';
import { scrambledEqual } from '@/application/Common/Array';
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 = [
{
name: 'undefined',
value: undefined,
expectedError: `undefined ${enumName}`,
},
{
name: 'empty',
value: '',
expectedError: `undefined ${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()
.testUndefinedValueThrows()
.testValidValueDoesNotThrow(validValue);
});
});