Permalink
Browse files

wip on tests

  • Loading branch information...
1 parent c250192 commit a531984ea444e01fafd493316f8c949965b247c6 @rmurphey rmurphey committed Apr 11, 2012
Showing with 208 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +9 −0 data/testdata.json
  3. +1 −1 server/server.js
  4. +56 −0 tests/app/arrays.js
  5. +42 −0 tests/app/async.js
  6. +33 −0 tests/app/objects.js
  7. +57 −0 tests/app/views.js
  8. +9 −0 tests/runner.js
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -0,0 +1,9 @@
+{
+ "people" : [
+ { "name" : "Matt" },
+ { "name" : "Rebecca" },
+ { "name" : "Paul" },
+ { "name" : "Alex" },
+ { "name" : "Adam" }
+ ]
+}
View
@@ -12,7 +12,7 @@ module.exports = function(opts) {
}, opts || {});
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(express.bodyParser());
View
@@ -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');
+ });
+ });
+});
View
@@ -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();
+ });
+
+ });
+
+});
View
@@ -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!!!');
+ });
+ });
+});
View
@@ -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');
+ });
+
+ });
+
+});
View
@@ -1,5 +1,14 @@
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() {

0 comments on commit a531984

Please sign in to comment.