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:
@@ -7,145 +7,148 @@ import { IScriptCode } from '@/domain/IScriptCode';
|
||||
import { ScriptCodeStub } from '@tests/unit/stubs/ScriptCodeStub';
|
||||
|
||||
describe('Script', () => {
|
||||
describe('ctor', () => {
|
||||
describe('scriptCode', () => {
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
const expected = new ScriptCodeStub();
|
||||
const sut = new ScriptBuilder()
|
||||
.withCode(expected)
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.code;
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
it('throws if undefined', () => {
|
||||
// arrange
|
||||
const name = 'script-name';
|
||||
const expectedError = `undefined code (script: ${name})`;
|
||||
const code: IScriptCode = undefined;
|
||||
// act
|
||||
const construct = () => new ScriptBuilder()
|
||||
.withName(name)
|
||||
.withCode(code)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('canRevert', () => {
|
||||
it('returns false without revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(false);
|
||||
});
|
||||
it('returns true with revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code', 'non empty revert code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
describe('level', () => {
|
||||
it('cannot construct with invalid wrong value', () => {
|
||||
// arrange
|
||||
const invalidValue: RecommendationLevel = 55;
|
||||
const expectedError = 'invalid level';
|
||||
// act
|
||||
const construct = () => new ScriptBuilder()
|
||||
.withRecommendationLevel(invalidValue)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
it('sets undefined as expected', () => {
|
||||
// arrange
|
||||
const expected = undefined;
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
expect(sut.level).to.equal(expected);
|
||||
});
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
for (const expected of getEnumValues(RecommendationLevel)) {
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
const actual = sut.level;
|
||||
expect(actual).to.equal(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('documentationUrls', () => {
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
const expected = [ 'doc1', 'doc2 '];
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withDocumentationUrls(expected)
|
||||
.build();
|
||||
const actual = sut.documentationUrls;
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
describe('ctor', () => {
|
||||
describe('scriptCode', () => {
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
const expected = new ScriptCodeStub();
|
||||
const sut = new ScriptBuilder()
|
||||
.withCode(expected)
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.code;
|
||||
// assert
|
||||
expect(actual).to.deep.equal(expected);
|
||||
});
|
||||
it('throws if undefined', () => {
|
||||
// arrange
|
||||
const name = 'script-name';
|
||||
const expectedError = `undefined code (script: ${name})`;
|
||||
const code: IScriptCode = undefined;
|
||||
// act
|
||||
const construct = () => new ScriptBuilder()
|
||||
.withName(name)
|
||||
.withCode(code)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('canRevert', () => {
|
||||
it('returns false without revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(false);
|
||||
});
|
||||
it('returns true with revert code', () => {
|
||||
// arrange
|
||||
const sut = new ScriptBuilder()
|
||||
.withCodes('code', 'non empty revert code')
|
||||
.build();
|
||||
// act
|
||||
const actual = sut.canRevert();
|
||||
// assert
|
||||
expect(actual).to.equal(true);
|
||||
});
|
||||
});
|
||||
describe('level', () => {
|
||||
it('cannot construct with invalid wrong value', () => {
|
||||
// arrange
|
||||
const invalidValue: RecommendationLevel = 55;
|
||||
const expectedError = 'invalid level';
|
||||
// act
|
||||
const construct = () => new ScriptBuilder()
|
||||
.withRecommendationLevel(invalidValue)
|
||||
.build();
|
||||
// assert
|
||||
expect(construct).to.throw(expectedError);
|
||||
});
|
||||
it('sets undefined as expected', () => {
|
||||
// arrange
|
||||
const expected = undefined;
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
expect(sut.level).to.equal(expected);
|
||||
});
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
for (const expected of getEnumValues(RecommendationLevel)) {
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withRecommendationLevel(expected)
|
||||
.build();
|
||||
// assert
|
||||
const actual = sut.level;
|
||||
expect(actual).to.equal(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('documentationUrls', () => {
|
||||
it('sets as expected', () => {
|
||||
// arrange
|
||||
const expected = ['doc1', 'doc2'];
|
||||
// act
|
||||
const sut = new ScriptBuilder()
|
||||
.withDocumentationUrls(expected)
|
||||
.build();
|
||||
const actual = sut.documentationUrls;
|
||||
// assert
|
||||
expect(actual).to.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class ScriptBuilder {
|
||||
private name = 'test-script';
|
||||
private code: IScriptCode = new ScriptCodeStub();
|
||||
private level = RecommendationLevel.Standard;
|
||||
private documentationUrls: readonly string[] = undefined;
|
||||
private name = 'test-script';
|
||||
|
||||
public withCodes(code: string, revertCode = ''): ScriptBuilder {
|
||||
this.code = new ScriptCodeStub()
|
||||
.withExecute(code)
|
||||
.withRevert(revertCode);
|
||||
return this;
|
||||
}
|
||||
private code: IScriptCode = new ScriptCodeStub();
|
||||
|
||||
public withCode(code: IScriptCode): ScriptBuilder {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
private level = RecommendationLevel.Standard;
|
||||
|
||||
public withName(name: string): ScriptBuilder {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
private documentationUrls: readonly string[] = undefined;
|
||||
|
||||
public withRecommendationLevel(level: RecommendationLevel): ScriptBuilder {
|
||||
this.level = level;
|
||||
return this;
|
||||
}
|
||||
public withCodes(code: string, revertCode = ''): ScriptBuilder {
|
||||
this.code = new ScriptCodeStub()
|
||||
.withExecute(code)
|
||||
.withRevert(revertCode);
|
||||
return this;
|
||||
}
|
||||
|
||||
public withDocumentationUrls(urls: readonly string[]): ScriptBuilder {
|
||||
this.documentationUrls = urls;
|
||||
return this;
|
||||
}
|
||||
public withCode(code: IScriptCode): ScriptBuilder {
|
||||
this.code = code;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): Script {
|
||||
return new Script(
|
||||
this.name,
|
||||
this.code,
|
||||
this.documentationUrls,
|
||||
this.level,
|
||||
);
|
||||
}
|
||||
public withName(name: string): ScriptBuilder {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withRecommendationLevel(level: RecommendationLevel): ScriptBuilder {
|
||||
this.level = level;
|
||||
return this;
|
||||
}
|
||||
|
||||
public withDocumentationUrls(urls: readonly string[]): ScriptBuilder {
|
||||
this.documentationUrls = urls;
|
||||
return this;
|
||||
}
|
||||
|
||||
public build(): Script {
|
||||
return new Script(
|
||||
this.name,
|
||||
this.code,
|
||||
this.documentationUrls,
|
||||
this.level,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user