Refactor endpoint query component, add additional tests
This commit is contained in:
@@ -6,77 +6,52 @@ define([
|
||||
], function(App, _, MailPoet) {
|
||||
|
||||
var Module = {};
|
||||
var postTypesCache,
|
||||
taxonomiesCache = {},
|
||||
termsCache = {},
|
||||
postsCache = {},
|
||||
transformedPostsCache = {};
|
||||
|
||||
Module._cachedQuery = _.memoize(function(args) {
|
||||
return MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
action: args.action,
|
||||
data: args.options || {},
|
||||
});
|
||||
}, JSON.stringify);
|
||||
|
||||
Module.getPostTypes = function() {
|
||||
if (!postTypesCache) {
|
||||
postTypesCache = MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
return Module._cachedQuery({
|
||||
action: 'getPostTypes',
|
||||
data: {},
|
||||
options: {},
|
||||
}).then(function(types) {
|
||||
return _.values(types);
|
||||
});
|
||||
}
|
||||
|
||||
return postTypesCache;
|
||||
};
|
||||
|
||||
Module.getTaxonomies = function(postType) {
|
||||
if (!taxonomiesCache[postType]) {
|
||||
taxonomiesCache[postType] = MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
return Module._cachedQuery({
|
||||
action: 'getTaxonomies',
|
||||
data: {
|
||||
options: {
|
||||
postType: postType,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return taxonomiesCache[postType];
|
||||
};
|
||||
|
||||
Module.getTerms = function(options) {
|
||||
var key = JSON.stringify(options);
|
||||
if (!termsCache[key]) {
|
||||
termsCache[key] = MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
return Module._cachedQuery({
|
||||
action: 'getTerms',
|
||||
data: options || {},
|
||||
options: options,
|
||||
});
|
||||
}
|
||||
|
||||
return termsCache[key];
|
||||
};
|
||||
|
||||
Module.getPosts = function(options) {
|
||||
var key = JSON.stringify(options);
|
||||
if (!postsCache[key]) {
|
||||
postsCache[key] = MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
return Module._cachedQuery({
|
||||
action: 'getPosts',
|
||||
data: options || {},
|
||||
options: options,
|
||||
});
|
||||
}
|
||||
|
||||
return postsCache[key];
|
||||
};
|
||||
|
||||
Module.getTransformedPosts = function(options) {
|
||||
var key = JSON.stringify(options);
|
||||
if (!transformedPostsCache[key]) {
|
||||
transformedPostsCache[key] = MailPoet.Ajax.post({
|
||||
endpoint: 'wordpress',
|
||||
return Module._cachedQuery({
|
||||
action: 'getTransformedPosts',
|
||||
data: options || {},
|
||||
options: options,
|
||||
});
|
||||
}
|
||||
|
||||
return transformedPostsCache[key];
|
||||
};
|
||||
|
||||
App.on('start', function(options) {
|
||||
|
@@ -15,6 +15,13 @@ define([
|
||||
describe('model', function () {
|
||||
var model;
|
||||
|
||||
before(function() {
|
||||
WordpressComponent.getPosts = function() {
|
||||
var deferred = jQuery.Deferred();
|
||||
return deferred;
|
||||
};
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
global.stubChannel(EditorApplication);
|
||||
global.stubConfig(EditorApplication);
|
||||
|
@@ -178,4 +178,146 @@ define([
|
||||
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