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).
This commit is contained in:
@@ -6,94 +6,99 @@ import { ProjectInformationStub } from '@tests/unit/stubs/ProjectInformationStub
|
||||
import { ExpressionsCompilerStub } from '@tests/unit/stubs/ExpressionsCompilerStub';
|
||||
|
||||
describe('CodeSubstituter', () => {
|
||||
describe('throws with invalid parameters', () => {
|
||||
// arrange
|
||||
const testCases = [{
|
||||
expectedError: 'undefined code',
|
||||
parameters: {
|
||||
code: undefined,
|
||||
info: new ProjectInformationStub(),
|
||||
}},
|
||||
{
|
||||
expectedError: 'undefined info',
|
||||
parameters: {
|
||||
code: 'non empty code',
|
||||
info: undefined,
|
||||
},
|
||||
}];
|
||||
for (const testCase of testCases) {
|
||||
it(`throws "${testCase.expectedError}" as expected`, () => {
|
||||
const sut = new CodeSubstituterBuilder().build();
|
||||
// act
|
||||
const act = () => sut.substitute(testCase.parameters.code, testCase.parameters.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 = [
|
||||
{
|
||||
parameter: 'homepage',
|
||||
argument: info.homepage,
|
||||
},
|
||||
{
|
||||
parameter: 'version',
|
||||
argument: info.version,
|
||||
},
|
||||
{
|
||||
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].parameters;
|
||||
expect(parameters.hasArgument(testCase.parameter));
|
||||
const argumentValue = parameters.getArgument(testCase.parameter).argumentValue;
|
||||
expect(argumentValue).to.equal(testCase.argument);
|
||||
});
|
||||
}
|
||||
});
|
||||
it('returns code as it is', () => {
|
||||
// arrange
|
||||
const expected = 'expected-code';
|
||||
describe('throws with invalid parameters', () => {
|
||||
// arrange
|
||||
const testCases = [{
|
||||
expectedError: 'undefined code',
|
||||
parameters: {
|
||||
code: undefined,
|
||||
info: new ProjectInformationStub(),
|
||||
},
|
||||
},
|
||||
{
|
||||
expectedError: 'undefined info',
|
||||
parameters: {
|
||||
code: 'non empty code',
|
||||
info: undefined,
|
||||
},
|
||||
}];
|
||||
for (const testCase of testCases) {
|
||||
it(`throws "${testCase.expectedError}" as expected`, () => {
|
||||
const sut = new CodeSubstituterBuilder().build();
|
||||
// act
|
||||
const act = () => sut.substitute(testCase.parameters.code, testCase.parameters.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 = [
|
||||
{
|
||||
parameter: 'homepage',
|
||||
argument: info.homepage,
|
||||
},
|
||||
{
|
||||
parameter: 'version',
|
||||
argument: info.version,
|
||||
},
|
||||
{
|
||||
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)
|
||||
.build();
|
||||
.withCompiler(compilerStub)
|
||||
.withDate(date)
|
||||
.build();
|
||||
// act
|
||||
sut.substitute(expected, new ProjectInformationStub());
|
||||
sut.substitute('non empty code', info);
|
||||
// assert
|
||||
expect(compilerStub.callHistory).to.have.lengthOf(1);
|
||||
expect(compilerStub.callHistory[0].code).to.equal(expected);
|
||||
});
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user