UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

720 lines 19.3 kB
// Generated by github.com/steida/coffee2closure 900.1.18 suite('este.Collection', function() { var Child, ChildCollection, Collection, Model, arrangeChildAndItsCollection, arrangeCollectionWithItems, collection, model; Collection = este.Collection; Model = este.Model; Child = null; ChildCollection = null; collection = null; model = null; setup(function() { arrangeChildAndItsCollection(); collection = new Collection; return model = new Model; }); arrangeChildAndItsCollection = function() { Child = function() { return Model.apply(this, arguments); }; goog.inherits(Child, Model); Child.prototype.schema = { c: { meta: function() { return 'fok'; } } }; ChildCollection = function() { return Collection.apply(this, arguments); }; goog.inherits(ChildCollection, Collection); return ChildCollection.prototype.model = Child; }; arrangeCollectionWithItems = function() { collection.add({ 'a': 1, 'aa': 1.5 }); collection.add({ 'b': 2, 'bb': 2.5 }); return collection.add({ 'c': 3, 'cc': 3.5 }); }; suite('constructor', function() { return test('should allow inject json data', function() { var json; json = [ { a: 1 }, { b: 2 } ]; collection = new Collection(json); return assert.deepEqual(collection.toJson(true), json); }); }); suite('model property', function() { test('should wrap json (meta test included)', function() { var json; json = [ { a: 1 }, { b: 2 } ]; collection = new ChildCollection(json); assert.instanceOf(collection.at(0), Child); assert.equal(collection.at(0).get('a'), 1); assert.equal(collection.at(1).get('b'), 2); return assert.equal(collection.at(0).get('c'), 'fok'); }); test('should dispatch add event', function(done) { collection = new Collection; goog.events.listen(collection, 'add', function(e) { assert.instanceOf(e.added[0], Model); return done(); }); return collection.add({ a: 1 }); }); return test('toJson should serialize raw model', function() { var collectionJson, json; json = [ { id: 0, a: 'aa' }, { id: 1, b: 'bb' } ]; collection = new ChildCollection(json); collectionJson = collection.toJson(true); return assert.deepEqual(collectionJson, [ { id: 0, a: 'aa' }, { id: 1, b: 'bb' } ]); }); }); suite('add, remove and getLength', function() { return test('should work', function() { assert.equal(collection.getLength(), 0); collection.add(model); assert.equal(collection.getLength(), 1); assert.isFalse(collection.remove(new Model), 'should not remove not existing model'); assert.isTrue(collection.remove(model)); return assert.equal(collection.getLength(), 0); }); }); suite('add item', function() { test('should fire add event', function() { var addCalled, added; addCalled = false; added = null; goog.events.listen(collection, 'add', function(e) { added = e.added; return addCalled = true; }); collection.add(model); assert.isTrue(addCalled); return assert.deepEqual(added, [model]); }); test('should fire update event', function(done) { goog.events.listen(collection, 'update', function(e) { return done(); }); return collection.add(model); }); test('should throw exception for model item with same id', function() { var called, e, error; called = false; collection = new ChildCollection; collection.add({ id: 1 }); try { collection.add({ id: 1 }); } catch (error) { e = error; called = true; } return assert.isTrue(called); }); return test('should not throw exception for model item with same id if item was removed', function() { var called, e, error; called = false; collection = new ChildCollection; collection.add({ id: 1 }); collection.remove(collection.at(0)); try { collection.add({ id: 1 }); } catch (error) { e = error; called = true; } return assert.isFalse(called); }); }); suite('add items', function() { return test('should fire add event', function() { var addCalled, added; addCalled = false; added = null; goog.events.listen(collection, 'add', function(e) { added = e.added; return addCalled = true; }); collection.add([model]); assert.isTrue(addCalled); return assert.deepEqual(added, [model]); }); }); suite('remove item', function() { test('should fire remove event', function() { var removeCalled, removed; removeCalled = false; removed = null; collection.add(model); goog.events.listen(collection, 'remove', function(e) { removed = e.removed; return removeCalled = true; }); collection.remove(model); assert.isTrue(removeCalled, 'removeCalled'); return assert.deepEqual(removed, [model]); }); test('should fire update event', function(done) { collection.add(model); goog.events.listen(collection, 'update', function(e) { return done(); }); return collection.remove(model); }); return test('should not fire remove event', function() { var removeCalled; removeCalled = false; goog.events.listen(collection, 'remove', function() { return removeCalled = true; }); collection.remove(model); return assert.isFalse(removeCalled); }); }); suite('remove item', function() { test('should fire remove event', function() { var removeCalled, removed; removeCalled = false; removed = null; collection.add(model); goog.events.listen(collection, 'remove', function(e) { removed = e.removed; return removeCalled = true; }); collection.remove([model]); assert.isTrue(removeCalled, 'removeCalled'); return assert.deepEqual(removed, [model]); }); return test('should not fire remove, change events', function() { var changeCalled, removeCalled; removeCalled = changeCalled = false; goog.events.listen(collection, 'remove', function() { return removeCalled = true; }); goog.events.listen(collection, 'change', function() { return changeCalled = true; }); collection.remove(model); assert.isFalse(removeCalled); return assert.isFalse(changeCalled); }); }); suite('contains', function() { return test('should return true if obj is present', function() { assert.isFalse(collection.contains(model)); collection.add(model); return assert.isTrue(collection.contains(model)); }); }); suite('removeIf', function() { return test('should remove item', function() { collection.add(model); assert.isTrue(collection.contains(model)); collection.removeIf(function(item) { return item === model; }); return assert.isFalse(collection.contains(model)); }); }); suite('at', function() { return test('should return item by index', function() { collection.add(model); return assert.equal(collection.at(0), model); }); }); suite('toArray', function() { return test('should return inner array', function() { collection.add(model); return assert.deepEqual(collection.toArray(), [model]); }); }); suite('toJson', function() { test('should return inner array', function() { collection.add(model); return assert.deepEqual(collection.toJson(true), [{}]); }); return test('should pass noMetas to model toJson method', function(done) { collection = new Collection; collection.add({ 'a': 1 }); collection.at(0).toJson = function(noMetas) { assert.isTrue(noMetas); return done(); }; return collection.toJson(true); }); }); suite('bubbling events', function() { test('from inner model should work', function() { var called, innerModel; called = 0; innerModel = new Model; collection.add(innerModel); goog.events.listen(collection, 'change', function(e) { return called++; }); innerModel.set('1', 1); assert.equal(called, 1); collection.remove(innerModel); assert.equal(called, 1); innerModel.set('1', 2); return assert.equal(called, 1); }); return test('from inner model (within other collection) should work', function() { var called, collection2, innerModel; called = 0; innerModel = new Model; collection.add(innerModel); collection2 = new Collection; collection2.add(innerModel); goog.events.listen(collection, 'change', function(e) { return called++; }); innerModel.set('1', 1); assert.equal(called, 1); collection.remove(innerModel); assert.equal(called, 1); innerModel.set('1', 2); return assert.equal(called, 1); }); }); suite('find', function() { return test('should find item', function() { var found; collection.add([ { a: 1 }, { b: 2 } ]); found = collection.find(function(item) { return item.get('a') === 1; }); assert.equal(found.get('a'), 1); found = collection.find(function(item) { return item.get('b') === 2; }); assert.equal(found.get('b'), 2); found = collection.find(function(item) { return item.get('b') === 3; }); return assert.isNull(found); }); }); suite('findById', function() { return test('should find item by id', function() { var found; collection.add([ { id: 1 }, { id: 2 } ]); found = collection.findById(1); assert.equal(found.getId(), 1); found = collection.findById(2); assert.equal(found.getId(), 2); found = collection.findById(3); return assert.isNull(found); }); }); suite('findByClientId', function() { return test('should find item by _cid', function() { var found; collection.add([ { id: 1, _cid: ':1' }, { id: 2, _cid: ':2' } ]); found = collection.findByClientId(':1'); assert.equal(found.getId(), 1); found = collection.findByClientId(':2'); assert.equal(found.getId(), 2); found = collection.findByClientId(':3'); return assert.isNull(found); }); }); suite('add object into collection', function() { return test('should work', function() { var child; collection = new ChildCollection; child = new Child; child.set('a', 1); collection.add(child); assert.instanceOf(collection.at(0), Child); return assert.equal(collection.at(0).get('a'), 1); }); }); suite('clear', function() { return test('should works', function() { var count; count = 0; collection = new Collection([model, new Model]); goog.events.listen(collection, 'remove', function() { return count++; }); collection.clear(); assert.equal(count, 1); assert.isUndefined(collection.at(0)); return assert.isUndefined(collection.at(1)); }); }); suite('sort', function() { test('should fire sort event', function(done) { goog.events.listen(collection, 'sort', function(e) { return done(); }); return collection.sort(); }); return test('should fire update event', function(done) { goog.events.listen(collection, 'update', function(e) { return done(); }); return collection.sort(); }); }); suite('sort by', function() { test('should sort collection', function() { collection.add({ id: 1 }); collection.add({ id: 2 }); collection.add({ id: 3 }); collection.sort({ by: function(item) { return item.id; } }); return assert.deepEqual(collection.toJson(true), [ { id: 1 }, { id: 2 }, { id: 3 } ]); }); return test('should sort added items', function() { collection.sort({ by: function(item) { return item.getId(); } }); collection.add({ id: 3 }); collection.add({ id: 2 }); collection.add({ id: 1 }); return assert.deepEqual(collection.toJson(true), [ { id: 1 }, { id: 2 }, { id: 3 } ]); }); }); suite('sort by, reversed', function() { test('should sort collection', function() { collection.add({ id: 1 }); collection.add({ id: 2 }); collection.add({ id: 3 }); collection.sort({ reversed: true, by: function(item) { return item.getId(); } }); return assert.deepEqual(collection.toJson(true), [ { id: 3 }, { id: 2 }, { id: 1 } ]); }); return test('should sort added items', function() { collection.sort({ reversed: true, by: function(item) { return item.getId(); } }); collection.add({ id: 3 }); collection.add({ id: 2 }); collection.add({ id: 1 }); return assert.deepEqual(collection.toJson(true), [ { id: 3 }, { id: 2 }, { id: 1 } ]); }); }); suite('filter', function() { test('should filter by function', function() { var filtered; arrangeCollectionWithItems(); filtered = collection.filter(function(item) { return item.get('a') === 1; }, true); assert.deepEqual(filtered, [ { 'a': 1, 'aa': 1.5 } ]); filtered = collection.filter(function(item) { return item.get('a') === 2; }, true); assert.deepEqual(filtered, []); filtered = collection.filter(function(item) { return item.get('a') === 1 || item.get('bb') === 2.5; }, true); return assert.deepEqual(filtered, [ { 'a': 1, 'aa': 1.5 }, { 'b': 2, 'bb': 2.5 } ]); }); return test('should filter by object', function() { var filtered; arrangeCollectionWithItems(); filtered = collection.filter({ 'a': 1 }, true); assert.deepEqual(filtered, [ { 'a': 1, 'aa': 1.5 } ]); filtered = collection.filter({ 'a': 2 }, true); assert.deepEqual(filtered, []); filtered = collection.filter({ 'bb': 2.5 }, true); return assert.deepEqual(filtered, [ { 'b': 2, 'bb': 2.5 } ]); }); }); suite('each', function() { return test('should call passed callback with each collection model', function() { var items; collection = new Collection; arrangeCollectionWithItems(); items = []; collection.each(function(item) { return items.push(item.toJson(true)); }); return assert.deepEqual(items, [ { 'a': 1, 'aa': 1.5 }, { 'b': 2, 'bb': 2.5 }, { 'c': 3, 'cc': 3.5 } ]); }); }); suite('multiple parent event propagation', function() { return test('should work', function() { var calls, collection1, collection2; collection1 = new Collection; collection2 = new Collection; model = new Model; collection1.add(model); collection2.add(model); calls = ''; goog.events.listen(collection1, 'change', function() { return calls += 'col1called'; }); goog.events.listen(collection2, 'change', function() { return calls += 'col2called'; }); model.set('foo', 'bla'); assert.equal(calls, 'col1calledcol2called'); calls = ''; goog.events.listen(collection1, 'update', function() { return calls += 'col1called'; }); goog.events.listen(collection2, 'update', function() { return calls += 'col2called'; }); model.set('foo', 'fok'); return assert.equal(calls, 'col1calledcol2calledcol1calledcol2called'); }); }); suite('getUrl', function() { test('should return associated model url if collection url is empty', function() { return assert.equal(collection.getUrl(), '/models'); }); test('should return string url', function() { collection.url = '/todos'; return assert.equal(collection.getUrl(), '/todos'); }); return test('should return function url', function() { collection.url = function() { return '/todos'; }; return assert.equal(collection.getUrl(), '/todos'); }); }); suite('reset', function() { return test('should replace a collection', function() { collection.add({ 'a': 1, 'aa': 1.5 }); assert.equal(collection.getLength(), 1); collection.reset({ 'b': 2, 'bb': 2.5 }); assert.equal(collection.getLength(), 1); return assert.deepEqual(collection.toJson(true), [ { 'b': 2, 'bb': 2.5 } ]); }); }); return suite('validate', function() { test('should validate collection items', function() { var a, b, errors; collection.add(1); collection.add(null); a = new este.Model; a.schema = { 'title': { 'validators': [este.validators.required()] } }; b = new este.Model; b.schema = { 'title': { 'validators': [este.validators.required()] } }; collection.add(a); collection.add(b); errors = collection.validate(); assert.lengthOf(errors, 2); collection.clear(); errors = collection.validate(); return assert.isNull(errors); }); return test('should be validable on model', function() { var a, b, errors; a = new este.Model; collection = new este.Collection; a.set('collection', collection); b = new este.Model; b.schema = { 'title': { 'validators': [este.validators.required()] } }; collection.add(b); errors = a.validate(); return assert.lengthOf(errors, 1); }); }); });