我正在尝试在我的Ember应用程序单元测试中测试组件,直到现在为止一切都很好,除了我处于断言需要呈现其模板的时候。
通常这样做会打电话给
var comp = App.SomeNamedComponent.create();
var comp.appendTo(App.rootElement);
但是,尽管这确实创建了组件的基础元素,但并未呈现其模板。经过一些研究,我最终发现该组件上既templateName
没有template
设置属性,也没有设置属性。因此,我决定templateName
自己动手,但后来抱怨了A Component must have a parent view in order to yield.
。
然后,我决定使用该组件的模板在测试中创建另一个自定义视图,但随后我无法访问该组件的实例...
我需要访问该实例以进行断言,并且需要渲染它的模板,因为某些属性是根据模板中某些元素的css计算出来的。
这是我通常在不需要容器时测试组件的方式(特别是在以编程方式向组件提供模板和布局时):
Ember.testing = true;
MyAwesomeComponent = Ember.Component.extend();
function createComponent(componentName, factory, options) {
if (typeof options.template === 'string') {
options.template = Ember.Handlebars.compile(options.template);
}
if (typeof options.layout === 'string') {
options.layout = Ember.Handlebars.compile(options.layout);
}
if (options.template && !options.layout) {
options.layout = options.template;
delete options.template;
}
var component = factory.create(options);
Ember.run(function(){
component.appendTo('#qunit-fixture');
});
return component;
}
module('component testing sample');
test('a component with template', function(){
var options = {layout: 'woot woot{{fullName}}'};
var component = createComponent('my-awesome', MyAwesomeComponent, options);
equal(component.$().text(), 'woot woot');
});
test('a component with custom options and a template', function(){
var options = {
fullName: 'Robert Jackson',
layout: '{{fullName}}'
};
var component = createComponent('my-awesome', MyAwesomeComponent, options);
equal(component.$().text(), 'Robert Jackson');
});
参见示例JSBin。
如果您需要/希望能够查找模板,则可以使用类似以下的内容(它创建一个隔离的容器):
Ember.testing = true;
MyAwesomeComponent = Ember.Component.extend();
function isolatedContainer() {
var container = new Ember.Container();
container.optionsForType('component', { singleton: false });
container.optionsForType('view', { singleton: false });
container.optionsForType('template', { instantiate: false });
container.optionsForType('helper', { instantiate: false });
return container;
}
function createComponent(componentName, factory, options) {
var fullName = 'component:' + componentName,
templateFullName = 'template:components/' + componentName;
container.register(fullName, factory);
if (container.has(templateFullName)) {
container.injection(fullName, 'layout', templateFullName);
}
var Component = container.lookupFactory(fullName),
component = Component.create(options);
Ember.run(function(){
component.appendTo('#qunit-fixture');
});
return component;
}
function registerTemplate(name, template){
if (typeof template !== 'function') {
template = Ember.Handlebars.compile(template);
}
container.register('template:' + name, template);
}
var container;
module('component testing sample', {
setup: function(){
container = isolatedContainer();
},
teardown: function(){
Ember.run(container, 'destroy');
}
});
test('a component with template', function(){
registerTemplate('components/my-awesome', 'woot woot{{fullName}}');
var component = createComponent('my-awesome', MyAwesomeComponent);
equal(component.$().text(), 'woot woot');
});
test('a component with custom options and a template', function(){
registerTemplate('components/my-awesome', '{{fullName}}');
var component = createComponent('my-awesome', MyAwesomeComponent, {fullName: 'Robert Jackson'});
equal(component.$().text(), 'Robert Jackson');
});
容器版本的JSBin。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句