Skip to content

Commit

Permalink
added test and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Akamaozu committed Nov 28, 2014
1 parent 0c0bd88 commit 93b8773
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
40 changes: 40 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
CHANGELOG
===
---

Version Notes are summaries of what changed and how safe it is to upgrade from the directly previous version. Please scan over them.
-
I dont expect everyone *(and by that I mean anyone)* wants to read over the Notes so I'm introducing the Overall Upgrade Safety rating system.

---
Overall Upgrade Safety is a 5 Point Rating System to indicate how safe the upgrade is from the preceeding version. Here's how you should interpret it.
-
**5/5**: **Fort Knox**. U.S Treasury Safe.

**4/5**: **Safe** unless you're using Dangerous APIs *(prefixed with underscore)* or accessing properties outside the conventional way.

**3/5**: **Minor Changes** made to non-dangerous APIs. Look over the version notes to check what changed.

**2/5**: **Major Changes** made. There's an upgrade path but it requires you treading with caution. Make no assumption of previous behavior on any updated APIs.

**1/5**: **VERY RISKY!** So many changes I can't even guarantee APIs that shouldn't be affected are safe. I wouldn't even recommend you upgrade unless you know what you are doing, what I am doing and what the meaning of life is.

---
V. 0.0.3
===
---
OVERALL UPGRADE SAFETY: 4/5
---
1. **Added Mocha Tests**
2. **Changed Internal Variable Names**

**NOTES**
-
- **Added Mocha Tests**
- To test, just run **mocha**.
- Test Coverage: All Public APIs and their expected behavior. Even the unsafe one.
- Hint: `_notifyWatcher` is the unsafe one.
- **Changed Internal Variable Names**
- Shouldn't affect anything EXCEPT you were using `_notifyWatcher` or if you were accessing watcher messages outside the callback.
- The naming convention is changed from `watcherParams` to `watcherMessage`. Likewise from `noticeParams` to `noticeMessage`. It's more consistent and likely to remain this way for the foreseeable future. Still not safe to use them.
197 changes: 197 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
var assert, Noticeboard, test_board, notice, watcher;

assert = require('assert');
Noticeboard = require('../cjs-noticeboard.js');

test_board = new Noticeboard();
notice = 'behavior-test';
watcher = 'assert-behavior';

describe('Noticeboard', function(){

it('is a function', function(){

assert.equal(typeof Noticeboard, 'function', 'is not a function');
});
});

describe('Instantiated Noticeboard Properties', function(){

it('has expected properties of its own', function(){

assert.equal( test_board.hasOwnProperty('settings') && typeof test_board.settings === 'object', true, 'didn\'t instantiate with its own "settings" object');
assert.equal( test_board.hasOwnProperty('watchers') && typeof test_board.watchers === 'object', true, 'didn\'t instantiate with its own "watchers" object');
assert.equal( test_board.hasOwnProperty('cache') && typeof test_board.cache === 'object', true, 'didn\'t instantiate with its own "cache" object');
});
});

describe('Instantiated Noticeboard Prototype Properties', function(){

it('prototype has expected properties of its own', function(){

assert.equal( test_board.__proto__.hasOwnProperty('watch') && typeof test_board.watch === 'function', true, 'prototype is missing "watch" function');
assert.equal( test_board.__proto__.hasOwnProperty('ignore') && typeof test_board.ignore === 'function', true, 'prototype is missing "ignore" function');
assert.equal( test_board.__proto__.hasOwnProperty('notify') && typeof test_board.notify === 'function', true, 'prototype is missing "notify" function');
assert.equal( test_board.__proto__.hasOwnProperty('once') && typeof test_board.once === 'function', true, 'prototype is missing "once" function');
assert.equal( test_board.__proto__.hasOwnProperty('log') && typeof test_board.log === 'function', true, 'prototype is missing "log" function');
assert.equal( test_board.__proto__.hasOwnProperty('_notifyWatcher') && typeof test_board._notifyWatcher === 'function', true, 'prototype is missing "_notifyWatcher" function');
});
});

