editor: Update block defaults for ALC changes

This commit is contained in:
Rostislav Wolny
2018-03-12 15:20:58 +01:00
parent 5e35a724f0
commit 8582c19ac0
2 changed files with 32 additions and 5 deletions

View File

@ -109,10 +109,10 @@ define([
},
initialize: function () {
base.BlockView.prototype.initialize.apply(this, arguments);
this.on('change:amount change:contentType change:terms change:inclusionType change:displayType change:titleFormat change:featuredImagePosition change:titleAlignment change:titleIsLink change:imageFullWidth change:showAuthor change:authorPrecededBy change:showCategories change:categoriesPrecededBy change:readMoreType change:readMoreText change:sortBy change:showDivider', this._scheduleFetchPosts, this);
this.listenTo(this.get('readMoreButton'), 'change', this._scheduleFetchPosts);
this.listenTo(this.get('divider'), 'change', this._scheduleFetchPosts);
this.on('add remove update reset', this._scheduleFetchPosts);
this.on('change:amount change:contentType change:terms change:inclusionType change:displayType change:titleFormat change:featuredImagePosition change:titleAlignment change:titleIsLink change:imageFullWidth change:showAuthor change:authorPrecededBy change:showCategories change:categoriesPrecededBy change:readMoreType change:readMoreText change:sortBy change:showDivider', this._handleChanges, this);
this.listenTo(this.get('readMoreButton'), 'change', this._handleChanges);
this.listenTo(this.get('divider'), 'change', this._handleChanges);
this.on('add remove update reset', this._handleChanges);
this.on('refreshPosts', this.updatePosts, this);
},
updatePosts: function (posts) {
@ -122,7 +122,8 @@ define([
* Batch more changes during a specific time, instead of fetching
* ALC posts on each model change
*/
_scheduleFetchPosts: function () {
_handleChanges: function () {
this._updateDefaults();
App.getChannel().trigger('automatedLatestContentRefresh');
}
});

View File

@ -73,6 +73,7 @@ define([
describe('model', function () {
var model;
var module;
var sandbox;
before(function () {
module = AutomatedLatestContentBlock;
@ -83,11 +84,13 @@ define([
global.stubConfig(EditorApplication);
EditorApplication.getBlockTypeModel = sinon.stub().returns(Backbone.SuperModel);
model = new (module.AutomatedLatestContentBlockModel)();
sandbox = sinon.sandbox.create();
});
afterEach(function () {
delete EditorApplication.getChannel;
delete EditorApplication.getBlockTypeModel;
sandbox.restore();
});
it('has automatedLatestContent type', function () {
@ -265,6 +268,29 @@ define([
expect(model.get('_container.blocks').size()).to.equal(1);
expect(model.get('_container.blocks').first().get('type')).to.equal('someCustomType');
});
it('updates blockDefaults.automatedLatestContent when handling changes', function () {
var stub = sandbox.stub(EditorApplication.getConfig(), 'set');
model.set('amount', '17');
model.set('contentType', 'mailpoet_page');
model.set('terms', []);
model.set('inclusionType', 'exclude');
model.set('displayType', 'full');
model.set('titleFormat', 'h3');
model.set('titleAlignment', 'right');
model.set('titleIsLink', true);
model.set('imageFullWidth', true);
model.set('featuredImagePosition', 'aboveTitle');
model.set('showAuthor', 'belowText');
model.set('authorPrecededBy', 'Custom config author preceded by');
model.set('showCategories', 'belowText');
model.set('categoriesPrecededBy', 'Custom config categories preceded by');
model.set('sortBy', 'oldest');
model.set('showDivider', false);
expect(stub.callCount).to.equal(16);
expect(stub.getCall(15).args[0]).to.equal('blockDefaults.automatedLatestContent');
expect(stub.getCall(15).args[1]).to.deep.equal(model.toJSON());
});
});
describe('block view', function () {