encapsulating in communication component + update js tests

This commit is contained in:
Jonathan Labreuille
2016-08-08 11:36:29 +02:00
parent 1305a10ee0
commit c136d91dd2
5 changed files with 64 additions and 35 deletions

View File

@ -51,9 +51,9 @@ define([
blocks: blocks,
}).then(_.partial(this.refreshBlocks, models));
},
refreshBlocks: function(models, response) {
refreshBlocks: function(models, renderedBlocks) {
_.each(
_.zip(models, response.data),
_.zip(models, renderedBlocks),
function(args) {
var model = args[0],
contents = args[1];
@ -220,16 +220,16 @@ define([
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
search: options.data.term,
taxonomies: _.keys(taxonomies)
}).then(function(response) {
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: response.data
terms: terms
};
});
return promise;

View File

@ -108,8 +108,8 @@ define([
},
fetchAvailablePosts: function() {
var that = this;
CommunicationComponent.getPosts(this.toJSON()).done(function(response) {
that.get('_availablePosts').reset(response.data);
CommunicationComponent.getPosts(this.toJSON()).done(function(posts) {
that.get('_availablePosts').reset(posts);
that.get('_selectedPosts').reset(); // Empty out the collection
that.trigger('change:_availablePosts');
}).fail(function() {
@ -127,8 +127,8 @@ define([
return;
}
CommunicationComponent.getTransformedPosts(data).done(function(response) {
that.get('_transformedPosts').get('blocks').reset(response.data, {parse: true});
CommunicationComponent.getTransformedPosts(data).done(function(posts) {
that.get('_transformedPosts').get('blocks').reset(posts, {parse: true});
}).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
});
@ -143,8 +143,8 @@ define([
if (data.posts.length === 0) return;
CommunicationComponent.getTransformedPosts(data).done(function(response) {
collection.add(response.data, { at: index });
CommunicationComponent.getTransformedPosts(data).done(function(posts) {
collection.add(posts, { at: index });
}).fail(function() {
MailPoet.Notice.error(MailPoet.I18n.t('failedToFetchRenderedPosts'));
});
@ -305,16 +305,16 @@ define([
var taxonomies;
var promise = CommunicationComponent.getTaxonomies(
that.model.get('contentType')
).then(function(response) {
taxonomies = response.data;
).then(function(tax) {
taxonomies = tax;
// Fetch available terms based on the list of taxonomies already fetched
var promise = CommunicationComponent.getTerms({
search: options.data.term,
taxonomies: _.keys(taxonomies)
}).then(function(response) {
}).then(function(terms) {
return {
taxonomies: taxonomies,
terms: response.data
terms: terms
};
});
return promise;

View File

@ -36,36 +36,46 @@ define([
return Module._cachedQuery({
action: 'getTaxonomies',
options: {
postType: postType,
},
postType: postType
}
}).then(function(response) {
return response.data;
});
};
Module.getTerms = function(options) {
return Module._cachedQuery({
action: 'getTerms',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getPosts = function(options) {
return Module._cachedQuery({
action: 'getPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getTransformedPosts = function(options) {
return Module._cachedQuery({
action: 'getTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};
Module.getBulkTransformedPosts = function(options) {
return Module._query({
action: 'getBulkTransformedPosts',
options: options,
options: options
}).then(function(response) {
return response.data;
});
};

View File

@ -53,7 +53,7 @@ define([
mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1);
mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2);
model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] });
model.refreshBlocks([block1, block2], [postsSet1, postsSet2]);
mock1.verify();
mock2.verify();

View File

@ -73,13 +73,17 @@ define([
expect(spy.args[0][0].data.postType).to.equal('post');
});
it('fetches post types from the server', function() {
it('fetches taxonomies from the server', function() {
var module = CommunicationInjector({
"mailpoet": {
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve({ 'category': 'val1' });
deferred.resolve({
data: {
'category': 'val1'
}
});
return deferred;
}
},
@ -110,13 +114,13 @@ define([
describe('getTerms', function() {
it('sends terms to endpoint', function() {
var spy,
post = function(params) {
var spy;
var post = function(params) {
var deferred = jQuery.Deferred();
deferred.resolve({});
return deferred;
},
module;
}
var module;
spy = sinon.spy(post);
module = CommunicationInjector({
"mailpoet": {
@ -138,7 +142,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve({ 'term1': 'term1val1', 'term2': 'term2val2' });
deferred.resolve({
data: {
'term1': 'term1val1',
'term2': 'term2val2'
}
});
return deferred;
}
},
@ -201,7 +210,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve([{post_title: 'title 1'}, {post_title: 'post title 2'}]);
deferred.resolve({
data: [
{post_title: 'title 1'},
{post_title: 'post title 2'}
]
});
return deferred;
}
},
@ -267,7 +281,12 @@ define([
Ajax: {
post: function() {
var deferred = jQuery.Deferred();
deferred.resolve([{type: 'text', text: 'something'}, {type: 'text', text: 'something else'}]);
deferred.resolve({
data: [
{type: 'text', text: 'something'},
{type: 'text', text: 'something else'}
]
});
return deferred;
}
},