Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add unit tests for ModelSync.REST.

Add tests for this modules public API sans its `sync()` method. Testing
the `sync()` method is more involved and will be as a separate commit.
  • Loading branch information...
commit 44581e06e358b31578c14735d5ddeee8134b5329 1 parent d3358f8
Eric Ferraiuolo ericf authored
Showing with 204 additions and 110 deletions.
  1. +3 −0  src/app/tests/app.html
  2. +201 −110 src/app/tests/model-sync-rest-test.js
3  src/app/tests/app.html
View
@@ -14,6 +14,9 @@
<script src="../../../build/yui/yui.js"></script>
<script>
+ YUI.Env.CSRF_TOKEN = 'asdf1234';
+</script>
+<script>
var Y = YUI({
allowRollup: false,
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'raw',
311 src/app/tests/model-sync-rest-test.js
View
@@ -15,190 +15,281 @@ modelSyncRESTSuite = new Y.Test.Suite('ModelSync.REST');
// -- ModelSync.REST: Lifecycle ------------------------------------------------
modelSyncRESTSuite.add(new Y.Test.Case({
- name : 'Lifecycle',
+ name: 'Lifecycle',
- setUp : function () {
- this.TestModel = Y.Base.create('testModel', Y.Model, [Y.ModelSync.REST]);
- this.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
- model : this.TestModel
+ setUp: function () {
+ Y.TestModel = Y.Base.create('customModel', Y.Model, [Y.ModelSync.REST]);
+
+ Y.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
+ model: Y.TestModel
});
},
- tearDown : function () {
- delete this.TestModel;
- delete this.TestModelList;
+ tearDown: function () {
+ delete Y.TestModel;
+ delete Y.TestModelList;
},
- 'initializer should set local `url` property' : function () {
- var model = new this.TestModel({ url: '/model/123' });
- Assert.areSame('/model/123', model.url);
+ 'initializer should set the `root` property on the instance': function () {
+ var model = new Y.TestModel({root: '/model/'}),
+ modelList = new Y.TestModelList({root: '/list/'});
+
+ Assert.areSame('/model/', model.root);
+ Assert.areSame('/list/', modelList.root);
+ },
+
+ 'initializer should set the `url` property on the instance': function () {
+ var model = new Y.TestModel({url: '/model/123'}),
+ modelList = new Y.TestModelList({url: '/model'});
- var modelList = new this.TestModelList({ url: '/model' });
+ Assert.areSame('/model/123', model.url);
Assert.areSame('/model', modelList.url);
}
}));
// -- ModelSync.REST: Properties -----------------------------------------------
modelSyncRESTSuite.add(new Y.Test.Case({
- name : 'Properties',
+ name: 'Properties',
+
+ setUp: function () {
+ Y.TestModel = Y.Base.create('customModel', Y.Model, [Y.ModelSync.REST]);
- setUp : function () {
- this.TestModel = Y.Base.create('testModel', Y.Model, [Y.ModelSync.REST]);
- this.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
- model : this.TestModel
+ Y.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
+ model: Y.TestModel
});
},
- tearDown : function () {
- delete this.TestModel;
- delete this.TestModelList;
+ tearDown: function () {
+ delete Y.TestModel;
+ delete Y.TestModelList;
},
- '`root` property should be an empty string by default' : function () {
- var model = new this.TestModel();
- Assert.areSame('', model.root);
+ '`root` property should be an empty string by default': function () {
+ var model = new Y.TestModel(),
+ modelList = new Y.TestModelList();
- var modelList = new this.TestModelList();
+ Assert.areSame('', model.root);
Assert.areSame('', modelList.root);
},
- '`url` property should be an empty string by default' : function () {
- var model = new this.TestModel();
- Assert.areSame('', model.url);
+ '`url` property should be an empty string by default': function () {
+ var model = new Y.TestModel(),
+ modelList = new Y.TestModelList();
- var modelList = new this.TestModelList();
+ Assert.areSame('', model.url);
Assert.areSame('', modelList.url);
+ },
+
+ 'Static `CSRF_TOKEN` should default to the value of `YUI.Env.CSRF_TOKEN`': function () {
+ Assert.areSame('asdf1234', YUI.Env.CSRF_TOKEN);
+ Assert.areSame(YUI.Env.CSRF_TOKEN, Y.ModelSync.REST.CSRF_TOKEN);
}
}));
// -- ModelSync.REST: Methods --------------------------------------------------
modelSyncRESTSuite.add(new Y.Test.Case({
- name : 'Methods',
+ name: 'Methods',
- setUp : function () {
- this.TestModel = Y.Base.create('testModel', Y.Model, [Y.ModelSync.REST]);
- this.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
- model : this.TestModel
+ setUp: function () {
+ Y.TestModel = Y.Base.create('customModel', Y.Model, [Y.ModelSync.REST]);
+
+ Y.TestModelList = Y.Base.create('testModelList', Y.ModelList, [Y.ModelSync.REST], {
+ model: Y.TestModel
});
},
- tearDown : function () {
- delete this.TestModel;
- delete this.TestModelList;
+ tearDown: function () {
+ delete Y.TestModel;
+ delete Y.TestModelList;
},
- 'getURL() should return a String' : function () {
- var model = new this.TestModel();
- Assert.isString(model.getURL());
+ 'getURL() should return an empty string by default': function () {
+ var model = new Y.TestModel(),
+ modelList = new Y.TestModelList();
- var modelList = new this.TestModelList();
+ Assert.isString(model.getURL());
Assert.isString(modelList.getURL());
- },
- 'getURL() should return locally set `url` property' : function () {
- var model = new this.TestModel({ url: '/model/123' });
- Assert.areSame('/model/123', model.getURL());
+ Assert.areSame('', model.getURL());
+ Assert.areSame('', modelList.getURL());
+ },
- model.url = '/model/abc';
- Assert.areSame('/model/abc', model.getURL());
+ 'getURL() of a model list should return the `root` of its model by default': function () {
+ Y.TestModel.prototype.root = '/root/';
- var modelList = new this.TestModelList({ url: '/model' });
- Assert.areSame('/model', modelList.getURL());
+ var modelList = new Y.TestModelList();
- modelList.url = '/models';
- Assert.areSame('/models', modelList.getURL());
+ Assert.areSame('/root/', modelList.getURL());
},
- 'getURL() should substitute placeholder values of Models’ `url`' : function () {
- var model = new this.TestModel({
- id : 123,
- url: '/model/{id}/'
- });
+ 'getURL() of a model list should return its `url` if defined': function () {
+ Y.TestModel.prototype.root = '/root/';
+ Y.TestModelList.prototype.url = '/list/';
+
+ var modelList = new Y.TestModelList();
- Assert.areSame('/model/123/', model.getURL());
+ Assert.areSame('/list/', modelList.getURL());
- model.addAttr('foo', { value: 'bar' });
- model.url = '/{foo}/{id}';
- Assert.areSame('/bar/123', model.getURL());
+ modelList.url = '/users/';
+
+ Assert.areSame('/users/', modelList.getURL());
},
- 'getURL() should not substitute placeholder values of ModelLists’ `url`' : function () {
- var modelList = new this.TestModelList({ url: '/{foo}/' });
+ 'getURL() of a new model should return its `root` if defined': function () {
+ Y.TestModel.prototype.root = '/root/';
+
+ var model = new Y.TestModel();
+
+ Assert.isTrue(model.isNew());
+ Assert.areSame('/root/', model.getURL());
- modelList.addAttr('foo', { value: 'bar' });
- Assert.areSame('bar', modelList.get('foo'));
- Assert.areSame('/{foo}/', modelList.getURL());
+ model.root = '/users/';
+
+ Assert.areSame('/users/', model.getURL());
},
- 'getURL() should URL-encode the substitutions of placeholder values of Models’ `url`' : function () {
- var model = new this.TestModel({
- id : '123 456',
- url: '/model/{id}'
- });
+ 'getURL() of a model should return its `root` when the `action` is "create"': function () {
+ Y.TestModel.prototype.root = '/root/';
- Assert.areSame('/model/123%20456', model.getURL());
+ var model = new Y.TestModel({id: 1});
+
+ Assert.isFalse(model.isNew());
+ Assert.areSame('/root/', model.getURL('create'));
+
+ model.root = '/users/';
+
+ Assert.areSame('/users/', model.getURL('create'));
},
- 'getURL() should not substitute Arrays, Objects, or Boolean values of Models’ `url`' : function () {
- var model = new this.TestModel({
- id : 'asdf',
- url: '/model/{foo}/{bar}/{baz}/{id}'
- });
+ 'getURL() of a model should return its `root` joined with its `id` by default': function () {
+ Y.TestModel.prototype.root = '/root';
- model.addAttrs({
- foo : {value: [1, 2, 3]},
- bar : {value: {zee: 'zee'}},
- baz : {value: true}
- });
+ var model = new Y.TestModel({id: 1});
- Assert.areSame('/model/{foo}/{bar}/{baz}/asdf', model.getURL());
+ Assert.isFalse(model.isNew());
+ Assert.areSame('/root/1', model.getURL());
+
+ model.root = '/users';
+
+ Assert.areSame('/users/1', model.getURL());
},
- 'getURL() should return `root` if `url` is falsy' : function () {
- var model = new this.TestModel();
+ 'getURL() of a model should return its `root` joined with its `id` and normalize slashes': function () {
+ var model = new Y.TestModel({id: 1});
+
+ model.root = 'users';
+ Assert.areSame('users/1', model.getURL());
+
+ model.root = 'users/';
+ Assert.areSame('users/1/', model.getURL());
- model.root = '/model/';
- model.url = '';
+ model.root = '/users';
+ Assert.areSame('/users/1', model.getURL());
- Assert.areSame('/model/', model.getURL());
+ model.root = '/users/';
+ Assert.areSame('/users/1/', model.getURL());
},
- 'getURL() should return `root` if the Model is new' : function () {
- var model = new this.TestModel();
- model.root = '/model';
- model.url = '/foo';
- Assert.areSame(model.root, model.getURL());
+ 'getURL() of a model should return its `url` if defined': function () {
+ Y.TestModel.prototype.url = '/users/1';
+
+ var model = new Y.TestModel({id: 'foo'});
+
+ Assert.isFalse(model.isNew());
+ Assert.areSame('/users/1', model.getURL());
+
+ model.url = '/users/bar';
+
+ Assert.areSame('/users/bar', model.getURL());
},
- 'getURL() should return a URL that ends with a / only if Model’s `root` ends with a /' : function () {
- var model = new this.TestModel({id: 123});
+ 'getURL() should substitute tokenized `url`s with attribute values': function () {
+ var modelList = new Y.TestModelList({url: '/{type}/'}),
+ model;
+
+ modelList.addAttr('type', {value: 'users'});
- model.root = '/model';
- Assert.areSame('/model/123', model.getURL());
+ Assert.areSame('users', modelList.get('type'));
+ Assert.areSame('/users/', modelList.getURL());
- model.root = '/model/';
- Assert.areSame('/model/123/', model.getURL());
+ model = modelList.add({
+ id : 1,
+ type: modelList.get('type'),
+ url : '/{type}/items/{id}/'
+ });
+
+ Assert.areSame(1, modelList.size());
+ Assert.areSame('users', model.get('type'));
+ Assert.areSame('/users/items/1/', model.getURL());
+ },
+
+ 'getURL() should substitute tokenized `url`s with `options` values': function () {
+ var model = new Y.TestModel({url: '/{type}/foo/'}),
+ modelList = new Y.TestModelList({url: '/{type}/'});
+
+ Assert.areSame('/users/foo/', model.getURL(null, {type: 'users'}));
+ Assert.areSame('/users/', modelList.getURL(null, {type: 'users'}));
},
- 'getURL() should return a URL determined from the sync action' : function () {
- var model = new this.TestModel({id: 123});
+ 'getURL() should substitute tokenized `url`s with attribute and `options` values': function () {
+ var modelList = new Y.TestModelList({url: '/{type}/?num={num}'}),
+ model;
+
+ modelList.addAttr('type', {value: 'users'});
+
+ Assert.areSame('users', modelList.get('type'));
+ Assert.areSame('/users/?num=10', modelList.getURL(null, {num: 10}));
+
+ Assert.areSame('/losers/?num=10', modelList.getURL(null, {
+ num : 10,
+ type: 'losers'
+ }));
+
+ model = modelList.add({
+ id : 1,
+ url: '/{type}/items/{id}/'
+ });
- model.getURL = function (action) {
- return '/model/' + action;
- };
+ Assert.areSame(1, modelList.size());
+ Assert.areSame(1, model.get('id'));
+ Assert.areSame('/users/items/1/', model.getURL(null, {type: 'users'}));
- Assert.areSame('/model/read', model.getURL('read'));
+ Assert.areSame('/losers/items/foo/', model.getURL(null, {
+ id : 'foo',
+ type: 'losers'
+ }));
},
- 'serialize() can modify the data' : function () {
- var model = new this.TestModel({id: 123});
+ 'getURL() should URL-encode the `url` substitution values': function () {
+ var model = new Y.TestModel({
+ id : '123 456',
+ url: '/model/{id}'
+ });
+
+ Assert.areSame('/model/123%20456', model.getURL());
+ },
+
+ 'getURL() should not substitute Arrays, Objects, or Boolean values into the `url`' : function () {
+ var model = new Y.TestModel({
+ id : 'asdf',
+ url: '/model/{foo}/{bar}/{baz}/{id}'
+ });
+
+ model.addAttrs({
+ foo : {value: [1, 2, 3]},
+ bar : {value: {zee: 'zee'}},
+ baz : {value: true}
+ });
+
+ Assert.areSame('/model/{foo}/{bar}/{baz}/asdf', model.getURL());
+ },
- model.serialize = function() {
- var data = this.toJSON();
- return Y.JSON.stringify({body: data});
- };
+ 'serialize() should return a JSON string by default': function () {
+ var model = new Y.TestModel({id: 123});
- Assert.areSame(Y.JSON.stringify({body: {id: 123}}), model.serialize());
+ Assert.isString(model.serialize());
+ Assert.areSame(Y.JSON.stringify(model), model.serialize());
+ Assert.areSame(Y.JSON.stringify(model.toJSON()), model.serialize());
}
}));
Please sign in to comment.
Something went wrong with that request. Please try again.