UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

216 lines 6.13 kB
// Generated by github.com/steida/coffee2closure 900.1.18 suite('este.storage.Local', function() { var Local, Model, collection, idFactory, local, mechanism, model, root; Local = este.storage.Local; Model = este.Model; root = null; mechanism = null; idFactory = null; model = null; collection = null; local = null; setup(function() { root = ''; mechanism = { set: function(key, value) { return this[key] = value; }, get: function(key) { return this[key]; }, remove: function(key) { return delete this[key]; } }; idFactory = function() { return 'someUniqueId'; }; model = new Model; collection = { getModel: function() { return Model; }, getUrl: function() { return 'Model::url'; }, add: function() {}, clear: function() {}, reset: function() {} }; local = new Local(root, '', mechanism, idFactory); return local.version = ''; }); suite('constructor', function() { return test('should work', function() { return assert.instanceOf(local, Local); }); }); suite('save', function() { test('should assign id for model without id', function() { local.save(model); return assert.equal(model.getId(), 'someUniqueId'); }); test('should store json to mechanism', function(done) { mechanism.set = function(key, value) { assert.equal(key, '/models'); assert.equal(value, '{"someUniqueId":{"foo":"bla","id":"fok"}}'); return done(); }; model.toJson = function(raw) { assert.isTrue(raw); return { foo: 'bla', id: 'fok' }; }; return local.save(model); }); return test('should return success result', function(done) { var result; result = local.save(model); return goog.result.waitOnSuccess(result, function(value) { assert.equal(value, model); return done(); }); }); }); suite('load', function() { test('should mechanism.get model', function(done) { var getKey; getKey = null; mechanism.get = function(key) { assert.equal(key, '/models'); return done(); }; model.id = '123'; return local.load(model); }); test('should load model', function(done) { mechanism.get = function(key) { assert.equal(key, '/models'); return '{"123":{"foo":"bla"}}'; }; model.id = '123'; model.set = function(json) { assert.deepEqual(json, { foo: 'bla' }); return done(); }; return local.load(model); }); test('should return success result', function(done) { var result; mechanism.get = function(key) { return '{"123":{"foo":"bla"}}'; }; model.id = '123'; result = local.load(model); return goog.result.waitOnSuccess(result, function(value) { assert.equal(value, model); assert.equal(value.getId(), '123'); return done(); }); }); test('should return error result if storage does not exists', function(done) { var result; mechanism.get = function(key) { return ''; }; model.id = '123'; result = local.load(model); return goog.result.waitOnError(result, function() { return done(); }); }); return test('should return error result if storage item does not exists', function(done) { var result; mechanism.get = function(key) { return '{"123":{"foo":"bla"}}'; }; model.id = '789'; result = local.load(model); return goog.result.waitOnError(result, function() { return done(); }); }); }); suite('remove', function() { test('should remove model from storage', function(done) { mechanism.get = function(key) { return '{"123":{"foo":"bla"}}'; }; mechanism.remove = function(key) { assert.equal(key, '/models'); return done(); }; model.id = '123'; return local.remove(model); }); test('should return success result with id', function(done) { var result; mechanism.get = function(key) { return '{"123":{"foo":"bla"}}'; }; model.id = '123'; result = local.remove(model); return goog.result.waitOnSuccess(result, function(value) { assert.equal(value, model); assert.equal(value.getId(), '123'); return done(); }); }); test('should return error result if storage does not exists', function(done) { var result; mechanism.get = function(key) { return ''; }; model.id = '456'; result = local.remove(model); return goog.result.waitOnError(result, function() { return done(); }); }); return test('should return error result if item does not exists', function(done) { var result; mechanism.get = function(key) { return '{"123":{"foo":"bla"}}'; }; model.id = '456'; result = local.remove(model); return goog.result.waitOnError(result, function() { return done(); }); }); }); return suite('query', function() { test('should load collection', function(done) { mechanism.get = function(key) { return '{"123":{"foo":"bla"},"456":{"bla":"foo"}}'; }; collection.reset = function(array) { assert.deepEqual(array, [ { foo: 'bla' }, { bla: 'foo' } ]); return done(); }; return local.query(collection); }); return test('should return success result with params', function(done) { var params, result; mechanism.get = function(key) { return '{"123":{"foo":"bla"},"456":{"bla":"foo"}}'; }; params = {}; result = local.query(collection, params); return goog.result.waitOnSuccess(result, function(value) { assert.equal(value, collection); return done(); }); }); }); });