Adds publish/subscribe channels. Unit tests added.

This commit is contained in:
jaubourg 2011-05-24 01:59:00 +02:00
parent 4f3f0e1d4e
commit 57634a2c15
4 changed files with 93 additions and 0 deletions

56
test/unit/channel.js Normal file
View file

@ -0,0 +1,56 @@
module("channel", { teardown: moduleTeardown });
test( "jQuery.Channel - Anonymous Channel", function() {
expect( 4 );
var channel = jQuery.Channel(),
count = 0;
function firstCallback( value ) {
strictEqual( count, 1, "Callback called when needed" );
strictEqual( value, "test", "Published value received" );
}
count++;
channel.subscribe( firstCallback );
channel.publish( "test" );
channel.unsubscribe( firstCallback );
count++;
channel.subscribe(function( value ) {
strictEqual( count, 2, "Callback called when needed" );
strictEqual( value, "test", "Published value received" );
});
channel.publish( "test" );
});
test( "jQuery.Channel - Named Channel", function() {
expect( 2 );
function callback( value ) {
ok( true, "Callback called" );
strictEqual( value, "test", "Proper value received" );
}
jQuery.Channel( "test" ).subscribe( callback );
jQuery.Channel( "test" ).publish( "test" );
jQuery.Channel( "test" ).unsubscribe( callback );
jQuery.Channel( "test" ).publish( "test" );
});
test( "jQuery.Channel - Helpers", function() {
expect( 2 );
function callback( value ) {
ok( true, "Callback called" );
strictEqual( value, "test", "Proper value received" );
}
jQuery.subscribe( "test", callback );
jQuery.publish( "test" , "test" );
jQuery.unsubscribe( "test", callback );
jQuery.publish( "test" , "test" );
});