Ember 更新注入服务时组件未在集成测试中更新
Ember component not updating in integration test when injected service is updated
我有一个 side-bar 组件依赖于 side-bar 服务,该服务通过初始化程序注入其中。
该组件然后有一个计算的 属性 标题,该标题绑定到服务上的相同 属性:
title: function () {
return this.get('sideBarService.title');
}.property('sideBarService.title'),
这在应用程序本身中有效,但当服务更新时,我无法在集成测试中更新组件。
这是我的非工作集成测试:
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
container = application.__container__;
sideBarService = container.lookup('service:side-bar');
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
Ember.run(function () {
sideBarService.set('title', 'Hello');
});
this.render(hbs`
{{side-bar}}
`);
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
// fails
assert.deepEqual(content, serviceTitle);
});
有趣的是,如果我在测试中调试并使用控制台获取组件,然后从组件中获取 sideBarService,它会知道更新的标题值,甚至组件本身的标题值似乎是已更新,但 dom 永远不会更新:
//debugged in browser console
var sb = container.lookup('component:side-bar')
undefined
sb.get('title')
"Hello"
sb.get('sideBarService.title')
"Hello"
this.$('.title').text().trim()
""
这是 运行 循环问题吗?如果是这样,我需要做什么来启动它?
编辑:关于 Toran 的评论。这看起来对吗?
var done = assert.async();
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
setTimeout(function() {
assert.deepEqual(content, serviceTitle);
done();
});
我可能会通过避免在初始化程序中注入并使用 Ember.inject.service
帮助程序来解决这个问题。
// component
import Ember from 'ember'
const { Component, inject, computed } = Ember;
const { service } = inject;
const { alias } = computed;
export default Component.extend({
sideBarService: service('side-bar'),
title: alias('sideBarService.title')
});
那么在你的测试中,你在使用组件的时候就可以通过服务了。
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
this.set('sideBarService', Ember.Object.create({
title: 'hello'
}));
this.render(hbs`
{{side-bar sideBarService=sideBarService}}
`);
var title = this.$('.side-bar-content .title').text().trim();
assert.equal(title, 'hello'); // Hopefully passes
});
我有一个 side-bar 组件依赖于 side-bar 服务,该服务通过初始化程序注入其中。
该组件然后有一个计算的 属性 标题,该标题绑定到服务上的相同 属性:
title: function () {
return this.get('sideBarService.title');
}.property('sideBarService.title'),
这在应用程序本身中有效,但当服务更新时,我无法在集成测试中更新组件。
这是我的非工作集成测试:
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
container = application.__container__;
sideBarService = container.lookup('service:side-bar');
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
Ember.run(function () {
sideBarService.set('title', 'Hello');
});
this.render(hbs`
{{side-bar}}
`);
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
// fails
assert.deepEqual(content, serviceTitle);
});
有趣的是,如果我在测试中调试并使用控制台获取组件,然后从组件中获取 sideBarService,它会知道更新的标题值,甚至组件本身的标题值似乎是已更新,但 dom 永远不会更新:
//debugged in browser console
var sb = container.lookup('component:side-bar')
undefined
sb.get('title')
"Hello"
sb.get('sideBarService.title')
"Hello"
this.$('.title').text().trim()
""
这是 运行 循环问题吗?如果是这样,我需要做什么来启动它?
编辑:关于 Toran 的评论。这看起来对吗?
var done = assert.async();
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
setTimeout(function() {
assert.deepEqual(content, serviceTitle);
done();
});
我可能会通过避免在初始化程序中注入并使用 Ember.inject.service
帮助程序来解决这个问题。
// component
import Ember from 'ember'
const { Component, inject, computed } = Ember;
const { service } = inject;
const { alias } = computed;
export default Component.extend({
sideBarService: service('side-bar'),
title: alias('sideBarService.title')
});
那么在你的测试中,你在使用组件的时候就可以通过服务了。
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
this.set('sideBarService', Ember.Object.create({
title: 'hello'
}));
this.render(hbs`
{{side-bar sideBarService=sideBarService}}
`);
var title = this.$('.side-bar-content .title').text().trim();
assert.equal(title, 'hello'); // Hopefully passes
});