Created
June 26, 2015 06:15
-
-
Save rueckstiess/73add06f953607f759e5 to your computer and use it in GitHub Desktop.
tests for ampersand-collection-filterable with polymorphic collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'), | |
collectionFilterable = require('../ampersand-collection-filterable'), | |
AmpersandCollection = require('ampersand-collection'), | |
AmpersandState = require('ampersand-state'); | |
var Candy = AmpersandState.extend({ | |
props: { | |
id: 'number', | |
name: 'string' | |
} | |
}); | |
var Chocolate = Candy.extend({ | |
props: { | |
flavor: 'string' | |
} | |
}); | |
var Lollipop = Candy.extend({ | |
props: { | |
color: 'string', | |
values: ['red', 'green', 'blue', 'yellow'] | |
} | |
}); | |
var CandyCollection = AmpersandCollection.extend(collectionFilterable, { | |
model: function (attrs, options) { | |
if (attrs.color) { | |
return new Lollipop(attrs, options); | |
} | |
if (attrs.flavor) { | |
return new Chocolate(attrs, options); | |
} | |
return new Candy(attrs, options); | |
}, | |
isModel: function(model) { | |
return model instanceof Candy; | |
} | |
}); | |
var bag = new CandyCollection([ | |
{ | |
id: 1, | |
name: 'Reese\'s Egg' | |
}, | |
{ | |
id: 2, | |
name: 'Snickers', | |
flavor: 'Caramel' | |
}, | |
{ | |
id: 3, | |
name: 'Chupa Chups', | |
color: 'red' | |
}, | |
{ | |
id: 4, | |
name: 'Milky Way', | |
flavor: 'White Chocolate' | |
}, | |
{ | |
id: 5, | |
name: 'Twix' | |
} | |
]); | |
describe('ampersand-collection-filterable', function() { | |
it('should not puke', function() { | |
bag._initFilters(); | |
bag.add({ | |
id: 7, | |
name: 'Reese\'s Peanut Butter Cup' | |
}); | |
bag.remove(7); | |
}); | |
it('should filter out candy that does not start with `T`', function(done) { | |
bag.once('change:length', function(newLength) { | |
assert.equal(newLength, 1); | |
done(); | |
}); | |
bag.filter(function(candy) { | |
return candy.name.charAt(0) === 'T'; | |
}); | |
}); | |
it('should maintain the filter if another candy added', function(done) { | |
bag.add({ | |
id: 6, | |
name: 'Peppermint Patty' | |
}); | |
setTimeout(function() { | |
assert.equal(bag.models.length, 2); | |
assert.equal(bag._filtered.length, 4); | |
done(); | |
}, 100); | |
}); | |
it('should handle indirect removes', function(done) { | |
bag.remove(5); | |
setTimeout(function() { | |
assert.equal(bag.models.length, 1); | |
assert.equal(bag._filtered.length, 4); | |
done(); | |
}, 100); | |
}); | |
it('should add back all candies when unfiltered', function(done) { | |
bag.once('change:length', function(newLength) { | |
assert.equal(newLength, 5); | |
done(); | |
}); | |
bag.unfilter(); | |
}); | |
it('should handle indirect resets', function(done) { | |
bag.reset(); | |
setTimeout(function() { | |
assert.equal(bag.models.length, 0); | |
assert.equal(bag._filtered.length, 0); | |
done(); | |
}, 100); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment