[英]Aurelia testing components with <compose>
I am trying to test a custom dialog component to be used with aurelia-dialog
which gets it's property bindings through an activate()
method.我正在尝试测试与
aurelia-dialog
一起使用的自定义对话框组件,该组件通过activate()
方法获取其属性绑定。 In order to test the component, I am setting up the tests with a <compose>
element which also utilizes the activate()
method like so:为了测试组件,我使用
<compose>
元素设置测试,该元素也使用activate()
方法,如下所示:
beforeEach(() => {
component = StageComponent
.withResources('path/to/dialogComponent')
.inView(`<compose view-model="path/to/dialogComponent" model.bind="mockModel">
</compose>`)
.boundTo(mockModel);
});
However, when I come to test the view-model for bound properties I get a null where the actual view model used to be.但是,当我来测试视图模型的绑定属性时,我在实际视图模型曾经所在的位置得到了一个空值。
describe('#someComponentMethod()', () => {
it('Should exist', done => {
// In the past, I succesfully accessed child viewModels for
// compose through the following property, after a general package update,
// this seems not to work anymore
let viewModel = component.viewModel.currentViewModel
expect(viewModel.someComponentMethod).toBeDefined();
// ==> Runtime error, since currentViewModel === null
done();
});
});
Is there a known or better way to test custom elements which have no bindable properties but rely on a model to be bound by activate()
?是否有已知或更好的方法来测试没有可绑定属性但依赖于被
activate()
绑定的模型的自定义元素?
Additionally, in order to test rendering: There seems to be a similair issue that might be related: Aurelia Testing Composed Custom Element此外,为了测试渲染:似乎有一个可能相关的类似问题: Aurelia Testing Composed Custom Element
You can call the create function and pass in bootstrap imported from aurelia-bootstrapper
like so:您可以调用 create 函数并传入从
aurelia-bootstrapper
导入aurelia-bootstrapper
如下所示:
import { bootstrap } from 'aurelia-bootstrapper';
...
component.create(bootstrap);
When I call create, the viewModel is built and populated.当我调用 create 时,会构建并填充 viewModel。 The create function is awaitable so you can either access the viewmodel property in .then(() => component.viewModel) or you use async/await like so:
create 函数是可等待的,因此您可以访问 .then(() => component.viewModel) 中的 viewmodel 属性,或者像这样使用 async/await:
await component.create(bootstrap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.