1
如果我有一個app.html模板如下:Aurelia StageComponent - 如何將子組件呈現到DOM樹中?
<template>
<require from="./MyComponent"></require>
<h1>${message}</h1>
<my-component>test</my-component>
</template>
隨着MyComponent.ts:
export class MyComponent {
myName : string;
}
而且MyComponent.html:
<template>
MyComponent
</template>
下面的單元測試總會失敗:
import {App} from '../../src/app';
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
describe('the app',() => {
var app ;
beforeEach(() => {
app = StageComponent
.withResources('app')
.inView(' <require from="./MyComponent"></require>' +
'<h1>${message}</h1>' +
'<my-component>test</my-component>')
.boundTo(new App());
});
it('says hello', (done) => {
app.create(bootstrap).then(() => {
var myComponent = document.querySelector('my-component');
expect(myComponent.innerHTML).toContain('MyComponent');
done();
});
});
});
請注意,StageComponent會正確地將模板中的$ {message}替換爲app.html,但不會創建新的MyComponent實例。 當瀏覽器內運行此,產生的DOM是:
<h1>Hello World!</h1>
<my-component class="au-target" au-target-id="2">
MyComponent
</my-component>
但通過StageComponent在測試運行相同的代碼時,DOM的產生是:
<h1>Hello World!</h1>
<my-component>test</my-component>
我缺少什麼?
嗨,Eric,謝謝你的迴應。你上面描述的是獨立測試MyComponent元素。我試圖同時渲染應用程序組件和MyComponent,這是瀏覽器中上述代碼的行爲。因此,要回答您的問題,是的,我想測試應用程序視圖是否與其組件正確呈現。 – blorkfish