micro-model
Version:
Model that can be used on both Client & Server
82 lines (70 loc) • 2.79 kB
text/coffeescript
global.p = (args...) -> console.log args...
expect = require('chai').expect
MicroModel = require '../micromodel'
Model = MicroModel.Model
withModel = MicroModel.withModel
Collection = MicroModel.Collection
withCollection = MicroModel.withCollection
withEventEmitter = MicroModel.withEventEmitter
_ = require 'underscore'
klass = MicroModel.klass
describe "Collection", ->
beforeEach ->
= klass 'Unit', withModel, withEventEmitter
= new name: 'Probe', id: 'probe'
= new name: 'SCV'
it "should add, delete and get by id, cid and index", ->
units = new Collection
expect(units).to.have.length 1
units.add
expect(units).to.have.length 2
expect(units.at(0)).to.eql
expect(units.at(1)).to.eql
expect(units.get(.id)).to.eql
expect(units.get(._cid)).to.eql
expect(units.get(._cid)).to.eql
units.delete
units.delete
expect(units).to.have.length 1
units.delete
expect(units).to.have.length 0
it "should not add twice", ->
units = new Collection ,
units.add
expect(units).to.have.length 1
it "should check for equality", ->
group1 = new Collection
group2 = new Collection
group3 = new Collection
expect(group1.eql(group2)).to.equal true
expect(group1.eql(group3)).to.equal false
it "should compare with non collections", ->
group = new Collection
expect(group.eql(1)).to.equal false
expect(group.eql(null)).to.equal false
expect(group.eql({})).to.equal false
expect(group.eql([])).to.equal false
it "should emit add, delete and change events", ->
Units = klass 'Units', withCollection, withEventEmitter
group = new Units()
events = []
group.on 'add', (model) -> events.push "add #{model.name}"
group.on 'delete', (model) -> events.push "delete #{model.name}"
group.on 'change', (model) -> events.push 'change'
group.add
group.add
group.add
group.delete
expect(events).to.eql ['add Probe', 'change', 'add SCV', 'change', 'delete Probe', 'change']
it "should proxy model events", ->
Units = klass 'Units', withCollection, withEventEmitter
group = new Units()
events = []
group.addListener 'model:change', (model) -> events.push "change #{model.name}"
group.add
.set race: 'Protoss'
expect(events).to.eql ['change Probe']
it "should convert to JSON", ->
group = new Collection
expect(JSON.parse(JSON.stringify(group))).to.eql [{id: 'probe', name: 'Probe'}]
it "should sort and preserve order"