Skip to content

Instantly share code, notes, and snippets.

@edwinvdgraaf
Created September 12, 2014 11:39
Show Gist options
  • Save edwinvdgraaf/0cc059e62183f5cc9477 to your computer and use it in GitHub Desktop.
Save edwinvdgraaf/0cc059e62183f5cc9477 to your computer and use it in GitHub Desktop.
mayFire.js
/* # Testing oberserved properties in Ember.js
It can happen that observers are still firing after testing your property, this snippets
lets you define an regex to catch errors, and make it look slightly nicer.
*/
import Ember from 'ember';
export default function mayFire(regEx, callback, scope){
var result;
scope = scope || this;
Ember.onerror = (function(){
var count, errorRegEx;
count = 0;
errorRegEx = regEx;
return function(error){
if(errorRegEx.test(error.message)){
count += 1; // Lets prevent it from cluttering the console.
if(count === 0){
// TODO: Console aint working
console.log(error.message);
}
} else {
throw error;
}
};
})();
stop();
return function(){
var args;
args = arguments;
// Lets delay
setTimeout(function(){
callback.apply(scope, args);
start();
}, 100);
};
}
// Now you can call mayFire
test('Bar draws once', function(){
expect(2);
return this.store().find('team').then(mayFire(/didSetProperty/, function(teams){
equal(teams.content[1].get('draws'), 1, 'Foo draws against bar');
equal(teams.content[2].get('draws'), 1, 'Bar draws against foo');
}, this));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment