Refactor endpoint query component, add additional tests
This commit is contained in:
@@ -6,77 +6,52 @@ define([
|
|||||||
], function(App, _, MailPoet) {
|
], function(App, _, MailPoet) {
|
||||||
|
|
||||||
var Module = {};
|
var Module = {};
|
||||||
var postTypesCache,
|
|
||||||
taxonomiesCache = {},
|
Module._cachedQuery = _.memoize(function(args) {
|
||||||
termsCache = {},
|
return MailPoet.Ajax.post({
|
||||||
postsCache = {},
|
endpoint: 'wordpress',
|
||||||
transformedPostsCache = {};
|
action: args.action,
|
||||||
|
data: args.options || {},
|
||||||
|
});
|
||||||
|
}, JSON.stringify);
|
||||||
|
|
||||||
Module.getPostTypes = function() {
|
Module.getPostTypes = function() {
|
||||||
if (!postTypesCache) {
|
return Module._cachedQuery({
|
||||||
postTypesCache = MailPoet.Ajax.post({
|
|
||||||
endpoint: 'wordpress',
|
|
||||||
action: 'getPostTypes',
|
action: 'getPostTypes',
|
||||||
data: {},
|
options: {},
|
||||||
}).then(function(types) {
|
}).then(function(types) {
|
||||||
return _.values(types);
|
return _.values(types);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return postTypesCache;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Module.getTaxonomies = function(postType) {
|
Module.getTaxonomies = function(postType) {
|
||||||
if (!taxonomiesCache[postType]) {
|
return Module._cachedQuery({
|
||||||
taxonomiesCache[postType] = MailPoet.Ajax.post({
|
|
||||||
endpoint: 'wordpress',
|
|
||||||
action: 'getTaxonomies',
|
action: 'getTaxonomies',
|
||||||
data: {
|
options: {
|
||||||
postType: postType,
|
postType: postType,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return taxonomiesCache[postType];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Module.getTerms = function(options) {
|
Module.getTerms = function(options) {
|
||||||
var key = JSON.stringify(options);
|
return Module._cachedQuery({
|
||||||
if (!termsCache[key]) {
|
|
||||||
termsCache[key] = MailPoet.Ajax.post({
|
|
||||||
endpoint: 'wordpress',
|
|
||||||
action: 'getTerms',
|
action: 'getTerms',
|
||||||
data: options || {},
|
options: options,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return termsCache[key];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Module.getPosts = function(options) {
|
Module.getPosts = function(options) {
|
||||||
var key = JSON.stringify(options);
|
return Module._cachedQuery({
|
||||||
if (!postsCache[key]) {
|
|
||||||
postsCache[key] = MailPoet.Ajax.post({
|
|
||||||
endpoint: 'wordpress',
|
|
||||||
action: 'getPosts',
|
action: 'getPosts',
|
||||||
data: options || {},
|
options: options,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return postsCache[key];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Module.getTransformedPosts = function(options) {
|
Module.getTransformedPosts = function(options) {
|
||||||
var key = JSON.stringify(options);
|
return Module._cachedQuery({
|
||||||
if (!transformedPostsCache[key]) {
|
|
||||||
transformedPostsCache[key] = MailPoet.Ajax.post({
|
|
||||||
endpoint: 'wordpress',
|
|
||||||
action: 'getTransformedPosts',
|
action: 'getTransformedPosts',
|
||||||
data: options || {},
|
options: options,
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
return transformedPostsCache[key];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
App.on('start', function(options) {
|
App.on('start', function(options) {
|
||||||
|
@@ -15,6 +15,13 @@ define([
|
|||||||
describe('model', function () {
|
describe('model', function () {
|
||||||
var model;
|
var model;
|
||||||
|
|
||||||
|
before(function() {
|
||||||
|
WordpressComponent.getPosts = function() {
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
return deferred;
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
global.stubChannel(EditorApplication);
|
global.stubChannel(EditorApplication);
|
||||||
global.stubConfig(EditorApplication);
|
global.stubConfig(EditorApplication);
|
||||||
|
@@ -178,4 +178,146 @@ define([
|
|||||||
mock.verify();
|
mock.verify();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('getPosts', function() {
|
||||||
|
var injector;
|
||||||
|
beforeEach(function() {
|
||||||
|
injector = require('amd-inject-loader!newsletter_editor/components/wordpress');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends options to endpoint', function() {
|
||||||
|
var spy,
|
||||||
|
post = function(params) {
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
deferred.resolve({});
|
||||||
|
return deferred;
|
||||||
|
},
|
||||||
|
module;
|
||||||
|
spy = sinon.spy(post);
|
||||||
|
module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: spy,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
module.getPosts({
|
||||||
|
type: 'posts',
|
||||||
|
search: 'some search term'
|
||||||
|
});
|
||||||
|
expect(spy.args[0][0].data).to.eql({
|
||||||
|
type: 'posts',
|
||||||
|
search: 'some search term'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fetches posts from the server', function() {
|
||||||
|
var module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: function() {
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
deferred.resolve([{post_title: 'title 1'}, {post_title: 'post title 2'}]);
|
||||||
|
return deferred;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
module.getPosts().done(function(posts) {
|
||||||
|
expect(posts).to.eql([{post_title: 'title 1'}, {post_title: 'post title 2'}]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('caches results', function() {
|
||||||
|
var deferred = jQuery.Deferred(),
|
||||||
|
mock = sinon.mock({ post: function() {} }).expects('post').once().returns(deferred),
|
||||||
|
module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: mock,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
deferred.resolve({
|
||||||
|
type: 'posts',
|
||||||
|
search: 'some search term'
|
||||||
|
});
|
||||||
|
module.getPosts({});
|
||||||
|
module.getPosts({});
|
||||||
|
|
||||||
|
mock.verify();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('getTransformedPosts', function() {
|
||||||
|
var injector;
|
||||||
|
beforeEach(function() {
|
||||||
|
injector = require('amd-inject-loader!newsletter_editor/components/wordpress');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends options to endpoint', function() {
|
||||||
|
var spy,
|
||||||
|
post = function(params) {
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
deferred.resolve({});
|
||||||
|
return deferred;
|
||||||
|
},
|
||||||
|
module;
|
||||||
|
spy = sinon.spy(post);
|
||||||
|
module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: spy,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
module.getTransformedPosts({
|
||||||
|
type: 'posts',
|
||||||
|
posts: [1, 2]
|
||||||
|
});
|
||||||
|
expect(spy.args[0][0].data).to.eql({
|
||||||
|
type: 'posts',
|
||||||
|
posts: [1, 2],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fetches transformed posts from the server', function() {
|
||||||
|
var module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: function() {
|
||||||
|
var deferred = jQuery.Deferred();
|
||||||
|
deferred.resolve([{type: 'text', text: 'something'}, {type: 'text', text: 'something else'}]);
|
||||||
|
return deferred;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
module.getTransformedPosts().done(function(posts) {
|
||||||
|
expect(posts).to.eql([{type: 'text', text: 'something'}, {type: 'text', text: 'something else'}]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('caches results', function() {
|
||||||
|
var deferred = jQuery.Deferred(),
|
||||||
|
mock = sinon.mock({ post: function() {} }).expects('post').once().returns(deferred),
|
||||||
|
module = injector({
|
||||||
|
"mailpoet": {
|
||||||
|
Ajax: {
|
||||||
|
post: mock,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
deferred.resolve({
|
||||||
|
type: 'posts',
|
||||||
|
posts: [1, 3],
|
||||||
|
});
|
||||||
|
module.getTransformedPosts({});
|
||||||
|
module.getTransformedPosts({});
|
||||||
|
|
||||||
|
mock.verify();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user