Skip to content

Commit

Permalink
Merge pull request #320 from elasticsales/collection-set-vs-reset
Browse files Browse the repository at this point in the history
Two tests (1 failing) showing Collection reset/set should both link related objects
  • Loading branch information
PaulUithol committed Apr 9, 2013
2 parents 9b52eb2 + 4b868c6 commit c31ea90
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,30 @@ $(document).ready(function() {
}
});

window.Country = Backbone.RelationalModel.extend();
window.CountryCollection = Backbone.Collection.extend({
model: Country
});

window.Address = Backbone.RelationalModel.extend({
urlRoot: '/address/',

relations: [{
type: Backbone.HasOne,
key: 'country',
keySource: 'country_id',
relatedModel: 'Country'
}
],

initialize: function(attributes, options) {
// initialize an empty object if we don't already have one
// as an alternative to fetchRelated()
if ( attributes.country_id && !this.get('country') ) {
this.set('country', Country.findOrCreate( { id: attributes.country_id } ));
}
},

toString: function() {
return 'Address (' + this.id + ')';
}
Expand Down Expand Up @@ -1238,6 +1259,48 @@ $(document).ready(function() {
ok( contact === contacts.first(), '... and same model instances' );
});

test( "object gets related object linked properly when loaded using Collection#set", function() {
var countries = new CountryCollection();

// create address
var address = new Address({
id: 'address-1',
country_id: 'US'
});
var country = address.get( 'country' );
ok( country, 'address country exists' );
ok( country.id, '... with valid id' );
ok( !country.has('name'), '... without a name yet' );
ok( country instanceof Country, '... and is instanceof Country' );

// then countries fetch completes... if using SET
countries.set( [ { id: 'US', name: 'United States' } ] );
var country2 = address.get( 'country' );
ok( country === country2, 'after reset, address country is the same instance' );
equal( country.get('name'), 'United States', '... but now it has all the attributes' );
});

test( "object gets related object linked properly when loaded using Collection#reset", function() {
var countries = new CountryCollection();

// create address
var address = new Address({
id: 'address-1',
country_id: 'US'
});
var country = address.get( 'country' );
ok( country, 'address country exists' );
ok( country.id, '... with valid id' );
ok( !country.has('name'), '... without a name yet' );
ok( country instanceof Country, '... and is instanceof Country' );

// then countries fetch completes... if using RESET
countries.reset( [ { id: 'US', name: 'United States' } ] );
var country2 = address.get( 'country' );
ok( country === country2, 'after reset, address country is the same instance' );
equal( country.get('name'), 'United States', '... but now it has all the attributes' );
});

test( "constructor.findOrCreate", function() {
var personColl = Backbone.Relational.store.getCollection( person1 ),
origPersonCollSize = personColl.length;
Expand Down

0 comments on commit c31ea90

Please sign in to comment.