Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
describe.only('validate-biniding-behavior', function () {
class App {
public person: Person = new Person((void 0)!, (void 0)!);
public controller: ValidationController;
public controller2: ValidationController;
public controllerSpy: Spy;
public controller2Spy: Spy;
public trigger: ValidationTrigger = ValidationTrigger.change;
public constructor(
@IContainer container: IContainer,
) {
const factory = container.get(IValidationControllerFactory);
this.controllerSpy = new Spy();
this.controller2Spy = new Spy();
// mocks ValidationCOntrollerFactory#createForCurrentScope
const controller = this.controller = this.controllerSpy.getMock(factory.create()) as unknown as ValidationController;
Registration.instance(IValidationController, controller).register(container);
this.controller2 = this.controller2Spy.getMock(factory.create()) as unknown as ValidationController;
const validationRules = container.get(IValidationRules);
validationRules
// .withMessage('hdhkjdah')
// .matches(/www/)
// .withMessageKey('re')
// .ensure("items[0].prop")
// .required()
// .withMessage('hdhkjdah')
// .matches(/www/)
// .withMessageKey('re')
// .rules
// ;
// console.log(rules);
const rules1 = new ValidationRules()
.ensure("b.c")
.required()
.ensure("d")
.required()
.on(this.a)
.rules;
const result = await this.validator.validateObject(this.a, rules1);
console.log(result);
}
async function runTest(
testFunction: TestFunction,
{ template, customDefaultTrigger }: TestSetupContext
) {
const ctx = TestContext.createHTMLTestContext();
const container = ctx.container;
const host = ctx.dom.createElement('app');
ctx.doc.body.appendChild(host);
let app;
const au = new Aurelia(container);
await au
.register(
customDefaultTrigger
? ValidationConfiguration.customize((options) => {
options.defaultTrigger = customDefaultTrigger;
})
: ValidationConfiguration
)
.app({
host,
component: app = (() => {
const ca = CustomElement.define({ name: 'app', isStrictBinding: true, template }, App);
return new ca(container);
})()
})
.start()
.wait();
await testFunction({ app, container, host });
it(`LengthRule#execute validates ${value} to be ${isValid} for length constraint ${length}`, function () {
const sut = new LengthRule((void 0)!, length, isMax);
assert.equal(sut.messageKey, isMax ? 'maxLength' : 'minLength');
assert.equal(sut.execute(value), isValid);
})
);
it(`RangeRule#execute validates ${value} to be ${isValid} for range ${isInclusive ? `[${range.min}, ${range.max}]` : `(${range.min}, ${range.max})`}`, function () {
const sut = new RangeRule((void 0)!, isInclusive, range);
assert.equal(sut.messageKey, key);
assert.equal(sut.execute(value), isValid);
})
);
it(`RegexRule#execute validates ${value} to be ${isValid}`, function () {
const sut = new RegexRule((void 0)!, /foo/);
assert.equal(sut.execute(value), isValid);
})
);
it(`SizeRule#execute validates ${value} to be ${isValid} for count constraint ${count}`, function () {
const sut = new SizeRule((void 0)!, count, isMax);
assert.equal(sut.messageKey, isMax ? 'maxItems' : 'minItems');
assert.equal(sut.execute(value), isValid);
})
);
const controller = app.controller;
const controllerSpy = app.controllerSpy;
const target: HTMLInputElement = (host as Element).querySelector("#target");
assertControllerBinding(controller, 'person.name', target);
assert.equal(controller.errors.length, 0, 'error1');
await controller.validate();
assert.equal(controller.errors.length, 1, 'error2');
target.value = 'foo';
await assertEventHandler(target, 'blur', 0, scheduler, controllerSpy);
await assertEventHandler(target, 'change', 1, scheduler, controllerSpy);
assert.equal(controller.errors.filter((e) => !e.valid && e.propertyName === 'name').length, 0, 'error3');
},
{ template: `<input type="text">`, customDefaultTrigger: ValidationTrigger.change }
);
$it('supports **changeOrBlur** validation trigger',
async function ({ app, host, container }: TestContext) {
const scheduler = container.get(IScheduler);
const controller = app.controller;
const controllerSpy = app.controllerSpy;
const target: HTMLInputElement = (host as Element).querySelector("#target");
assertControllerBinding(controller, 'person.name', target);
await assertEventHandler(target, 'blur', 1, scheduler, controllerSpy);
assert.equal(controller.errors.filter((e) => !e.valid && e.propertyName === 'name').length, 1, 'error3');
target.value = 'foo';
await assertEventHandler(target, 'change', 1, scheduler, controllerSpy);
it(`EqualsRule#execute validates ${value} to be ${isValid} for expected value ${expectedValue}`, function () {
const sut = new EqualsRule((void 0)!, expectedValue);
assert.equal(sut.execute(value), isValid);
})
);
it(`RequiredRule#execute validates ${value} to be ${isValid}`, function () {
const sut = new RequiredRule((void 0)!);
assert.equal(sut.execute(value), isValid);
})
);