From a52f4c403bb68ed895fd0b7a3ea217c67ce22e2d Mon Sep 17 00:00:00 2001 From: Romke van der Meulen Date: Fri, 1 Nov 2019 12:01:39 +0100 Subject: [PATCH] feat(config): make config constructor param optional and place default trigger in static property --- src/config.ts | 4 +++- src/validation-controller.ts | 6 ++++-- test/validation-controller.ts | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 test/validation-controller.ts diff --git a/src/config.ts b/src/config.ts index cde94c3c..edf3b794 100644 --- a/src/config.ts +++ b/src/config.ts @@ -7,8 +7,10 @@ import { validateTrigger } from './validate-trigger'; * Aurelia Validation Configuration API */ export class AureliaValidationConfiguration { + public static DEFAULT_VALIDATION_TRIGGER = validateTrigger.blur; + private validatorType: { new (...args: any[]): Validator } = StandardValidator; - private validationTrigger = validateTrigger.blur; + private validationTrigger = AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER; /** * Use a custom Validator implementation. diff --git a/src/validation-controller.ts b/src/validation-controller.ts index 941ed8f0..5f20840e 100644 --- a/src/validation-controller.ts +++ b/src/validation-controller.ts @@ -58,9 +58,11 @@ export class ValidationController { constructor( private validator: Validator, private propertyParser: PropertyAccessorParser, - config: AureliaValidationConfiguration, + config?: AureliaValidationConfiguration, ) { - this.validateTrigger = config.getDefaultValidationTrigger(); + this.validateTrigger = config instanceof AureliaValidationConfiguration + ? config.getDefaultValidationTrigger() + : AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER; } /** diff --git a/test/validation-controller.ts b/test/validation-controller.ts new file mode 100644 index 00000000..f2ac13eb --- /dev/null +++ b/test/validation-controller.ts @@ -0,0 +1,22 @@ +import { + AureliaValidationConfiguration, + PropertyAccessorParser, + ValidationController, + Validator, + validateTrigger, +} from '../src/aurelia-validation'; + +describe('ValidationController', () => { + it('takes a validator, a PropertyAccessorParser, and optional config', () => { + const validator = {} as any as Validator; + const parser = {} as any as PropertyAccessorParser; + const controller = new ValidationController(validator, parser); + expect(controller.validateTrigger).toBe(AureliaValidationConfiguration.DEFAULT_VALIDATION_TRIGGER); + + const trigger = validateTrigger.changeOrBlur; + const config = new AureliaValidationConfiguration(); + config.defaultValidationTrigger(trigger); + const controllerWithConfig = new ValidationController(validator, parser, config); + expect(controllerWithConfig.validateTrigger).toBe(trigger); + }); +});