diff --git a/packages/concerto-analysis/src/compare-config.ts b/packages/concerto-analysis/src/compare-config.ts index 85b5b6700..75c725e91 100644 --- a/packages/concerto-analysis/src/compare-config.ts +++ b/packages/concerto-analysis/src/compare-config.ts @@ -100,12 +100,16 @@ export class CompareConfigBuilder { /** * Adds default comparer configuration onto the configuration * being built. + * + * @returns {CompareConfigBuilder} A reference to the builder object to chain */ - public default() { + public default(): CompareConfigBuilder { this._config = { comparerFactories: [...this._config.comparerFactories, ...defaultCompareConfig.comparerFactories], rules: { ...this._config.rules, ...defaultCompareConfig.rules }, }; + + return this; } /** @@ -113,12 +117,15 @@ export class CompareConfigBuilder { * with the provided config. * * @param {CompareConfig} config - The configuration to extend with + * @returns {CompareConfigBuilder} A reference to the builder object to chain */ - public extend(config: CompareConfig) { + public extend(config: CompareConfig): CompareConfigBuilder { this._config = { comparerFactories: [...this._config.comparerFactories, ...config.comparerFactories], rules: { ...this._config.rules, ...config.rules }, }; + + return this; } /** @@ -126,32 +133,41 @@ export class CompareConfigBuilder { * * @param {string} ruleKey - A key that is referenced from one of the comparer factories * @param {CompareResult} result - A version diff outcome based on this rule + * @returns {CompareConfigBuilder} A reference to the builder object to chain */ - public addRule(ruleKey: string, result: CompareResult) { + public addRule(ruleKey: string, result: CompareResult): CompareConfigBuilder { this._config.rules[ruleKey] = result; + + return this; } /** * Removes a comparison outcome rule from the configuration * * @param {string} ruleKey - A key that is referenced from one of the comparer factories + * @returns {CompareConfigBuilder} A reference to the builder object to chain * @throws {ReferenceError} * Thrown if the `ruleKey` does not exist in the configuration */ - public removeRule(ruleKey: string) { + public removeRule(ruleKey: string): CompareConfigBuilder { if (!this._config.rules[ruleKey]) { throw new ReferenceError(`ruleKey '${ruleKey}' does not exist`); } delete this._config.rules[ruleKey]; + + return this; } /** * Add a {@link ComparerFactory} to the configuration. * * @param {ComparerFactory} f - A {@link ComparerFactory} that should reference the rules in the configuration + * @returns {CompareConfigBuilder} A reference to the builder object to chain */ - public addComparerFactory(f: ComparerFactory) { + public addComparerFactory(f: ComparerFactory): CompareConfigBuilder { this._config.comparerFactories = [...this._config.comparerFactories, f]; + + return this; } } diff --git a/packages/concerto-analysis/test/unit/compare-config.test.ts b/packages/concerto-analysis/test/unit/compare-config.test.ts index 24aeaf569..d37fcba63 100644 --- a/packages/concerto-analysis/test/unit/compare-config.test.ts +++ b/packages/concerto-analysis/test/unit/compare-config.test.ts @@ -12,9 +12,8 @@ describe('CompareConfigBuilder', () => { it('Should add default config with `default`', () => { const builder = new CompareConfigBuilder(); - builder.default(); - const actual = builder.build(); + const actual = builder.default().build(); expect(actual.comparerFactories.length).toEqual(11); expect(Object.keys(actual.rules).length).toEqual(20); @@ -34,10 +33,8 @@ describe('CompareConfigBuilder', () => { rules: newRules }; const builder = new CompareConfigBuilder(); - builder.default(); - builder.extend(toExtend); - const actual = builder.build(); + const actual = builder.default().extend(toExtend).build(); expect(actual.comparerFactories.length).toEqual(12); expect(Object.keys(actual.rules).length).toEqual(21); @@ -46,10 +43,8 @@ describe('CompareConfigBuilder', () => { it('Should add a new comparer factory', () => { const builder = new CompareConfigBuilder(); - builder.default(); - builder.addComparerFactory(() => ({})); - const actual = builder.build(); + const actual = builder.default().addComparerFactory(() => ({})).build(); expect(actual.comparerFactories.length).toEqual(12); expect(Object.keys(actual.rules).length).toEqual(20); @@ -57,10 +52,8 @@ describe('CompareConfigBuilder', () => { it('Should add a new rule', () => { const builder = new CompareConfigBuilder(); - builder.default(); - builder.addRule('a-new-rule', CompareResult.MAJOR); - const actual = builder.build(); + const actual = builder.default().addRule('a-new-rule', CompareResult.MAJOR).build(); expect(actual.comparerFactories.length).toEqual(11); expect(Object.keys(actual.rules).length).toEqual(21); @@ -69,10 +62,8 @@ describe('CompareConfigBuilder', () => { it('Should remove an existing rule', () => { const builder = new CompareConfigBuilder(); - builder.default(); - builder.removeRule('optional-property-added'); - const actual = builder.build(); + const actual = builder.default().removeRule('optional-property-added').build(); expect(actual.comparerFactories.length).toEqual(11); expect(Object.keys(actual.rules).length).toEqual(19); @@ -81,7 +72,6 @@ describe('CompareConfigBuilder', () => { it('Should throw while removing a rule that does not exist', () => { const builder = new CompareConfigBuilder(); - builder.default(); - expect(() => builder.removeRule('does-not-exist')).toThrow('ruleKey \'does-not-exist\' does not exist'); + expect(() => builder.default().removeRule('does-not-exist')).toThrow('ruleKey \'does-not-exist\' does not exist'); }); });