I have a function that contains a node Fiber which is yielded at the start, then returned when it is ready. What I am trying to do is run the Fiber only when the data has been fetched and added to the collections. The function is below:
/**
* Function to fetch data from Contentful
* @param {String} which - can be contentTypes, entries or assets
* @return {Object} - the result when the data has been fetched - timed by a Fiber yield
*/
fetch: function(which) {
var current = this.Fiber.current,
now,
selector,
modifier,
action,
result;
if(!this.client) {
throw new Meteor.Error(500, 'Contentful client not started. Did you forget to call MeteorContentful.start() ?');
}
else if(typeof this.client[which] !== 'function') {
throw new Meteor.Error(500, 'Contentful does not support this function: ' + which);
}
else {
this.client[which]().then(function(data, err) {
if(err) {
throw new Meteor.Error(500, 'Cannot fetch this data from Contentful: ' + which);
}
this.Fiber(function() {
data.forEach(function(record) {
now = new Date().getTime(),
selector = {'sys\uff0eid': record.sys.id},
modifier = {$setOnInsert: {fetchedAt: now}, $set: {refreshedAt: now, fields: record.fields, sys: record.sys}},
Collections.updateToCollection(which, selector, modifier);
});
current.run(this);
}.bind(this)).run();
}.bind(this));
}
result = this.Fiber.yield();
return result;
},
I am trying to test this function using Velocity and Jasmine, and it seems to be hanging and not finishing. Here is my test:
it('should call Collections.updateToCollection with the correct parameters', function(done) {
/**
* Stubs
*/
MeteorContentful.client = {
collected: function() {
return {
then: function(cb) {
cb([
{sys: {id: 1}, fields: {x: 1, y: 2}}
]);
}
}
}
};
/**
* Spies
*/
spyOn(Collections, 'updateToCollection');
/**
* Run the function and test
*/
console.log('X: ', MeteorContentful.fetch('collected'));
expect(Collections.updateToCollection).toThrow(new Meteor.Error);
/**
* Cleanup and done
*/
MeteorContentful.client = false;
done();
});
So what I want to do is make sure that the Collections.updateToCollection function is being run with the correct parameters. I have checked around for examples and documentation and cannot find a solid and working example of how to test a function that returns a Fiber. Has anyone tried and succeeded in this before and if so, do you have any pointers?
Thanks