From 7d7e79751c805d0880bfc51ec61a00faa0d7354b Mon Sep 17 00:00:00 2001 From: Donovan Walker Date: Sat, 8 Apr 2017 01:30:17 -0500 Subject: [PATCH] Tests - buttoning up jsonMaps._ and jsonMaps..inputs..fn behaviors --- src/base.js | 56 ++++++++++---------- test/base.collection.dotpathischildtree.js | 3 ++ test/base.model.jsonmap.from.js | 61 ++++++++++++++++++++-- 3 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/base.js b/src/base.js index 54d4710..5141071 100644 --- a/src/base.js +++ b/src/base.js @@ -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. @@ -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]; diff --git a/test/base.collection.dotpathischildtree.js b/test/base.collection.dotpathischildtree.js index 0e0549f..6ca6f60 100644 --- a/test/base.collection.dotpathischildtree.js +++ b/test/base.collection.dotpathischildtree.js @@ -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(){ diff --git a/test/base.model.jsonmap.from.js b/test/base.model.jsonmap.from.js index 1d4890d..a1cdcc4 100644 --- a/test/base.model.jsonmap.from.js +++ b/test/base.model.jsonmap.from.js @@ -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..from - transform input {...} on 'new Base.Model({...}, )'", function() { @@ -15,7 +16,16 @@ describe("Base.Model.jsonMap..from - transform input {...} on 'new Base.Mo }, camelC:{ from:{ - convert:'toCamel' + convert:'toCamel', + } + }, + badFn:{ + from:{ + inputs:{ + c:{ + fn:'zorp' + } + } } } } @@ -43,6 +53,16 @@ describe("Base.Model.jsonMap..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(){ @@ -195,7 +215,42 @@ describe("Base.Model.jsonMap..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); + }); }); }); \ No newline at end of file