Skip to content

Commit

Permalink
wip on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rmurphey committed Apr 11, 2012
1 parent c250192 commit a531984
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
9 changes: 9 additions & 0 deletions data/testdata.json
@@ -0,0 +1,9 @@
{
"people" : [
{ "name" : "Matt" },
{ "name" : "Rebecca" },
{ "name" : "Paul" },
{ "name" : "Alex" },
{ "name" : "Adam" }
]
}
2 changes: 1 addition & 1 deletion server/server.js
Expand Up @@ -12,7 +12,7 @@ module.exports = function(opts) {
}, opts || {}); }, opts || {});


site.configure(function() { site.configure(function() {
[ 'app', 'lib', 'tests' ].forEach(function(dir) { [ 'app', 'lib', 'tests', 'data' ].forEach(function(dir) {
site.use('/' + dir, staticDir(opts.baseDir + dir)); site.use('/' + dir, staticDir(opts.baseDir + dir));
}); });
site.use(express.bodyParser()); site.use(express.bodyParser());
Expand Down
56 changes: 56 additions & 0 deletions tests/app/arrays.js
@@ -0,0 +1,56 @@
define([ 'use!underscore' ], function(_) {
describe("arrays", function() {
var a, b, fn;

beforeEach(function() {
a = [ 1, 2, 3, 4 ];
b = {
foo : 'bar',
baz : 'bim'
};

fn = function() { };
});

it("should be possible to determine the location of an item in an array", function() {
// define a function for fn so that the following will pass
expect(fn(a, 3)).to.be(2);
});

it("should be possible to add the values of an array", function() {
// define a function for fn so that the following will pass
expect(fn(a)).to.be(10);
});

it("should be possible to remove an item from an array", function() {
// define a function for fn so that the following will pass
var result = fn(a);
expect(result).to.have.length(3);
expect(result.join(' ')).to.be('1 3 4');
});

it("should be possible to add an item to the end of an array", function() {
// define a function for fn so that the following will pass
var result = fn(a, 10);
expect(result).to.have.length(5);
expect(result[result.length - 1]).to.be(10);
});

it("should be possible to create an array from two arrays", function() {
// define a function for fn so that the following will pass
var c = [ 'a', 'b', 'c' ],
result = fn(a, c);

expect(result).to.have.length(7);
expect(result.join(' ')).to.be('1 2 3 4 a b c');
});

it("should be possible to add an item anywhere in an array", function() {
// define a function for fn so that the following will pass
var result = fn(a, 'z', 2);

expect(result).to.have.length(5);
expect(result.join(' ')).to.be('1 2 z 3 4');
});
});
});
42 changes: 42 additions & 0 deletions tests/app/async.js
@@ -0,0 +1,42 @@
define([ 'jquery', 'use!underscore' ], function($, _) {
describe("async behavior", function() {
var promise, fn = function() { };

beforeEach(function() {

});

it("should return a promise that resolves true", function(done) {
var flag = false;

fn = function() {
// write a function that makes the test pass
};

fn().then(function(result) {
flag = result;
expect(flag).to.be(true);
done();
});
});

it("should receive and manipulate data from the server", function(done) {
var peopleArray,
url = '/data/testdata.json',

tests = function() {
expect(peopleArray).to.have.length(5);
expect(peopleArray.join(' ')).to.be('Adam Alex Matt Paul Rebecca');
done();
};

// replace the call to the tests function below with code that calls the
// tests function once the data has been a) retrieved from the server and
// b) manipulated so the tests will pass.

tests();
});

});

});
33 changes: 33 additions & 0 deletions tests/app/objects.js
@@ -0,0 +1,33 @@
define([ 'use!underscore' ], function(_) {
describe("objects and context", function() {
var a, b, fn = function() {};

beforeEach(function() {
a = {
name : 'Matt',
greeting : 'Hello',
sayIt : function(preamble, punctuation) {
return (preamble || '') +
this.name + ', ' +
this.greeting +
(punctuation || '!');
}
};

b = {
name : 'Rebecca',
greeting : 'Yo'
};
});

it("should be able to alter the context in which a function runs", function() {
fn = function() { };
expect(fn()).to.be('Yo, Rebecca!');
});

it("should be able to pass in arguments stored in an array", function() {
fn = function() { };
expect(fn()).to.be('Why Hello, Matt!!!');
});
});
});
57 changes: 57 additions & 0 deletions tests/app/views.js
@@ -0,0 +1,57 @@
define([ 'use!backbone', 'use!underscore', 'jquery' ], function(Backbone, _, $) {
describe("a Backbone view", function() {
var tpl, model, view;

beforeEach(function() {
tpl = '<div id="my-view"><%= greeting %></div>';
model = new Backbone.Model({
greeting : 'Hello, world'
});

if (view && view.remove) {
view.remove();
}
});

it("should render a view from a template", function() {
var MyView = Backbone.View.extend({
template : tpl,
render : function() {
// write code here to make the tests below pass
}
});

view = new MyView({
model : model
}).render();

expect(document.getElementById('my-view')).to.be.ok();
expect(document.getElementById('my-view').innerHTML).to.contain('Hello, world');
});

it("should update the view when the model changes", function() {
var MyView = Backbone.View.extend({
// fill in the code required in the initialize and render methods
// to make the tests below pass
initialize : function() {

},
template : tpl,
render : function() {

}
});

view = new MyView({
model : model
}).render();

model.set('greeting', 'Goodbye, world');

expect(document.getElementById('my-view').innerHTML).to.contain('Goodbye, world');
expect(document.getElementById('my-view').innerHTML).not.to.contain('Hello, world');
});

});

});
9 changes: 9 additions & 0 deletions tests/runner.js
@@ -1,5 +1,14 @@
var tests = [ var tests = [
// link to test files here


// 'tests/app/arrays',
// 'tests/app/objects',
// 'tests/app/async',
'tests/app/views'

// 'tests/app/models',
// 'tests/app/prototypes',
// 'tests/app/functions'
]; ];


require(tests, function() { require(tests, function() {
Expand Down

0 comments on commit a531984

Please sign in to comment.