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).
61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
import 'mocha';
|
|
import { expect } from 'chai';
|
|
import { ApplicationFactory, ApplicationGetter } from '@/application/ApplicationFactory';
|
|
import { ApplicationStub } from '@tests/unit/stubs/ApplicationStub';
|
|
|
|
describe('ApplicationFactory', () => {
|
|
describe('ctor', () => {
|
|
it('throws if getter is undefined', () => {
|
|
// arrange
|
|
const expectedError = 'undefined getter';
|
|
const getter = undefined;
|
|
// act
|
|
const act = () => new SystemUnderTest(getter);
|
|
// assert
|
|
expect(act).to.throw(expectedError);
|
|
});
|
|
});
|
|
describe('getApp', () => {
|
|
it('returns result from the getter', async () => {
|
|
// arrange
|
|
const expected = new ApplicationStub();
|
|
const getter: ApplicationGetter = () => expected;
|
|
const sut = new SystemUnderTest(getter);
|
|
// act
|
|
const actual = await Promise.all([
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
]);
|
|
// assert
|
|
expect(actual.every((value) => value === expected));
|
|
});
|
|
it('only executes getter once', async () => {
|
|
// arrange
|
|
let totalExecution = 0;
|
|
const expected = new ApplicationStub();
|
|
const getter: ApplicationGetter = () => {
|
|
totalExecution++;
|
|
return expected;
|
|
};
|
|
const sut = new SystemUnderTest(getter);
|
|
// act
|
|
await Promise.all([
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
sut.getApp(),
|
|
]);
|
|
// assert
|
|
expect(totalExecution).to.equal(1);
|
|
});
|
|
});
|
|
});
|
|
|
|
class SystemUnderTest extends ApplicationFactory {
|
|
public constructor(costlyGetter: ApplicationGetter) {
|
|
super(costlyGetter);
|
|
}
|
|
}
|