describe('Instantiated Noticeboard Behavior', function(){

it('"watch" adds watcher to notice\'s list of watchers', function(){

var callback, options, watcher_list, this_watcher, success;

callback = function(){ console.log("i am a watcher's callback"); }
options = {message: "this is the watcher's message"};
success = test_board.watch(notice, watcher, callback, options);

if(!success){ throw new Error("watch function was unsuccessful"); }

watcher_list = test_board.watchers[notice];
this_watcher = watcher_list[watcher];

assert.equal( typeof watcher_list !== 'undefined', true, 'list of watchers was not correctly created');
assert.equal( typeof this_watcher !== 'undefined', true, 'watcher was not added to list of watchers');
assert.equal( this_watcher.callback === callback && this_watcher.watcherMessage === options.message, true, 'watcher was not correctly added to notice\'s list of watchers');
});

it('"notify" triggers a watcher\'s callback', function(done){

var message, success, callback_triggered;

message = "this is the notice's message";
callback_triggered = false;

test_board.watch(notice, watcher, function(msg){

var mocha_done = msg.watcher;

callback_triggered = true;
mocha_done();

assert.equal( callback_triggered === true, true, 'watcher\'s callback was not triggered' );
}, {message: done});

success = test_board.notify(notice, message);

if(!success){ throw new Error("notify function was unsuccessful"); }
});

it('"watch" triggers its callback if cache exists and useCache is true', function(done){

var cache_used = false;

test_board.watch(notice, watcher, function(msg){

var mocha_done = msg.watcher;

cache_used = true;
mocha_done();

assert.equal(cache_used === true, true, 'watcher did not use cache to trigger callback');
}, {message: done, useCache: true});
});

it('"ignore" removes a watcher from a notice\'s watcher list', function(){

var this_watcher, watcher_exists;

this_watcher = test_board.watchers[notice][watcher];

watcher_exists = (typeof this_watcher !== 'undefined');

if(!watcher_exists){ throw new Error('watcher wasn\'t set up before assertion'); }

test_board.ignore(notice, watcher);

assert.equal(typeof test_board.watchers[notice][watcher] === "undefined", true, 'watcher hasn\'t been removed from watcher list');
});

it('"once" ignores a notice after its callback has been fired', function(done){

// chain of events
// 1. notify once-completed in once's callback
// 2. do assertions when notified once-completed

test_board.once(notice, watcher, function(msg){

var mocha_done = msg.watcher;

test_board.notify('once-completed', mocha_done);

}, {message: done});

test_board.watch('once-completed', 'do-assertions', function(msg){

var relayed_mocha_done = msg.notice;

relayed_mocha_done();

assert.equal(typeof test_board.watchers[notice][watcher] === "undefined", true, 'watcher hasn\'t been removed from watcher list');
});

test_board.notify(notice);
});

it('"once" triggers callback if cache exists and useCache is true', function(done){

var callback_autotriggered = false;

test_board.notify(notice, done);

test_board.once(notice, watcher, function(msg){

var mocha_done = msg.notice;

callback_autotriggered = true;
mocha_done();

assert.equal(callback_autotriggered === true, true, 'once\'s callback was not triggered');

},{useCache: true});
});

it('"log" sends its message to watchers of log-entry', function(done){

// once should trigger the callback due to autolog
test_board.once('log-entry', watcher, function(msg){

var log_arguments, mocha_done;

mocha_done = msg.watcher;
log_arguments = msg.notice;

mocha_done();

assert.equal(log_arguments.length > 0, true, 'message received is not the same as log entry');

},{message: done});
});

it('"_notifyWatcher" overrides default message and fires watcher\'s callback', function(done){

var watcher_msg, notice_msg, mocha_done;

watcher_msg = "untouchable";
notice_msg = "_notifyWatcher";
mocha_done = done;

test_board.watch(notice, watcher, function(msg){

var received_msg, was_touched;

received_msg = msg.watcher;
was_touched = ((watcher_msg !== received_msg) && (msg.notice === notice_msg));

mocha_done();

assert.equal(was_touched === true, true, 'message was not touched');

}, {message: watcher_msg});

test_board._notifyWatcher(watcher, test_board.watchers[notice], {watcherMessage: "touched", noticeMessage: notice_msg});
});
});

0 comments on commit 93b8773

Please sign in to comment.