Skip to content

Commit

Permalink
Tests - buttoning up jsonMaps._ and jsonMaps.<mode>.inputs.<attr>.fn …
Browse files Browse the repository at this point in the history
…behaviors
  • Loading branch information
relativityboy committed Apr 8, 2017
1 parent 6012ded commit 7d7e797
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 32 deletions.
56 changes: 27 additions & 29 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,17 @@ define([
options = {mode:options};
break;
case 'undefined' :
options = {mode:false};
options = {};
if(this.jsonMaps && this.jsonMaps.hasOwnProperty('_') && this.jsonMaps._.hasOwnProperty('from')) {
options.mode = '_';
}
}
if(this.jsonMaps.hasOwnProperty(options.mode)) {

if(options.mode) {
attributes = this.fromJSON(attributes, options.mode);
}
arguments[0] = attributes;
Backbone.Model.apply(this, arguments);

},
/**
* Compares the attributes of this model with another model, or json in the same structure.
Expand Down Expand Up @@ -586,38 +589,33 @@ define([
},

fromJSON:function(data, mode) {
var keys, map = {inputs:{}}, rsp = {}, rspAttrName, converter = function(val) { return val};;
if(!mode && this.jsonMaps.hasOwnProperty('_') && this.jsonMaps._.hasOwnProperty('from')) {
mode = '_';
}
if(mode) {
map = (this.jsonMaps[mode] && this.jsonMaps[mode].from) ? this.jsonMaps[mode].from : false;
if (!map) { //we want to be pretty strict here. All objects in the tree must know they're going to be called within a particular context.
throw new Error('fromJSON mode:' + mode + ' does not have a map (jsonMaps.' + mode + '.from) for all models in the tree being Jsonified');
}
map.inputs = (map.hasOwnProperty('inputs')) ? map.inputs : {};
var keys, map = {inputs:{}}, rsp = {}, rspAttrName, converter = function(val) { return val};

if (map.include) {
keys = map.include;
} else if (map.exclude) {
keys = [];
for (var key in data) if (data.hasOwnProperty(key)) {
if (map.exclude.indexOf(key) == -1) {
keys.push(key)
}
map = (this.jsonMaps[mode] && this.jsonMaps[mode].from) ? this.jsonMaps[mode].from : false;
if (!map) { //we want to be pretty strict here. All objects in the tree must know they're going to be called within a particular context.
throw new Error('fromJSON mode:' + mode + ' does not have a map (jsonMaps.' + mode + '.from) for all models in the tree being Jsonified');
}
map.inputs = (map.hasOwnProperty('inputs')) ? map.inputs : {};

if (map.include) {
keys = map.include;
} else if (map.exclude) {
keys = [];
for (var key in data) if (data.hasOwnProperty(key)) {
if (map.exclude.indexOf(key) == -1) {
keys.push(key)
}
} else {
keys = _.keys(data);
}

if (map.convert === 'toCamel') {
converter = toCamel;
} else if (map.convert == 'toUnderscored') {
converter = toUnderscored;
}
} else {
keys = _.keys(data);
}

if (map.convert === 'toCamel') {
converter = toCamel;
} else if (map.convert == 'toUnderscored') {
converter = toUnderscored;
}

_.each(keys, function(key) {
rspAttrName = (map.inputs[key] && map.inputs[key].attrName) ? map.inputs[key].attrName : converter(key);
rsp[rspAttrName] = (typeof data[key] === 'object')? converter(data[key]) : data[key];
Expand Down
3 changes: 3 additions & 0 deletions test/base.collection.dotpathischildtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ describe("Base.Model.dotPathIsChildTree=true - transform x.get/set('a.b.c') into
expect(mdl.get('m')).to.equal(updateValue2);
});
});
it("throws an error when attempting to set an array as a model",function() {
assert.throws(function() { aList.setOn('m', []);}, Error);
})

/////////////////////////////
it("can set a property on a model in a child collection where id is a string", function(){
Expand Down
61 changes: 58 additions & 3 deletions test/base.model.jsonmap.from.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Base = require('../src/base');
var expect = require("chai").expect;
var assert = require("chai").assert;
var _ = require('underscore');

describe("Base.Model.jsonMap.<mode>.from - transform input {...} on 'new Base.Model({...}, <mode>)'", function() {
Expand All @@ -15,7 +16,16 @@ describe("Base.Model.jsonMap.<mode>.from - transform input {...} on 'new Base.Mo
},
camelC:{
from:{
convert:'toCamel'
convert:'toCamel',
}
},
badFn:{
from:{
inputs:{
c:{
fn:'zorp'
}
}
}
}
}
Expand Down Expand Up @@ -43,6 +53,16 @@ describe("Base.Model.jsonMap.<mode>.from - transform input {...} on 'new Base.Mo
model = new Model(attributes, 'camelC');
expect(model.attributes.hasOwnProperty('objectTwo')).to.equal(true);
});
it("throws an error on undefined mode",function() {
assert.throws(function() {
new Model(attributes, 'xx');
}, Error);
});
it("throws an error on incorrect fn",function() {
assert.throws(function() {
new Model(attributes, 'badFn');
}, Error);
});
});
describe(".exclude", function() {
it("keeps specified attributes from being added to the model",function(){
Expand Down Expand Up @@ -195,7 +215,42 @@ describe("Base.Model.jsonMap.<mode>.from - transform input {...} on 'new Base.Mo
expect(model).to.equal(thisModel);
});
});
describe("jsonMap._.from automatically used if present and no mode is passed to the constructor", function() {
it("stub", function(){})
describe("jsonMap._.from", function() {
var attributes, model, Model;
before(function() {
Model = Base.Model.extend({
jsonMaps:{
_:{
from:{
inputs:{
objectOne:{
attrName:'goober'
}
}
}
}
}
});
});
beforeEach(function() {
attributes = {
b:1,
c:'apple',
objectOne:{
'pear':'fruit',
'banana':'more fruit'
},
object_two:{
'pear':'glim',
'banana':'grom'
},
jsonString1:'{"a":1,"gross":"hot dog"}'
};

model = new Model(attributes);
});
it("automatically used if present and no mode is passed to the constructor", function(){
expect(model.get('goober')).to.equal(attributes.objectOne);
});
});
});

0 comments on commit 7d7e797

Please sign in to comment.