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

View File

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

View File

@ -36,36 +36,46 @@ define([
return Module._cachedQuery({ return Module._cachedQuery({
action: 'getTaxonomies', action: 'getTaxonomies',
options: { options: {
postType: postType, postType: postType
}, }
}).then(function(response) {
return response.data;
}); });
}; };
Module.getTerms = function(options) { Module.getTerms = function(options) {
return Module._cachedQuery({ return Module._cachedQuery({
action: 'getTerms', action: 'getTerms',
options: options, options: options
}).then(function(response) {
return response.data;
}); });
}; };
Module.getPosts = function(options) { Module.getPosts = function(options) {
return Module._cachedQuery({ return Module._cachedQuery({
action: 'getPosts', action: 'getPosts',
options: options, options: options
}).then(function(response) {
return response.data;
}); });
}; };
Module.getTransformedPosts = function(options) { Module.getTransformedPosts = function(options) {
return Module._cachedQuery({ return Module._cachedQuery({
action: 'getTransformedPosts', action: 'getTransformedPosts',
options: options, options: options
}).then(function(response) {
return response.data;
}); });
}; };
Module.getBulkTransformedPosts = function(options) { Module.getBulkTransformedPosts = function(options) {
return Module._query({ return Module._query({
action: 'getBulkTransformedPosts', 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); mock1.expects('trigger').once().withArgs('refreshPosts', postsSet1);
mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2); mock2.expects('trigger').once().withArgs('refreshPosts', postsSet2);
model.refreshBlocks([block1, block2], { data: [postsSet1, postsSet2] }); model.refreshBlocks([block1, block2], [postsSet1, postsSet2]);
mock1.verify(); mock1.verify();
mock2.verify(); mock2.verify();

View File

@ -73,13 +73,17 @@ define([
expect(spy.args[0][0].data.postType).to.equal('post'); 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({ var module = CommunicationInjector({
"mailpoet": { "mailpoet": {
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ 'category': 'val1' }); deferred.resolve({
data: {
'category': 'val1'
}
});
return deferred; return deferred;
} }
}, },
@ -110,13 +114,13 @@ define([
describe('getTerms', function() { describe('getTerms', function() {
it('sends terms to endpoint', function() { it('sends terms to endpoint', function() {
var spy, var spy;
post = function(params) { var post = function(params) {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({}); deferred.resolve({});
return deferred; return deferred;
}, }
module; var module;
spy = sinon.spy(post); spy = sinon.spy(post);
module = CommunicationInjector({ module = CommunicationInjector({
"mailpoet": { "mailpoet": {
@ -138,7 +142,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); var deferred = jQuery.Deferred();
deferred.resolve({ 'term1': 'term1val1', 'term2': 'term2val2' }); deferred.resolve({
data: {
'term1': 'term1val1',
'term2': 'term2val2'
}
});
return deferred; return deferred;
} }
}, },
@ -201,7 +210,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); 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; return deferred;
} }
}, },
@ -267,7 +281,12 @@ define([
Ajax: { Ajax: {
post: function() { post: function() {
var deferred = jQuery.Deferred(); 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; return deferred;
} }
}, },