Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Stuart committed Feb 23, 2012
1 parent 27de1cd commit f51c1cf
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 31 deletions.
3 changes: 3 additions & 0 deletions script/test/application.coffee
Expand Up @@ -16,6 +16,9 @@ module "Core"
$.mockjaxClear()

test "Compenents initialize", ->
expect 4
return unless @app

ok @app.status , 'Status'
ok @app.page , 'Page'
ok @app.keyboard , 'Keyboard'
Expand Down
2 changes: 2 additions & 0 deletions script/test/application.js
Expand Up @@ -17,6 +17,8 @@ module("Core", {
});

test("Compenents initialize", function() {
expect(4);
if (!this.app) return;
ok(this.app.status, 'Status');
ok(this.app.page, 'Page');
ok(this.app.keyboard, 'Keyboard');
Expand Down
22 changes: 22 additions & 0 deletions script/test/event.coffee
@@ -0,0 +1,22 @@
module 'Event'
setup: ->
@event = new Event

test "handles subscribtions", ->
callback = ->
@event.subscribe callback
equal @event.subscribers.length, 1

test "calls subscribers when triggered", ->
@callback_count = 0
callback = => @callback_count += 1
@event.subscribers = [callback, callback, callback]
@event.trigger()
equal @callback_count, 3

test "calls subscribers with all arguments passed to trigger", ->
@callback_args_count = 0
callback = (args...)=> @callback_args_count = args.length
@event.subscribers = [callback]
@event.trigger(1,2,3)
equal @callback_args_count, 3
38 changes: 38 additions & 0 deletions script/test/event.js
@@ -0,0 +1,38 @@
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __slice = Array.prototype.slice;

module('Event', {
setup: function() {
return this.event = new Event;
}
});

test("handles subscribtions", function() {
var callback;
callback = function() {};
this.event.subscribe(callback);
return equal(this.event.subscribers.length, 1);
});

test("calls subscribers when triggered", function() {
var callback;
this.callback_count = 0;
callback = __bind(function() {
return this.callback_count += 1;
}, this);
this.event.subscribers = [callback, callback, callback];
this.event.trigger();
return equal(this.callback_count, 3);
});

test("calls subscribers with all arguments passed to trigger", function() {
var callback;
this.callback_args_count = 0;
callback = __bind(function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return this.callback_args_count = args.length;
}, this);
this.event.subscribers = [callback];
this.event.trigger(1, 2, 3);
return equal(this.callback_args_count, 3);
});
7 changes: 0 additions & 7 deletions script/test/manager.coffee
Expand Up @@ -10,10 +10,6 @@ module "Manager"
miss: -> null
}
}
status: {
recordHit: => @hit_count = (@hit_count || 0) + 1
recordMiss: => @miss_count = (@miss_count || 0) + 1
}
keyboard: {
selectKey: (charCode, keyCode)=> @selected_key = keyCode
deselectKey: => @selected_key = null
Expand All @@ -26,13 +22,10 @@ test "keypress within document deligates to page and status", ->

($ document).trigger e
equal @space_index, 1, '@base.page.nextSpace called'
equal @hit_count, 1, '@base.state.recordHit called'

@manager.base.page.current_space.match = -> false
($ document).trigger e
equal @space_index, 1, '@base.page.nextSpace not called'
equal @hit_count, 1, '@base.state.recordHit not called'
equal @miss_count, 1, '@base.state.recordMiss called'

test "#processKeyPress does nothing for backspace", ->
equal @manager.processKeyPress({ keyCode: 8 }), null
Expand Down
13 changes: 1 addition & 12 deletions script/test/manager.js
Expand Up @@ -20,14 +20,6 @@ module("Manager", {
}
}
},
status: {
recordHit: __bind(function() {
return this.hit_count = (this.hit_count || 0) + 1;
}, this),
recordMiss: __bind(function() {
return this.miss_count = (this.miss_count || 0) + 1;
}, this)
},
keyboard: {
selectKey: __bind(function(charCode, keyCode) {
return this.selected_key = keyCode;
Expand All @@ -46,14 +38,11 @@ test("keypress within document deligates to page and status", function() {
e.charCode = 97;
($(document)).trigger(e);
equal(this.space_index, 1, '@base.page.nextSpace called');
equal(this.hit_count, 1, '@base.state.recordHit called');
this.manager.base.page.current_space.match = function() {
return false;
};
($(document)).trigger(e);
equal(this.space_index, 1, '@base.page.nextSpace not called');
equal(this.hit_count, 1, '@base.state.recordHit not called');
return equal(this.miss_count, 1, '@base.state.recordMiss called');
return equal(this.space_index, 1, '@base.page.nextSpace not called');
});

test("#processKeyPress does nothing for backspace", function() {
Expand Down
4 changes: 4 additions & 0 deletions script/test/page.coffee
Expand Up @@ -141,6 +141,10 @@ module "Page.Row.Space"
setup: ->
page = {
$container: ($ "#application")
events: {
hit: new Event
miss: new Event
}
config:
font_size: 18
}
Expand Down
4 changes: 4 additions & 0 deletions script/test/page.js
Expand Up @@ -208,6 +208,10 @@ module("Page.Row.Space", {
var page, row;
page = {
$container: $("#application"),
events: {
hit: new Event,
miss: new Event
},
config: {
font_size: 18
}
Expand Down
13 changes: 8 additions & 5 deletions script/test/status.coffee
@@ -1,7 +1,10 @@
module "Status"
setup: ->
Store.clear()
@status = new Status {}
@status = new Status {
events:
page_init: new Event
}

test "records hit", ->
expect 1
Expand Down Expand Up @@ -35,22 +38,22 @@ test "records wpms", ->
finish()
, "Status#wpms.length is 2 for every 10 hits"

test "#wpmAvg calculates wpm average", ->
test "#wpmBufferAvg calculates wpm average", ->
@status.wpm_buffer = 4

# 2 words taking 5 seconds each
for [1..10]
@status.recordHitSpeed 1

equal @status.wpmAvg(), 12, '2 words, 5 seconds each'
equal @status.wpmBufferAvg(), 12, '2 words, 5 seconds each'

# 2 more words, first taking 15 and 41 seconds respectivly
for n in [1..10]
@status.recordHitSpeed n

equal @status.wpmAvg(), 7.375, '4 words, 12, 12, 4, and 1.5 seconds respectivly'
equal @status.wpmBufferAvg(), 7.375, '4 words, 12, 12, 4, and 1.5 seconds respectivly'

# 1 more word at 6 seconds
for [1..5]
@status.recordHitSpeed 2
equal @status.wpmAvg(), 5.875, '4 words (buffer), 12, 4, 1.5, and 6 seconds respectivly'
equal @status.wpmBufferAvg(), 5.875, '4 words (buffer), 12, 4, 1.5, and 6 seconds respectivly'
14 changes: 9 additions & 5 deletions script/test/status.js
Expand Up @@ -3,7 +3,11 @@ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments)
module("Status", {
setup: function() {
Store.clear();
return this.status = new Status({});
return this.status = new Status({
events: {
page_init: new Event
}
});
}
});

Expand Down Expand Up @@ -45,19 +49,19 @@ test("records wpms", function() {
}, this), "Status#wpms.length is 2 for every 10 hits");
});

test("#wpmAvg calculates wpm average", function() {
test("#wpmBufferAvg calculates wpm average", function() {
var n, _i, _j;
this.status.wpm_buffer = 4;
for (_i = 1; _i <= 10; _i++) {
this.status.recordHitSpeed(1);
}
equal(this.status.wpmAvg(), 12, '2 words, 5 seconds each');
equal(this.status.wpmBufferAvg(), 12, '2 words, 5 seconds each');
for (n = 1; n <= 10; n++) {
this.status.recordHitSpeed(n);
}
equal(this.status.wpmAvg(), 7.375, '4 words, 12, 12, 4, and 1.5 seconds respectivly');
equal(this.status.wpmBufferAvg(), 7.375, '4 words, 12, 12, 4, and 1.5 seconds respectivly');
for (_j = 1; _j <= 5; _j++) {
this.status.recordHitSpeed(2);
}
return equal(this.status.wpmAvg(), 5.875, '4 words (buffer), 12, 4, 1.5, and 6 seconds respectivly');
return equal(this.status.wpmBufferAvg(), 5.875, '4 words (buffer), 12, 4, 1.5, and 6 seconds respectivly');
});
4 changes: 2 additions & 2 deletions views/test.erb
Expand Up @@ -17,8 +17,8 @@
<div id='javascript'>
<% javascript 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js' %>
<% javascript :qunit, 'jquery.mockjax' %>
<% javascript :underscore, :constants, :keyboard, :page, :status, :manager, 'store-js' %>
<% javascript_test :test_helper, :application, :manager, :page, :status %>
<% javascript :underscore, :constants, :event, :keyboard, :page, :status, :manager, 'store-js' %>
<% javascript_test :test_helper, :application, :event, :manager, :page, :status %>
<script type='text/javascript'>
require_js = function(scripts, callback){} // disable application from initializing
</script>
Expand Down

0 comments on commit f51c1cf

Please sign in to comment.