Skip to content

Commit

Permalink
Try tree nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jamestalmage committed Feb 13, 2015
1 parent fd47c01 commit 6ce0317
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/TreeNode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

function TreeNode(){
this._callbacks = null;
this._children = {};
this._value = undefined;
this._pending_value = undefined;
}

require('./EventEmitter')(TreeNode.prototype);

TreeNode.prototype.setValue = function(value){
if(value === undefined) {
value = null;
}
this._pending_value = value;
return this._value !== this._pending_value;
};

TreeNode.prototype.flushChanges = function(){
if(this._value !== this._pending_value){
this._value = this._pending_value;
this.emit('value');
}
};

TreeNode.prototype.isInitialized = function(){
return this._value !== undefined;
};

module.exports = TreeNode;

184 changes: 184 additions & 0 deletions test/TreeNode-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
xdescribe('TreeNode',function(){

// Assertion Framework
var chai = require('chai');
var expect = chai.expect;

var sinon = require('sinon');
chai.use(require('sinon-chai'));


// Source Under Test
var TreeNode = require('../src/TreeNode');


// Test Initialization
var node,spy1,spy2,spy3;

beforeEach(function(){
node = new TreeNode();
spy1 = sinon.spy();
spy2 = sinon.spy();
spy3 = sinon.spy();
});


// TESTS

describe('#setValue',function(){
describe('returns true the first time it is called with a ',function(){
it('string',function(){
expect(node.setValue('foo')).to.equal(true);
});

it('false',function(){
expect(node.setValue(false)).to.equal(true);
});

it('true',function(){
expect(node.setValue(true)).to.equal(true);
});

it('0',function(){
expect(node.setValue(0)).to.equal(true);
});

it('1',function(){
expect(node.setValue(1)).to.equal(true);
});

it('null',function(){
expect(node.setValue(null)).to.equal(true);
});

it('object',function(){
expect(node.setValue({a:'a'})).to.equal(true);
});
});

describe('returns true if value is changed',function(){
it('string',function(){
node.setValue('foo');
node.flushChanges();
expect(node.setValue('bar')).to.equal(true);
});

it('boolean',function(){
node.setValue(true);
node.flushChanges();
expect(node.setValue(false)).to.equal(true);
});

it('number',function(){
node.setValue(1);
node.flushChanges();
expect(node.setValue(2)).to.equal(true);
});

it('type changes',function(){
node.setValue(1);
node.flushChanges();
expect(node.setValue(true)).to.equal(true);
node.flushChanges();
expect(node.setValue('foo')).to.equal(true);
node.flushChanges();
expect(node.setValue(null)).to.equal(true);
});

it('deeply unequal object',function(){
node.setValue({a:'a'});
node.flushChanges();
expect(node.setValue({a:'b'})).to.equal(true);
});
});

describe('returns false if value is unchanged ',function(){
it('string',function(){
node.setValue('foo');
node.flushChanges();
expect(node.setValue('foo')).to.equal(false);
});

it('boolean',function(){
node.setValue(true);
node.flushChanges();
expect(node.setValue(true)).to.equal(false);
});

it('number',function(){
node.setValue(1);
node.flushChanges();
expect(node.setValue(1)).to.equal(false);
});

it('null',function(){
node.setValue(null);
node.flushChanges();
expect(node.setValue(null)).to.equal(false);
});

it('key ordering',function(){
expect(Object.keys({a:'a',b:'b'}).sort()).to.eql(Object.keys({b:'a',a:'b'}).sort())
});

xit('deeply unequal object',function(){
node.setValue({a:'a'});
node.flushChanges();
expect(node.setValue({a:'a'})).to.equal(false);
});
});
});

describe('#isInitialized() ',function(){
it('returns false before setValue is called',function(){
node.flushChanges();
expect(node.isInitialized()).to.equal(false);
});

describe('returns true once setValue is called with a ',function(){
it('string',function(){
node.setValue('foo');
node.flushChanges();
expect(node.isInitialized()).to.equal(true);
});

it('boolean',function(){
node.setValue(false);
node.flushChanges();
expect(node.isInitialized()).to.equal(true);
});

it('number',function(){
node.setValue(3);
node.flushChanges();
expect(node.isInitialized()).to.equal(true);
});

it('null',function(){
node.setValue(null);
node.flushChanges();
expect(node.isInitialized()).to.equal(true);
});
});
});

describe('#on() ',function(){
it('"value" events are called when value changes', function(){
node.on('value',spy1);
node.setValue(true);
expect(spy1).not.to.have.been.called;
node.flushChanges();
expect(spy1).to.have.been.called;
});

it('"value" events are not called when setValue() does not change anything',function(){
node.setValue('foo');
node.on('value',spy1);
node.flushChanges();
spy1.reset();
node.setValue('foo');
node.flushChanges();
expect(spy1).not.to.have.been.called;
});
});
});

0 comments on commit 6ce0317

Please sign in to comment.