Skip to content

Commit

Permalink
feat(ValidationControllerFactory): add validator arg to createForCurr…
Browse files Browse the repository at this point in the history
…entScope

fixes aurelia#347
  • Loading branch information
jdanyow committed Sep 26, 2016
1 parent 5f6da59 commit 70b1176
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/validation-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Container} from 'aurelia-dependency-injection';
import {ValidationController} from './validation-controller';
import {Validator} from './validator';

/**
* Creates ValidationController instances.
Expand All @@ -12,19 +13,21 @@ export class ValidationControllerFactory {
constructor(private container: Container) {}

/**
* Creates a new controller and registers it in the current element's container so that it's
* available to the validate binding behavior and renderers.
* Creates a new controller instance.
*/
create() {
return this.container.invoke(ValidationController);
create(validator?: Validator) {
if (!validator) {
validator = <Validator>this.container.get(Validator);
}
return new ValidationController(validator);
}

/**
* Creates a new controller and registers it in the current element's container so that it's
* available to the validate binding behavior and renderers.
*/
createForCurrentScope() {
const controller = this.create();
createForCurrentScope(validator?: Validator) {
const controller = this.create(validator);
this.container.registerInstance(ValidationController, controller);
return controller;
}
Expand Down
8 changes: 7 additions & 1 deletion test/validation-controller-factory.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import {Container, Optional} from 'aurelia-dependency-injection';
import {
ValidationControllerFactory,
ValidationController
ValidationController,
Validator
} from '../src/aurelia-validation';

describe('ValidationControllerFactory', () => {
it('createForCurrentScope', () => {
const container = new Container();
const standardValidator = {};
container.registerInstance(Validator, standardValidator);
const childContainer = container.createChild();
const factory = childContainer.get(ValidationControllerFactory);
const controller = factory.createForCurrentScope();
expect(controller.validator).toBe(standardValidator);
expect(container.get(Optional.of(ValidationController))).toBe(null);
expect(childContainer.get(Optional.of(ValidationController))).toBe(controller);
const customValidator = {};
expect(factory.createForCurrentScope(customValidator).validator).toBe(customValidator);
});
});

0 comments on commit 70b1176

Please sign in to comment.