Skip to content

Commit

Permalink
Add event related tests for previousAttributes()
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardograca committed Nov 27, 2018
1 parent 54554b7 commit 3cdfa6e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/integration/model.js
Expand Up @@ -1583,6 +1583,17 @@ module.exports = function(bookshelf) {
});
});

it("returns the current model's attributes if no attributes were changed after save", function() {
return new Models.Site({id: 1})
.fetch()
.then(function(site) {
return site.save({name: site.get('name')});
})
.then(function(site) {
expect(site.previousAttributes()).to.eql(site.attributes);
});
});

it("returns the model's original attributes if the model has changed", function() {
return new Models.Site({id: 1}).fetch().then(function(site) {
var originalAttributes = _.clone(site.attributes);
Expand Down Expand Up @@ -1610,6 +1621,38 @@ module.exports = function(bookshelf) {
});
});

it("returns the model's original attributes after save on the 'updated' event", function(done) {
var originalAttributes;
var siteModel = new Models.Site({id: 1});

siteModel.on('updated', function(site) {
expect(site.previousAttributes()).to.eql(originalAttributes);
expect(site.previousAttributes()).to.not.eql(site.attributes);

new Models.Site({id: 1}).save({name: originalAttributes.name}).finally(() => done());
});

siteModel.fetch().then(function(site) {
originalAttributes = _.clone(site.attributes);
return siteModel.save({name: 'Blah'});
});
});

it("returns the current model's attributes after save without changes on the 'updated' event", function(done) {
var originalAttributes;
var siteModel = new Models.Site({id: 1});

siteModel.on('updated', function(site) {
expect(site.previousAttributes()).to.eql(site.attributes);
new Models.Site({id: 1}).save({name: originalAttributes.name}).finally(() => done());
});

siteModel.fetch().then(function(site) {
originalAttributes = _.clone(site.attributes);
return siteModel.save({name: site.get('name')});

This comment has been minimized.

Copy link
@kirrg001

kirrg001 Dec 3, 2018

Contributor

Maybe it makes a difference if you do

  • siteModel.set('name, ...)
  • siteModel.save()

?

This comment has been minimized.

Copy link
@ricardograca

ricardograca Dec 4, 2018

Author Member

Doesn't make a difference. I also tried using siteModel in the expectation above and it also passed.

This comment has been minimized.

Copy link
@kirrg001

kirrg001 Dec 4, 2018

Contributor

Hm so weird. Then there must be something else in Ghost which is different to the test suite 🤔

});
});

it("returns the model's original attributes after destroy", function() {
var originalAttributes;

Expand Down

0 comments on commit 3cdfa6e

Please sign in to comment.