我正在尝试在表单上实施一些单元测试,以查看验证规则是否按预期工作。
从此页面上:https : //github.com/aurelia/testing/issues/63
我发现了这个实现:https : //github.com/aurelia/validation/blob/master/test/validate-binding-behavior.ts
我试图在我的项目中实现它
login.spec.js
import {bootstrap} from 'aurelia-bootstrapper';
import {StageComponent} from 'aurelia-testing';
import {PLATFORM} from 'aurelia-pal';
import { configure, blur, change } from './shared';
import { Login } from './login';
describe('ValidateBindingBehavior', () => {
it('sets validateTrigger', (done) => {
const component = StageComponent
.withResources(PLATFORM.moduleName('features/account/login/login'))
.inView('<login></login>')
.boundTo({});
component.bootstrap(configure);
let viewModel;
const renderer = { render: jasmine.createSpy() };
component.create(bootstrap)
// grab some references.
.then(() => {
viewModel = component.viewModel;
viewModel.controller.addRenderer(renderer);
})
.then(() => expect(viewModel.controller.errors.length).toBe(0))
.then(() => blur(viewModel.firstName))
.then(() => expect(viewModel.controller.errors.length).toBe(1))
.then(() => component.dispose())
.then(done);
});
});
login.js
import { inject, NewInstance } from 'aurelia-dependency-injection';
import { ValidationController } from 'aurelia-validation';
import { User } from './login.model';
@inject(NewInstance.of(ValidationController), User)
export class Login {
constructor(controller, user) {
this.controller = controller;
this.firstName = '';
this.lastName = '';
this.userName = '';
this.showForm = true;
this.user = user;
}
};
login.model.js
import {ValidationRules} from 'aurelia-validation';
export class User {
firstName = '';
lastName = '';
userName = '';
constructor() {
ValidationRules
.ensure('firstName')
.required()
.ensure('lastName')
.required()
.minLength(10)
.ensure('userName')
.required()
.on(this);
}
}
shared.js
import {DOM, PLATFORM} from 'aurelia-pal';
export function configure(aurelia) {
return aurelia.use
.standardConfiguration()
.plugin(PLATFORM.moduleName('aurelia-validation'))
}
export function blur(element) {
element.dispatchEvent(DOM.createCustomEvent('blur', {}));
return new Promise(resolve => setTimeout(resolve));
}
export function change(element, value) {
element.value = value;
element.dispatchEvent(DOM.createCustomEvent('change', { bubbles: true }));
return new Promise(resolve => setTimeout(resolve));
}
这是一段html标记:
<div>
<input ref="firstName" type="text" value.bind="user.firstName & validateOnBlur"
validation-errors.bind="firstNameErrors">
<label style="display: block;color:red" repeat.for="errorInfo of firstNameErrors">
${errorInfo.error.message}
</label>
</div>
<div>
在规范中,当我模糊元素时,我希望得到一个错误,但是“ controller.errors”始终是一个空数组。我得到这个失败的消息:
错误:预期0为1。
更新1:
我尝试手动验证,因此在规范中添加了此代码:
.then(()=>
viewModel.controller.validate({object: viewModel.user, propertyName: 'firstName' })
)
而且效果很好,但模糊和更改功能不会触发验证。
更新2:
我按照“ Sayan Pal”的建议进行了更改。现在可以使用,但是有一个小问题。当我“模糊”元素一次显示一个错误时。但是当我“模糊”几个元素(比如说三个)时,它并没有显示最后一个错误。在这种情况下,controller.errors.length将为2。
我可以模糊两次最后一个元素,以获取正确长度的错误。但我认为应该有一个更好的解决方案。
.then(() => blur(viewModel.firstName))
.then(() => blur(viewModel.userName))
.then(() => blur(viewModel.lastName))
.then(() => blur(viewModel.lastName))
我认为不必使用createCustomEvent
您,只需要做element.dispatchEvent(new Event("blur"));
。同去的change
事件。
这一直对我有用,希望对您也有帮助:)
值得一提的是,我使用默认的ValidationController
生成器工厂方法来确保默认触发器如下。
import { validateTrigger, ValidationControllerFactory } from "aurelia-validation";
...
const validationController = validationControllerFactory.createForCurrentScope();
validationController.changeTrigger(validateTrigger.changeOrBlur);
OP更新问题后更新
如果不调试,很难说出它为什么会发生。由于我在您的测试代码中没有看到任何迫在眉睫的问题,因此我认为这是一个计时问题。主要思想是您需要等待更改发生。您可以通过多种方式来做到这一点,所有这些需求都将在您声明方式方面发生变化。
一种实现方法是采用带有超时的promise,该超时会定期对更改进行轮询。然后等待诺言。
或者,您可以使用TaskQueue在断言调用之后和对断言进行排队done
。如下所示。
new TaskQueue().queueMicroTask(() => {
expect(foo).toBe(bar);
done();
});
另一种选择是使用cypress作为e2e测试框架。赛普拉斯开箱即用,等待更改发生,直到超时。
选择最适合您的需求。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句