Skip to content

Commit

Permalink
all tests passing, linted
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikJoreteg committed Mar 29, 2014
1 parent d6f80f0 commit 963d9e7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 94 deletions.
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
12 changes: 12 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"asi": false,
"expr": true,
"loopfunc": true,
"curly": false,
"evil": true,
"white": true,
"undef": true,
"node": true,
"indent": 4,
"eqnull": true
}
32 changes: 18 additions & 14 deletions ampersand-state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var _ = require('underscore');
var BBEvents = require('backbone-events-standalone');
var arrayNext = require('array-next');
var propertyUtils = require('./properties');
var dataTypes = require('./dataTypes');

Expand Down Expand Up @@ -27,18 +28,6 @@ function Base(attrs, options) {


_.extend(Base.prototype, BBEvents, {
// getter for attributes
get attributes() {
return this._getAttributes(true);
},

// getter for derived properties
get derived() {
var res = {};
for (var item in this._derived) res[item] = this._derived[item].fn.apply(this);
return res;
},

idAttribute: 'id',

namespaceAttribute: 'namespace',
Expand Down Expand Up @@ -341,7 +330,7 @@ _.extend(Base.prototype, BBEvents, {
},

_createPropertyDefinition: function (name, desc, isSession) {
propertyUtils.createPropertyDefinition(this, name, desc, isSession);
return propertyUtils.createPropertyDefinition(this, name, desc, isSession);
},

// just makes friendlier errors when trying to define a new model
Expand Down Expand Up @@ -398,6 +387,21 @@ _.extend(Base.prototype, BBEvents, {
}
});

// getter for attributes
Object.defineProperties(Base.prototype, {
attributes: {
get: function () {
return this._getAttributes(true);
}
},
derived: {
get: function () {
var res = {};
for (var item in this._derived) res[item] = this._derived[item].fn.apply(this);
return res;
}
}
});

var extend = function (protoProps) {
var parent = this;
Expand All @@ -421,7 +425,7 @@ var extend = function (protoProps) {

// Set the prototype chain to inherit from `parent`, without calling
// `parent`'s constructor function.
var Surrogate = function(){ this.constructor = child; };
var Surrogate = function () { this.constructor = child; };
Surrogate.prototype = parent.prototype;
child.prototype = new Surrogate();

Expand Down
3 changes: 3 additions & 0 deletions dataTypes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var _ = require('underscore');


// our dataTypes
module.exports = {
date: {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
},
"dependencies": {
"backbone-events-standalone": "~0.2.1",
"underscore": "~1.6.0"
"underscore": "~1.6.0",
"array-next": "~0.0.1"
},
"devDependencies": {
"ampersand-registry": "0.x.x",
"run-browser": "~1.2.0",
"tape": "~2.11.0"
"tape": "~2.11.0",
"precommit-hook": "~0.3.10"
},
"homepage": "https://github.com/ampersandjs/ampersand-state",
"keywords": [
Expand All @@ -29,6 +31,7 @@
},
"scripts": {
"test": "node test/index.js",
"validate": "jshint .",
"start": "run-browser test/index.js"
},
"testling": {
Expand Down
6 changes: 3 additions & 3 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ test('instanceof checks should pass for all parents in the chain', function (t)
var P1 = Person.extend({});
var P2 = P1.extend({});
var P3 = P2.extend({});
var p1 = new P1;
var p2 = new P2;
var p3 = new P3;
var p1 = new P1();
var p2 = new P2();
var p3 = new P3();
t.ok(p1 instanceof Person);
t.ok(p2 instanceof Person);
t.ok(p3 instanceof Person);
Expand Down
98 changes: 23 additions & 75 deletions test/full.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var tape = require('tape');
var _ = require('underscore');
var State = require('../ampersand-state');
var AmpersandRegistry = require('ampersand-registry');
var definition, Foo, registry;
Expand All @@ -8,7 +9,7 @@ var definition, Foo, registry;
var test = function () {
reset();
tape.apply(tape, arguments);
}
};

function reset() {
registry = new AmpersandRegistry();
Expand Down Expand Up @@ -154,7 +155,7 @@ test('Setting other properties ignores them by default', function (t) {
t.strictEqual(foo.craziness, undefined, 'property should be ignored');
t.end();
});
/*

test('Setting other properties is ok if allowOtherProperties is true', function (t) {
var foo = new Foo();
foo.extraProperties = 'allow';
Expand All @@ -180,6 +181,7 @@ test('should throw a type error for bad data types', function (t) {

try { new Foo({list: 10}); }
catch (err) { t.ok(err instanceof TypeError); }

t.end();
});

Expand Down Expand Up @@ -207,34 +209,13 @@ test('should store previous attributes', function (t) {
t.end();
});

test('should have list method helpers', function (t) {
var foo = new Foo({
hash: [1, 2, 'a', 'b']
});
t.ok(foo.hasListVal('hash', 2));
foo.removeListVal('hash', 1);
t.deepEqual([2, 'a', 'b'], foo.hash);
foo.addListVal('hash', 10);
t.deepEqual([2, 'a', 'b', 10], foo.hash);
t.end();
});
test('should have data serialization methods', function (t) {
var foo = new Foo({
firstName: 'bob',
lastName: 'tom',
thing: 'abc'
});

t.deepEqual(foo.keys(), [
'firstName',
'lastName',
'thing',
'myBool',
'active'
]);
t.deepEqual(foo.attributes, {
firstName: 'bob',
lastName: 'tom',
Expand Down Expand Up @@ -292,7 +273,7 @@ test('should fire events normally for properties defined on the fly', function (
});

test('should fire event on derived properties, even if dependent on ad hoc prop.', function (t) {
var Foo = new State.extend({
var Foo = State.extend({
extraProperties: 'allow',
derived: {
isCrazy: {
Expand All @@ -304,8 +285,7 @@ test('should fire event on derived properties, even if dependent on ad hoc prop.
}
});
var foo = new Foo();
foo.extraProperties = 'allow';
foo.on('change:isCrazy', function (t) {
foo.on('change:isCrazy', function () {
t.ok(true);
});
foo.set({
Expand Down Expand Up @@ -354,7 +334,7 @@ test('derived properties', function (t) {
cache: false,
deps: ['name'],
fn: function () {
notCachedRan++
notCachedRan++;
return 'hi, ' + this.name;
}
}
Expand All @@ -365,7 +345,7 @@ test('derived properties', function (t) {
t.equal(foo.greeting, 'hi, henrik');
t.equal(foo.greeting, 'hi, henrik');
t.equal(ran, 1, 'cached derived should only run once');
t.equal(notCachedRan, 0, 'shold not have been run yet')
t.equal(notCachedRan, 0, 'shold not have been run yet');
foo.name = 'someone';
t.equal(foo.greeting, 'hi, someone');
t.equal(foo.greeting, 'hi, someone');
Expand Down Expand Up @@ -396,7 +376,7 @@ test('cached, derived properties should only fire change event if they\'ve actua
});
var foo = new Foo({name: 'henrik'});
foo.on('change:greeting', function () {
changed++
changed++;
});
t.equal(changed, 0);
foo.name = 'new';
Expand Down Expand Up @@ -442,7 +422,7 @@ test('derived properties with derived dependencies', function (t) {
});
foo.on('change', function () {
ran++;
t.ok(true, 'should file main event')
t.ok(true, 'should file main event');
});
foo.name = 'something';
t.equal(ran, 4);
Expand All @@ -464,26 +444,6 @@ test('derived properties triggered with multiple instances', function (t) {
t.end();
});

test('should fire a remove event', function (t) {
var foo = new Foo({firstName: 'hi'});
foo.on('remove', function () {
t.ok(true);
});
foo.remove();
t.end();
});
test('should remove all event bindings after remove', function (t) {
var foo = new Foo({thing: 'meow'});
foo.on('change', function () {
t.ok(false);
});
foo.remove();
foo.thing = 'cow';
t.ok(true);
t.end();
});
test('should store models in the registry', function (t) {
var foo = new Foo({
id: 1,
Expand All @@ -500,17 +460,6 @@ test('should store models in the registry', function (t) {
t.end();
});

test('should remove from registry on remove', function (t) {
var foo = new Foo({id: 20, lastName: 'hi'});
foo.remove();
var found = registry.lookup('foo', 20);
t.ok(!found);
// make a new one
var bar = new Foo({id: 20});
t.strictEqual(bar.lastName, '');
t.end();
});
test('should be able to bind events even if sealed', function (t) {
var SealedModel = State.extend({seal: true, props: {name: 'string'}});

Expand Down Expand Up @@ -567,12 +516,12 @@ test('Should be able to define and use custom data types', function (t) {
};
},
get: function (val) {
return val + 'crazy!'
return val + 'crazy!';
}
};

var Foo = State.extend({
props:{
props: {
silliness: 'crazyType'
}
});
Expand Down Expand Up @@ -600,13 +549,13 @@ test('Should only allow nulls where specified', function (t) {
});

test('Attribute test function works', function (t) {
var foo = new Foo({good: 'good'});
t.equal(foo.good, 'good');
var foo = new Foo({good: 'good'});
t.equal(foo.good, 'good');

t.throws(function () {
foo.good = 'bad';
}, TypeError, 'Throws exception on invalid attribute value');
t.end();
t.throws(function () {
foo.good = 'bad';
}, TypeError, 'Throws exception on invalid attribute value');
t.end();
});

test('Values attribute basic functionality', function (t) {
Expand All @@ -621,7 +570,7 @@ test('Values attribute basic functionality', function (t) {
var m = new Model();

t.throws(function () {
m.state = 'PR'
m.state = 'PR';
}, TypeError, 'Throws exception when setting something not in list');

t.equal(m.state, undefined, 'Should be undefined if no default');
Expand All @@ -647,7 +596,7 @@ test('Values attribute default works', function (t) {
t.equal(m.state, 'CA', 'Should have applied the default');

t.throws(function () {
m.state = 'PR'
m.state = 'PR';
}, TypeError, 'Throws exception when setting something not in list');
t.end();
});
Expand Down Expand Up @@ -677,11 +626,11 @@ test('toggle() works on boolean and values properties.', function (t) {
m.toggle('state');
t.equal(m.state, 'CA', 'Should go to next with loop');

m.toggle('isAwesome')
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, true, 'Should toggle even if undefined');
m.toggle('isAwesome')
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, false, 'Should toggle if true.');
m.toggle('isAwesome')
m.toggle('isAwesome');
t.strictEqual(m.isAwesome, true, 'Should toggle if false.');
t.end();
});
Expand All @@ -706,4 +655,3 @@ test('property test function scope is correct.', function (t) {
t.equal(m, temp);
t.end();
});
*/

0 comments on commit 963d9e7

Please sign in to comment.