2014-09-04 31 views
1

我正在使用Pikaday在Ember CLI项目中创建Ember Datepicker组件。在组件测试中测试用户交互似乎是不可能的。有谁知道如何做到这一点?接受使用Ember CLI测试Ember Datepicker组件

例如我试图测试点击组件的输入时显示的Pikaday小部件。测试如下所示:

import { test, moduleForComponent } from 'ember-qunit'; 

moduleForComponent('datepicker-input'); 

test('is an input tag', function() { 
    equal('INPUT', this.$().prop('tagName')); 
}); 

test('clicking the input opens the pikaday dialog', function() { 
    ok($('.pika-single').hasClass('is-hidden')); 
    click(this.$().find('input')); 
    ok(!$('.pika-single').hasClass('is-hidden')); 
}); 

第二个测试由于ReferenceError: click is not defined而失败。我不知道我在做什么错误,据我可以告诉我的测试做相同的Ember.js网站上的示例:http://emberjs.com/guides/testing/testing-components/#toc_interacting-with-components-in-the-dom

所以我猜这个问题也可以与Ember CLI。欢迎任何帮助,我愿意提供如何测试组件用户交互的建议。

回答

1

您需要将组件添加到DOM。

test('clicking the input opens the pikaday dialog', function() { 
    $input = this.append(); 

    ok($('.pika-single').hasClass('is-hidden')); 
    click('#your-input').then(function() { 
     ok(!$('.pika-single').hasClass('is-hidden')); 
    }); 
}); 
+0

这使用jQuery中的'click'方法,而不是Ember.js提供的集成测试助手。所以不是我想要完成的。 – stravid 2014-09-04 19:45:42

+0

确定使用集成测试助手。请注意,它需要一个选择器,而不是一个元素。 – Gaurav 2014-09-04 20:04:19

+0

正如我的问题所述,问题在于'click'集成测试助手没有定义。 – stravid 2014-09-04 21:23:43

0

在什么版本的余烬是这个问题?我已经使用自动生成的组件测试几乎完全相同地测试了如何在Ember AddOn(ember 2.4.3)上的ember App上进行测试。

import { moduleForComponent, test } from 'ember-qunit'; 
import hbs from 'htmlbars-inline-precompile'; 

moduleForComponent('image-item', 'Integration | Component | image item', { 
    integration: true 
}); 

test('it renders an image with alt', function(assert) { 
    this.set('src', 'image.png'); 
    this.set('alt', 'grandmother'); 

    this.render(hbs`{{image-item src=src alt=alt}}`); 

    assert.equal(this.$('img').attr('src'), 'image.png'); 
    assert.equal(this.$('img').attr('alt'), 'grandmother'); 
});