How can you test a function that observes a property of an Ember controller?
How can this be done when the observes method is asynchronous (does not return immediately)?
Controller:
var FooController = Ember.Controller.extend({
bar: 1,
baz: null,
barObserver: function() {
var req = ic.ajax.raw({
method: 'POST',
url: '/api/bar',
data: { bar: bar },
});
return req.then(function resolve(result) {
this.set('baz', result.response.baz);
});
}.observes('bar'),
});
Unit test:
test('barObserver', function() {
var ctrl = this.subject();
equal(ctrl.get('baz'), null, 'baz initially null');
Ember.$.mockjax({
url: '/api/bar',
'type': 'POST',
dataType: 'json',
response: function() {
this.responseText = { baz: 'something' };
},
});
ctrl.set('bar', 2);
//wait for barObserver's promise (from ic.ajax.raw) to return
equal(ctrl.get('baz'), 'something', 'baz set to returned value');
});