UNPKG

southbay

Version:

Cocoa-inspired key-value coding, binding, and observing for node

86 lines (78 loc) 2.52 kB
var $ = require('southbay'), vows = require('vows'), assert = require('assert'), model = {arrowToThe: 'knee'}, KVC = new $.Base(), // any setting of values in '2' will reflect in '3' // since they share a model. KVC2 = new $.Base(model), KVC3 = new $.Base(model); vows.describe('Southbay Key-Value Coding').addBatch({ 'A KVC Object': { topic: KVC, 'sets a proposed value internally': function(kvc) { kvc.setValue('foo'); assert.equal(kvc._proposedValue_, 'foo'); } }, 'Another KVC Object': { topic: KVC2, 'should not share the proposed value': function(kvc) { assert.isUndefined(kvc._proposedValue_); } }, 'The first KVC Object': { topic: KVC, 'sets and gets the value with the key': function(kvc) { kvc.setValue('eggs').forKey('spam'); assert.equal(kvc.valueForKey('spam'), 'eggs'); }, 'should have cleared the _proposedValue_': function(kvc) { assert.isUndefined(kvc._proposedValue_); }, 'should return undefined for non-existant keys': function(kvc) { assert.isUndefined(kvc.valueForKey('aabbcc')); } }, 'The second KVC Object': { topic: KVC2, 'should not share the key': function(kvc) { assert.isUndefined(kvc.valueForKey('spam')); } }, 'A third KVC Object': { topic: KVC3, 'is sharing a model with the second KVC Object': function(kvc) { assert.equal(kvc.valueForKey('arrowToThe'), KVC2.valueForKey('arrowToThe')); }, 'can affect change to the shared model': function(kvc) { kvc.setValue('-Da').forKey('Fus-Ro'); assert.equal(KVC2.valueForKey('Fus-Ro'), '-Da'); } }, 'All KVC Objects': { topic: KVC, 'should correctly use keypaths': function(kvc) { kvc.setValue('Jarrvaskr').forKeyPath('Skyrim.Whiterun.Companions'); assert.equal(kvc.valueForKeyPath('Skyrim.Whiterun.Companions'), 'Jarrvaskr'); }, 'should correctly use existing keypath parts': function(kvc) { kvc.setValue('Dragonsreach').forKeyPath('Skyrim.Whiterun.Palace'); assert.equal(kvc.valueForKeyPath('Skyrim.Whiterun.Palace'), 'Dragonsreach'); }, 'can set multiple keys and values via a dictionary': function(kvc) { kvc.setValuesForKeys({ Winterhold: 'magesCollege', Solitude: 'bluePalace', Falkreath: 'cemetery' }); assert.equal(kvc.valueForKey('Solitude'), 'bluePalace'); }, 'can fetch mutliple values for an array of keys': function(kvc) { assert.deepEqual(kvc.valuesForKeys(['Winterhold', 'Falkreath']), { Winterhold: 'magesCollege', Falkreath: 'cemetery' }); } } }).export(module);