Converting to test build and not src files

This commit is contained in:
ifandelse 2014-01-20 18:03:03 -05:00
parent 39c5ee9183
commit 43b7f5fe75
22 changed files with 7516 additions and 7168 deletions

View file

@ -3,7 +3,7 @@
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
* Version: v0.8.11
* Url: http://github.com/postaljs/postal.js
* License:
* License(s): MIT, GPL
*/
(function (root, factory) {

File diff suppressed because one or more lines are too long

View file

@ -3,7 +3,7 @@
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
* Version: v0.8.11
* Url: http://github.com/postaljs/postal.js
* License:
* License(s): MIT, GPL
*/
(function (root, factory) {

File diff suppressed because one or more lines are too long

View file

@ -1,259 +1,270 @@
@charset "utf-8";
body {
margin:0;
margin:0;
}
#mocha {
font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 60px 50px;
font: 20px/1.5 "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: 60px 50px;
}
#mocha ul, #mocha li {
margin: 0;
padding: 0;
#mocha ul,
#mocha li {
margin: 0;
padding: 0;
}
#mocha ul {
list-style: none;
list-style: none;
}
#mocha h1, #mocha h2 {
margin: 0;
#mocha h1,
#mocha h2 {
margin: 0;
}
#mocha h1 {
margin-top: 15px;
font-size: 1em;
font-weight: 200;
margin-top: 15px;
font-size: 1em;
font-weight: 200;
}
#mocha h1 a {
text-decoration: none;
color: inherit;
text-decoration: none;
color: inherit;
}
#mocha h1 a:hover {
text-decoration: underline;
text-decoration: underline;
}
#mocha .suite .suite h1 {
margin-top: 0;
font-size: .8em;
margin-top: 0;
font-size: .8em;
}
#mocha .hidden {
display: none;
display: none;
}
#mocha h2 {
font-size: 12px;
font-weight: normal;
cursor: pointer;
font-size: 12px;
font-weight: normal;
cursor: pointer;
}
#mocha .suite {
margin-left: 15px;
margin-left: 15px;
}
#mocha .test {
margin-left: 15px;
overflow: hidden;
margin-left: 15px;
overflow: hidden;
}
#mocha .test.pending:hover h2::after {
content: '(pending)';
font-family: arial, sans-serif;
content: '(pending)';
font-family: arial, sans-serif;
}
#mocha .test.pass.medium .duration {
background: #C09853;
background: #c09853;
}
#mocha .test.pass.slow .duration {
background: #B94A48;
background: #b94a48;
}
#mocha .test.pass::before {
content: '✓';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #00d6b2;
content: '✓';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #00d6b2;
}
#mocha .test.pass .duration {
font-size: 9px;
margin-left: 5px;
padding: 2px 5px;
color: white;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
font-size: 9px;
margin-left: 5px;
padding: 2px 5px;
color: #fff;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-moz-box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
box-shadow: inset 0 1px 1px rgba(0,0,0,.2);
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
-ms-border-radius: 5px;
-o-border-radius: 5px;
border-radius: 5px;
}
#mocha .test.pass.fast .duration {
display: none;
display: none;
}
#mocha .test.pending {
color: #0b97c4;
color: #0b97c4;
}
#mocha .test.pending::before {
content: '◦';
color: #0b97c4;
content: '◦';
color: #0b97c4;
}
#mocha .test.fail {
color: #c00;
color: #c00;
}
#mocha .test.fail pre {
color: black;
color: black;
}
#mocha .test.fail::before {
content: '✖';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #c00;
content: '✖';
font-size: 12px;
display: block;
float: left;
margin-right: 5px;
color: #c00;
}
#mocha .test pre.error {
color: #c00;
max-height: 300px;
overflow: auto;
color: #c00;
max-height: 300px;
overflow: auto;
}
/**
* (1): approximate for browsers not supporting calc
* (2): 42 = 2*15 + 2*10 + 2*1 (padding + margin + border)
* ^^ seriously
*/
#mocha .test pre {
display: block;
float: left;
clear: left;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
border-bottom-color: #ddd;
-webkit-border-radius: 3px;
-webkit-box-shadow: 0 1px 3px #eee;
-moz-border-radius: 3px;
-moz-box-shadow: 0 1px 3px #eee;
display: block;
float: left;
clear: left;
font: 12px/1.5 monaco, monospace;
margin: 5px;
padding: 15px;
border: 1px solid #eee;
max-width: 85%; /*(1)*/
max-width: calc(100% - 42px); /*(2)*/
word-wrap: break-word;
border-bottom-color: #ddd;
-webkit-border-radius: 3px;
-webkit-box-shadow: 0 1px 3px #eee;
-moz-border-radius: 3px;
-moz-box-shadow: 0 1px 3px #eee;
border-radius: 3px;
}
#mocha .test h2 {
position: relative;
position: relative;
}
#mocha .test a.replay {
position: absolute;
top: 3px;
right: 0;
text-decoration: none;
vertical-align: middle;
display: block;
width: 15px;
height: 15px;
line-height: 15px;
text-align: center;
background: #eee;
font-size: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-transition: opacity 200ms;
-moz-transition: opacity 200ms;
transition: opacity 200ms;
opacity: 0.3;
color: #888;
position: absolute;
top: 3px;
right: 0;
text-decoration: none;
vertical-align: middle;
display: block;
width: 15px;
height: 15px;
line-height: 15px;
text-align: center;
background: #eee;
font-size: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
-webkit-transition: opacity 200ms;
-moz-transition: opacity 200ms;
transition: opacity 200ms;
opacity: 0.3;
color: #888;
}
#mocha .test:hover a.replay {
opacity: 1;
opacity: 1;
}
#mocha-report.pass .test.fail {
display: none;
display: none;
}
#mocha-report.fail .test.pass {
display: none;
display: none;
}
#mocha-report.pending .test.pass,
#mocha-report.pending .test.fail {
display: none;
display: none;
}
#mocha-report.pending .test.pass.pending {
display: block;
display: block;
}
#mocha-error {
color: #c00;
font-size: 1.5em;
font-weight: 100;
letter-spacing: 1px;
color: #c00;
font-size: 1.5em;
font-weight: 100;
letter-spacing: 1px;
}
#mocha-stats {
position: fixed;
top: 15px;
right: 10px;
font-size: 12px;
margin: 0;
color: #888;
z-index: 1;
position: fixed;
top: 15px;
right: 10px;
font-size: 12px;
margin: 0;
color: #888;
z-index: 1;
}
#mocha-stats .progress {
float: right;
padding-top: 0;
float: right;
padding-top: 0;
}
#mocha-stats em {
color: black;
color: black;
}
#mocha-stats a {
text-decoration: none;
color: inherit;
text-decoration: none;
color: inherit;
}
#mocha-stats a:hover {
border-bottom: 1px solid #eee;
border-bottom: 1px solid #eee;
}
#mocha-stats li {
display: inline-block;
margin: 0 5px;
list-style: none;
padding-top: 11px;
display: inline-block;
margin: 0 5px;
list-style: none;
padding-top: 11px;
}
#mocha-stats canvas {
width: 40px;
height: 40px;
width: 40px;
height: 40px;
}
#mocha code .comment { color: #ddd }
#mocha code .init { color: #2F6FAD }
#mocha code .string { color: #5890AD }
#mocha code .keyword { color: #8A6343 }
#mocha code .number { color: #2F6FAD }
#mocha code .comment { color: #ddd; }
#mocha code .init { color: #2f6fad; }
#mocha code .string { color: #5890ad; }
#mocha code .keyword { color: #8a6343; }
#mocha code .number { color: #2f6fad; }
@media screen and (max-device-width: 480px) {
#mocha {
margin: 60px 0px;
}
#mocha {
margin: 60px 0px;
}
#mocha #stats {
position: absolute;
}
}
#mocha #stats {
position: absolute;
}
}

10978
ext/mocha.js

File diff suppressed because it is too large Load diff

View file

@ -20,7 +20,7 @@ var banner = ["/**",
" * Author: <%= pkg.author %>",
" * Version: v<%= pkg.version %>",
" * Url: <%= pkg.homepage %>",
" * License: <%= pkg.license %>",
" * License(s): <% pkg.licenses.forEach(function( license, idx ){ %><%= license.type %><% if(idx !== pkg.licenses.length-1) { %>, <% } %><% }); %>",
" */",
""].join("\n");

View file

@ -3,7 +3,7 @@
* Author: Jim Cowart (http://freshbrewedcode.com/jimcowart)
* Version: v0.8.11
* Url: http://github.com/postaljs/postal.js
* License:
* License(s): MIT, GPL
*/
(function (root, factory) {

4
lib/postal.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,470 +0,0 @@
/* global describe, postal, it, after, before, expect, bindingsResolver */
describe( "amqpBindingsResolver", function () {
describe( "When calling compare", function () {
beforeEach( function () {
bindingsResolver.reset();
} );
describe( "With '*' wildcards", function () {
// Passing matches
describe( "With topic Top.Middle.Bottom and binding *.Middle.Bottom", function () {
var result = bindingsResolver.compare( "*.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.Middle.*", function () {
var result = bindingsResolver.compare( "Top.Middle.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "Top.*.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.*.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "*.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding *.Middle.*", function () {
var result = bindingsResolver.compare( "*.Middle.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.Middle.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding *.*.*", function () {
var result = bindingsResolver.compare( "*.*.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.*.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing Matches
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.Middle.Bottom", function () {
var result = bindingsResolver.compare( "*.Middle.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok()
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok()
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok()
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok()
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.Middle.*", function () {
var result = bindingsResolver.compare( "Top.Middle.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.Middle.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "Top.*.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.*.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "*.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.Middle.*", function () {
var result = bindingsResolver.compare( "*.Middle.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.Middle.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.*.*", function () {
var result = bindingsResolver.compare( "*.*.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.*.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With '#' wildcards", function () {
// Passing matches
// # at beginning of binding
describe( "With topic Top.Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.SubTop.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Middle.Bottom" ),
cached = bindingsResolver.cache["Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// # in middle of binding
describe( "With topic Top.Middle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// # at end of binding
describe( "With topic Top.Middle.Bottom and binding Top.Middle.#", function () {
var result = bindingsResolver.compare( "Top.Middle.#", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.Bottom and binding Top.SubTop.#", function () {
var result = bindingsResolver.compare( "Top.SubTop.#", "Top.SubTop.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.Bottom"]["Top.SubTop.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding Middle.#", function () {
var result = bindingsResolver.compare( "Middle.#", "Middle.Bottom" ),
cached = bindingsResolver.cache["Middle.Bottom"]["Middle.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing matches
// # at beginning of binding
describe( "With topic Top.Middle.SubMiddle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding #.Middle.SubMiddle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
// # in middle of binding
describe( "With topic Top.Middle.Bottom and binding Top.SubTop.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.SubTop.#.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"] && bindingsResolver.cache["Top.Middle.Bottom"]["Top.SubTop.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom.SubBottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.SubMiddle.Bottom.SubBottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom.SubBottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom.SubBottom"]["Top.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding Top.#.Middle.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Middle.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["Top.#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Bottom and binding SubTop.#.Bottom", function () {
var result = bindingsResolver.compare( "SubTop.#.Bottom", "Top.SubTop.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Bottom"] && bindingsResolver.cache["Top.SubTop.Bottom"]["SubTop.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
// # at end of binding
describe( "With topic Top.Bottom and binding Top.Middle.#", function () {
var result = bindingsResolver.compare( "Top.Middle.#", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["Top.Middle.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.SubTop.#", function () {
var result = bindingsResolver.compare( "Top.SubTop.#", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"] && bindingsResolver.cache["Top.Middle.Bottom"]["Top.SubTop.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Bottom and binding Middle.#", function () {
var result = bindingsResolver.compare( "Middle.#", "Bottom" ),
cached = bindingsResolver.cache["Bottom"] && bindingsResolver.cache["Bottom"]["Middle.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With both '#' and '*' wildcards", function () {
// Passing matches
describe( "With topic Top.Middle.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding *.#.Bottom", function () {
var result = bindingsResolver.compare( "*.#.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["*.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing matches
describe( "With topic Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Bottom" ),
cached = bindingsResolver.cache["Bottom"] && bindingsResolver.cache["Bottom"]["#.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.Middle.SubMiddle.#.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.Middle.SubMiddle.#.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.Middle.SubMiddle.#.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding #.*.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Middle.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["#.*.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With plain string matching", function () {
describe( "With topic Top.Middle.Bottom and binding Top.Middle.Bottom", function () {
var result = bindingsResolver.compare( "Top.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic 'Topic' and binding 'Topic'", function () {
var result = bindingsResolver.compare( "Topic", "Topic" ),
cached = bindingsResolver.cache["Topic"]["Topic"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic '/sample/topic' and binding '/sample/topic'", function () {
var result = bindingsResolver.compare( "/sample/topic", "/sample/topic" ),
cached = bindingsResolver.cache["/sample/topic"] ? bindingsResolver.cache["/sample/topic"]["/sample/topic"] : null;
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
} );
} );
} );

View file

@ -1,52 +0,0 @@
/* global describe, postal, it, after, before, expect, ChannelDefinition */
describe( "ChannelDefinition", function () {
describe( "When initializing a channel definition", function () {
var chDef = new ChannelDefinition( "TestChannel" );
it( "should set channel to TestChannel", function () {
expect( chDef.channel ).to.be( "TestChannel" );
} );
} );
describe( "When calling subscribe", function () {
var ch = new ChannelDefinition( "TestChannel" ),
sub = ch.subscribe( "TestTopic", function () {} );
it( "subscription should be instance of SubscriptionDefinition", function () {
expect( sub instanceof SubscriptionDefinition ).to.be.ok();
} );
} );
describe( "When publishing from a channel definition", function () {
var channel, subscription;
beforeEach( function () {
channel = postal.channel( "OhHai" );
} );
afterEach( function () {
postal.utils.reset();
channel = undefined;
subscription = undefined;
} );
it( "Should allow a topic only to be used", function ( done ) {
subscription = channel.subscribe( "topic.only", function ( d, e ) {
expect( typeof d === "undefined" ).to.be( true );
expect( e.topic ).to.be( "topic.only" );
done();
} );
channel.publish( "topic.only" );
} );
it( "Should allow a topic and data argument to be used", function ( done ) {
subscription = channel.subscribe( "topic.and.data", function ( d, e ) {
expect( d ).to.be( "hai" );
expect( e.topic ).to.be( "topic.and.data" );
done();
} );
channel.publish( "topic.and.data", "hai" );
} );
it( "Should allow an envelope argument to be used", function ( done ) {
subscription = channel.subscribe( "envelope", function ( d, e ) {
expect( e.channel ).to.be( "OhHai" );
expect( e.data ).to.be( "hai" );
expect( e.foo ).to.be( "bar" );
done();
} );
channel.publish( { topic : "envelope", data : "hai", foo : "bar" } );
} );
} );
} );

View file

@ -1,761 +0,0 @@
/* global describe, postal, it, after, before, expect */
describe( "Postal", function () {
var subscription,
sub,
channel,
caughtSubscribeEvent = false,
caughtUnsubscribeEvent = false;
describe( "When creating basic subscription", function () {
var systemSubscription = {};
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.created",
callback : function ( data, envelope ) {
if ( data.event &&
data.event == "subscription.created" &&
data.channel == "MyChannel" &&
data.topic == "MyTopic" ) {
caughtSubscribeEvent = true;
}
}
} );
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () {} );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
} );
after( function () {
systemSubscription.unsubscribe();
postal.utils.reset();
} );
it( "should create a channel called MyChannel", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"] !== undefined ).to.be.ok();
} );
it( "should create a topic under MyChannel called MyTopic", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"]["MyTopic"] !== undefined ).to.be.ok();
} );
it( "should have set subscription channel value", function () {
expect( sub.channel ).to.be( "MyChannel" );
} );
it( "should have set subscription topic value", function () {
expect( sub.topic ).to.be( "MyTopic" );
} );
it( "should have defaulted the subscription constraints array", function () {
expect( sub.constraints.length ).to.be( 0 );
} );
it( "should have defaulted the subscription context value", function () {
expect( sub.context ).to.be( null );
} );
it( "should have captured subscription creation event", function () {
expect( caughtSubscribeEvent ).to.be.ok();
} );
} );
describe( "When unsubscribing", function () {
describe( "With a single subscription", function () {
var subExistsBefore = false,
subExistsAfter = true;
var systemSubscription = {};
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.*",
callback : function ( data, env ) {
if ( data.event &&
data.event === "subscription.removed" &&
data.channel === "MyChannel" &&
data.topic === "MyTopic" ) {
caughtUnsubscribeEvent = true;
}
}
} );
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () { });
subExistsBefore = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0] !== undefined;
subscription.unsubscribe();
subExistsAfter = postal.configuration.bus.subscriptions.MyChannel.MyTopic.length !== 0;
} );
after( function () {
systemSubscription.unsubscribe();
postal.utils.reset();
} );
it( "subscription should exist before unsubscribe", function () {
expect( subExistsBefore ).to.be.ok();
} );
it( "subscription should not exist after unsubscribe", function () {
expect( subExistsAfter ).to.not.be.ok();
} );
it( "should have captured unsubscription creation event", function () {
expect( caughtUnsubscribeEvent ).to.be.ok();
} );
it( "postal.getSubscribersFor('MyChannel', 'MyTopic') should not return any subscriptions", function () {
expect( postal.utils.getSubscribersFor("MyChannel", "MyTopic").length ).to.be(0);
} );
} );
describe( "With multiple subscribers on one channel", function () {
var subscription1, subscription2, results = [];
before( function () {
channel = postal.channel();
subscription1 = channel.subscribe( 'test',function () {
results.push( '1 received message' );
} ).once();
subscription2 = channel.subscribe( 'test', function () {
results.push( '2 received message' );
} );
channel.publish( 'test' );
channel.publish( 'test' );
} );
after( function () {
subscription2.unsubscribe();
postal.utils.reset();
} );
it( "should produce expected messages", function () {
expect( results.length ).to.be( 3 );
expect( results[0] ).to.be( "1 received message" );
expect( results[1] ).to.be( "2 received message" );
expect( results[2] ).to.be( "2 received message" );
} );
} );
describe( "With nested publishing", function () {
var subscription1, subscription2, sysub, results = [];
before( function () {
channel = postal.channel();
sysub = postal.subscribe( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
callback : function ( d, e ) {
results.push( "unsubscribed" );
}
} );
subscription1 = channel.subscribe( 'nest.test',function () {
results.push( '1 received message' );
channel.publish( "nest.test2", "Hai" );
} ).once();
subscription2 = channel.subscribe( 'nest.test2', function () {
results.push( '2 received message' );
} );
channel.publish( 'nest.test' );
channel.publish( 'nest.test' );
} );
after( function () {
//subscription2.unsubscribe();
sysub.unsubscribe();
postal.utils.reset();
} );
it( "should produce expected messages", function () {
expect( results.length ).to.be( 3 );
expect( results[0] ).to.be( "1 received message" );
expect( results[1] ).to.be( "2 received message" );
expect( results[2] ).to.be( "unsubscribed" );
} );
} );
} );
describe( "When publishing a message", function () {
var msgReceivedCnt = 0,
msgData;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
msgReceivedCnt++;
msgData = data;
} );
channel.publish( "MyTopic", "Testing123" );
subscription.unsubscribe();
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked once", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
it( "subscription callback should receive published data", function () {
expect( msgData ).to.be( "Testing123" );
} );
} );
describe( "When subscribing with a disposeAfter of 5", function () {
var msgReceivedCnt = 0, subExistsAfter, systemSubscription;
before( function () {
caughtUnsubscribeEvent = false;
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function () {
msgReceivedCnt++;
}).disposeAfter( 5 );
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.*",
callback : function ( data, env ) {
if ( data.event &&
data.event === "subscription.removed" &&
data.channel === "MyChannel" &&
data.topic === "MyTopic" ) {
caughtUnsubscribeEvent = true;
}
}
} );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
subExistsAfter = postal.configuration.bus.subscriptions.MyChannel.MyTopic.length !== 0;
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked 5 times", function () {
expect( msgReceivedCnt ).to.be( 5 );
} );
it( "subscription should not exist after unsubscribe", function () {
expect( subExistsAfter ).to.not.be.ok();
} );
it( "should have captured unsubscription creation event", function () {
expect( caughtUnsubscribeEvent ).to.be.ok();
} );
it( "postal.getSubscribersFor('MyChannel', 'MyTopic') should not return any subscriptions", function () {
expect( postal.utils.getSubscribersFor("MyChannel", "MyTopic").length ).to.be(0);
} );
} );
describe( "When subscribing with once()", function () {
var msgReceivedCnt = 0;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic",function ( data ) {
msgReceivedCnt++;
} ).once();
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked 1 time", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
} );
describe( "When subscribing and ignoring duplicates", function () {
var subInvokedCnt = 0;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
subInvokedCnt++;
} )
.distinctUntilChanged();
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
subInvokedCnt = 0;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "subscription callback should be invoked once", function () {
expect( subInvokedCnt ).to.be( 1 );
} );
} );
describe( "When subscribing with one constraint returning true", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraint( function () {
return true;
} );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "should have invoked the subscription callback", function () {
expect( recvd ).to.be.ok();
} );
} );
describe( "When subscribing with one constraint returning false", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraint( function () {
return false;
} );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "should not have invoked the subscription callback", function () {
expect( recvd ).to.not.be.ok();
} );
} );
describe( "When subscribing with multiple constraints returning true", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraints( [function () {
return true;
},
function () {
return true;
},
function () {
return true;
}] );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 3 );
} );
it( "should have invoked the callback", function () {
expect( recvd ).to.be.ok();
} );
} );
describe( "When subscribing with multiple constraints and one returning false", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraints( [function () {
return true;
},
function () {
return false;
},
function () {
return true;
}] );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 3 );
} );
it( "should not have invoked the callback", function () {
expect( recvd ).to.not.be.ok()
} );
} );
describe( "When subscribing with the context being set", function () {
var count = 0,
obj = {
increment : function () {
count++;
}
};
before( function () {
channel = postal.channel( "ContextChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
this.increment();
} )
.withContext( obj );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have called obj.increment", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with defer", function () {
var results = [];
before( function () {
channel = postal.channel( "DeferChannel" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have met expected results", function ( done ) {
subscription = channel.subscribe( "MyTopic",
function ( data ) {
results.push( "second" );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
done();
} ).defer();
channel.publish( "MyTopic", "Testing123" );
results.push( "first" );
} );
} );
describe( "When subscribing with delay", function () {
var results = [];
before( function () {
channel = postal.channel( "DelayChannel" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have met expected results", function ( done ) {
subscription = channel.subscribe( "MyTopic",
function ( data ) {
results.push( "second" );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
done();
} ).withDelay( 500 );
channel.publish( "MyTopic", "Testing123" );
results.push( "first" );
} );
} );
describe( "When subscribing with debounce", function () {
var results = [], debouncedChannel;
before( function () {
debouncedChannel = postal.channel( "DebounceChannel" );
subscription = debouncedChannel.subscribe( "MyTopic",
function ( data ) {
results.push( data );
} ).withDebounce( 800 );
} );
after( function () {
postal.utils.reset();
} );
it( "should have only invoked debounced callback once", function ( done ) {
debouncedChannel.publish( "MyTopic", 1 ); // starts the two second clock on debounce
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 2 );
}, 20 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 3 );
}, 80 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 4 );
}, 250 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 5 );
}, 500 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 6 );
}, 1000 ); // should invoke callback
setTimeout( function () {
expect( results[0] ).to.be( 6 );
expect( results.length ).to.be( 1 );
done();
}, 2400 );
} );
} );
describe( "When subscribing with throttle", function () {
var results = [], throttledChannel;
before( function () {
throttledChannel = postal.channel( "ThrottleChannel" );
subscription = throttledChannel.subscribe( "MyTopic",
function ( data ) {
results.push( data );
} ).withThrottle( 500 );
} );
after( function () {
postal.utils.reset();
} );
it( "should have only invoked throttled callback twice", function ( done ) {
throttledChannel.publish( "MyTopic", 1 ); // starts the two second clock on debounce
setTimeout( function () {
throttledChannel.publish( "MyTopic", 800 );
}, 800 ); // should invoke callback
for ( var i = 0; i < 20; i++ ) {
(function ( x ) {
throttledChannel.publish( "MyTopic", x );
})( i );
}
setTimeout( function () {
expect( results[0] ).to.be( 1 );
expect( results[1] ).to.be( 800 );
expect( results.length ).to.be( 2 );
done();
}, 1500 );
} );
} );
describe( "When subscribing with a hierarchical binding, no wildcards", function () {
var count = 0, channelB, channelC;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.MiddleTopic", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback only once", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with a hierarchical binding, using #", function () {
var count, channelB, channelC, channelD, channelE;
before( function () {
count = 0;
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.#.SubTopic", function ( data, env ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubMiddle.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 2 );
} );
} );
describe( "When subscribing with a hierarchical binding, using *", function () {
var count = 0, channelB, channelC, channelD;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.MiddleTopic.*", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with a hierarchical binding, using # and *", function () {
var count = 0, channelB, channelC, channelD, channelE;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.#.*", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
channel.publish( "OtherTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 3 );
} );
} );
describe( "When using global publish api", function () {
var msgReceivedCnt = 0,
msgData;
before( function () {
channel = postal.channel( "MyGlobalChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
msgReceivedCnt++;
msgData = data;
} );
postal.publish( { channel : "MyGlobalChannel", topic : "MyTopic", data : "Testing123" } );
subscription.unsubscribe();
postal.publish( { channel : "MyGlobalChannel", topic : "MyTopic", data : "Testing123" } );
} );
after( function () {
postal.utils.reset();
msgReceivedCnt = 0;
} );
it( "channel should be of type ChannelDefinition", function () {
expect( channel instanceof ChannelDefinition ).to.be.ok();
} );
it( "subscription callback should be invoked once", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
it( "subscription callback should receive published data", function () {
expect( msgData ).to.be( "Testing123" );
} );
} );
describe( "When using global subscribe api", function () {
before( function () {
subscription = postal.subscribe( {
channel : "MyChannel",
topic : "MyTopic",
callback : function () {
}
} );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
} );
after( function () {
postal.utils.reset();
} );
it( "subscription should be of type SubscriptionDefinition", function () {
expect( subscription instanceof SubscriptionDefinition ).to.be.ok();
} );
it( "should create an channel called MyChannel", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"] !== undefined ).to.be.ok();
} );
it( "should create a topic under MyChannel called MyTopic", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"]["MyTopic"] !== undefined ).to.be.ok();
} );
it( "should have set subscription channel value", function () {
expect( sub.channel ).to.be( "MyChannel" );
} );
it( "should have set subscription topic value", function () {
expect( sub.topic ).to.be( "MyTopic" );
} );
it( "should have defaulted the subscription constraints array", function () {
expect( sub.constraints.length ).to.be( 0 );
} );
it( "should have defaulted the subscription context value", function () {
expect( sub.context ).to.be( null );
} );
} );
describe( "When using global channel api", function () {
var gch;
describe( "With no channel name provided", function () {
describe( "Using string argument", function () {
before( function () {
gch = postal.channel( "SomeChannel" );
} );
after( function () {
gch = undefined;
} );
it( "channel should be of type ChannelDefinition", function () {
expect( gch instanceof ChannelDefinition ).to.be.ok();
} );
it( "should set channel name to SomeChannel", function () {
expect( gch.channel ).to.be( "SomeChannel" );
} );
} );
} );
} );
describe( "When subscribing and unsubscribing a wire tap", function () {
var wireTapData,
wireTapEnvelope,
wiretap;
before( function () {
caughtUnsubscribeEvent = false;
wireTapData = [];
wireTapEnvelope = [];
wiretap = postal.addWireTap( function ( msg, envelope ) {
wireTapData.push( msg );
wireTapEnvelope.push( envelope );
} );
postal.publish( { topic : "Oh.Hai.There", data : "I'm in yer bus, tappin' yer subscriptionz..."} );
wiretap();
postal.publish( { topic : "Oh.Hai.There", data : "I'm in yer bus, tappin' yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "wire tap should have been invoked only once", function () {
expect( wireTapData.length ).to.be( 1 );
expect( wireTapEnvelope.length ).to.be( 1 );
} );
it( "wireTap data should match expected results", function () {
expect( wireTapData[0] ).to.be( "I'm in yer bus, tappin' yer subscriptionz..." );
} );
it( "wireTap envelope should match expected results", function () {
expect( wireTapEnvelope[0].channel ).to.be( postal.configuration.DEFAULT_CHANNEL );
expect( wireTapEnvelope[0].topic ).to.be( "Oh.Hai.There" );
} );
} );
describe( "When calling postal.utils.reset", function () {
var resolver;
before( function () {
postal.utils.reset();
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () {
} );
postal.channel( "MyChannel" ).publish( "MyTopic", "Oh Hai!" );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
resolver = postal.configuration.resolver.cache.MyTopic;
postal.utils.reset();
} );
after( function () {
} );
it( "should have created a subscription definition", function () {
expect( sub.channel ).to.be( "MyChannel" );
expect( sub.topic ).to.be( "MyTopic" );
expect( sub.constraints.length ).to.be( 0 );
expect( sub.context ).to.be( null );
} );
it( "should have created a resolver cache entry", function () {
expect( _.isEmpty( resolver ) ).to.not.be.ok()
expect( resolver["MyTopic"] ).to.be.ok();
} );
it( "subscriptions cache should now be empty", function () {
expect( _.isEmpty( postal.configuration.bus.subscriptions ) ).to.be.ok();
} );
it( "resolver cache should now be empty", function () {
expect( _.isEmpty( postal.configuration.resolver.cache ) ).to.be.ok();
} );
} );
describe( "When calling utils.getSubscribersFor", function () {
var subs = [], i;
before( function () {
i = 10;
var ch1 = postal.channel( "MyChannel" ),
ch2 = postal.channel( "MyChannel2" );
while ( i ) {
subs.push( ch1.subscribe( "MyTopic", function () {
} ) );
subs.push( ch2.subscribe( "MyTopic2", function () {
} ) );
i--;
}
} );
after( function () {
sub = [];
postal.utils.reset();
} );
it( "should return expected results for MyChannel/MyTopic", function () {
var results = postal.utils.getSubscribersFor( { channel : "MyChannel", topic : "MyTopic" } );
expect( results.length ).to.be( 10 );
} );
it( "should return expected results for MyChannel2/MyTopic2", function () {
var results = postal.utils.getSubscribersFor( { channel : "MyChannel2", topic : "MyTopic2" } );
expect( results.length ).to.be( 10 );
} );
} );
describe( "When publishing on a channel where no subscribers exist", function () {
it( "should return expected results for MyChannel/MyTopic", function () {
var env = postal.publish( {
channel : "NoOneIsUsingThisOne",
topic : "This.Is.A.Lonely.Topic",
data : "Y U NO SUBSCRIBE TO ME?"
} );
expect( !_.isEmpty( env ) ).to.be( true );
} );
} );
} );

View file

@ -1,275 +0,0 @@
/* global describe, postal, it, after, before, expect, SubscriptionDefinition */
var NO_OP = function () {};
describe( "SubscriptionDefinition", function () {
describe( "When initializing SubscriptionDefinition", function () {
var sDef,
caughtSubscribeEvent,
systemSubscription;
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.created",
callback : function ( data, envelope ) {
if ( data.event &&
data.event === "subscription.created" &&
data.channel === "SubDefTestChannel" &&
data.topic === "SubDefTestTopic" ) {
caughtSubscribeEvent = true;
}
}
} );
sDef = new SubscriptionDefinition( "SubDefTestChannel", "SubDefTestTopic", NO_OP );
} );
after( function () {
sDef.unsubscribe();
systemSubscription.unsubscribe();
caughtSubscribeEvent = false;
} );
it( "should set the channel to SubDefTestChannel", function () {
expect( sDef.channel ).to.be( "SubDefTestChannel" );
} );
it( "should set the topic to SubDefTestTopic", function () {
expect( sDef.topic ).to.be( "SubDefTestTopic" );
} );
it( "should set the callback", function () {
expect( sDef.callback ).to.be( NO_OP );
} );
it( "should default the constraints", function () {
expect( sDef.constraints.length ).to.be( 0 );
} );
it( "should default the context", function () {
expect( sDef.context ).to.be( null );
} );
it( "should fire the subscription.created message", function () {
expect( caughtSubscribeEvent ).to.be( true );
} );
} );
describe( "When setting distinctUntilChanged", function () {
var sDefa = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).distinctUntilChanged();
it( "Should add a DistinctPredicate constraint to the configuration constraints", function () {
expect( sDefa.constraints.length ).to.be( 1 );
} );
} );
describe( "When adding a constraint", function () {
var sDefb = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).withConstraint( function () {
} );
it( "Should add a constraint", function () {
expect( sDefb.constraints.length ).to.be( 1 );
} );
} );
describe( "When adding multiple constraints", function () {
var sDefc = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).withConstraints( [function () {
}, function () {
}, function () {
}] );
it( "Should add a constraint", function () {
expect( sDefc.constraints.length ).to.be( 3 );
} );
} );
describe( "When setting the context", function () {
var obj = { name : "Rose" },
name,
sDefd = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP )
.withContext( obj )
.withConstraint( function ( d, e ) {
name = this.name;
return true;
} );
postal.publish( { channel : "TestChannel", topic : "TestTopic", data : "Oh, hai"} )
it( "Should set context", function () {
expect( sDefd.context ).to.be( obj );
} );
it( "Should apply context to predicate/constraint", function () {
expect( name ).to.be( "Rose" );
} );
} );
describe( "When calling subscribe to set the callback", function () {
var sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ),
fn = function () {
};
sDefe.subscribe( fn );
it( "Should set the callback", function () {
expect( sDefe.callback ).to.be( fn );
} );
} );
describe( "When deferring the callback", function () {
var results = [], sDefe;
it( "Should defer the callback", function ( done ) {
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
results.push( data );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
expect( env.topic ).to.be( "TestTopic" );
done();
} ).defer();
sDefe.callback( "second", { topic : "TestTopic" } );
results.push( "first" );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).defer();
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
it( "Should keep the context intact when modified later", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).defer().withContext(context);
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
describe( "When throttling the callback", function () {
var results = [],
sDefe = new SubscriptionDefinition( "ThrottleTest", "TestTopic", function ( data ) {
results.push( data );
} ).withThrottle( 500 );
var timeout1, timeout2;
it( "should have only invoked throttled callback twice", function ( done ) {
sDefe.callback( 1 ); // starts the two second clock on debounce
timeout1 = setTimeout( function () {
sDefe.callback( 800 );
}, 900 ); // should invoke callback
for ( var i = 0; i < 20; i++ ) {
(function ( x ) {
sDefe.callback( x );
})( i );
}
timeout2 = setTimeout( function () {
expect( results[0] ).to.be( 1 );
expect( results[1] ).to.be( 800 );
expect( results.length ).to.be( 2 );
done();
}, 1500 );
} );
it( "Should keep the context intact", function( done ) {
var context = {
key : 'abcd'
};
sDefe = new SubscriptionDefinition( "ThrottleTest", "TestTopic", function( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext( context ).withThrottle( 500 );
sDefe.callback.call( sDefe.context, 1 );
} );
} );
describe( "When delaying the callback", function () {
var results = [], sDefe;
it( "Should delay the callback", function ( done ) {
sDefe = new SubscriptionDefinition( "DelayTest", "TestTopic", function ( data, env ) {
results.push( data );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
expect( env.topic ).to.be( "TestTopic" );
done();
} ).withDelay( 200 );
sDefe.callback( "second", { topic : "TestTopic" } );
results.push( "first" );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "DelayTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).withDelay( 200 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
describe( "When debouncing the callback", function () {
var results = [],
sDefe = new SubscriptionDefinition( "DebounceTest", "TestTopic", function ( data ) {
results.push( data );
} ).withDebounce( 800 );
it( "should have only invoked debounced callback once", function ( done ) {
sDefe.callback( 1 ); // starts the two second clock on debounce
setTimeout( function () {
sDefe.callback( 2 );
}, 20 ); // should not invoke callback
setTimeout( function () {
sDefe.callback( 3 );
}, 80 ); // should not invoke callback
setTimeout( function () {
sDefe.callback( 6 );
}, 800 ); // should invoke callback
setTimeout( function () {
expect( results[0] ).to.be( 6 );
expect( results.length ).to.be( 1 );
done();
}, 2300 );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 5678
};
var timeout;
sDefe = new SubscriptionDefinition( "DebounceTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
clearTimeout(timeout);
done();
} ).withContext(context).withDebounce( 100 );
sDefe.callback.call( sDefe.context, 1 );
timeout = setTimeout( function () {
sDefe.callback.call( sDefe.context, 2 );
}, 200 ); // should invoke callback
});
} );
describe( "When self disposing", function () {
var context = {
key : 'abcd'
};
it( "Should be inactive", function () {
var sDefe = new SubscriptionDefinition( "DisposeTest", "TestTopic", function ( data, env ) {
} ).withContext(context).disposeAfter( 1 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
expect( sDefe.inactive ).to.be( true );
} );
it( "Should keep the context intact", function ( done ) {
var sDefe = new SubscriptionDefinition( "DisposeTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).disposeAfter( 200 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
} );

View file

@ -0,0 +1,473 @@
/* global describe, postal, it, after, before, expect */
(function(){
var bindingsResolver = postal.configuration.resolver;
describe( "amqpBindingsResolver", function () {
describe( "When calling compare", function () {
before( function () {
bindingsResolver.reset();
});
describe( "With '*' wildcards", function () {
// Passing matches
describe( "With topic Top.Middle.Bottom and binding *.Middle.Bottom", function () {
var result = bindingsResolver.compare( "*.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.Middle.*", function () {
var result = bindingsResolver.compare( "Top.Middle.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "Top.*.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.*.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "*.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding *.Middle.*", function () {
var result = bindingsResolver.compare( "*.Middle.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.Middle.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding *.*.*", function () {
var result = bindingsResolver.compare( "*.*.*", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["*.*.*"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing Matches
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.Middle.Bottom", function () {
var result = bindingsResolver.compare( "*.Middle.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok()
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok()
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok()
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok()
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.Middle.*", function () {
var result = bindingsResolver.compare( "Top.Middle.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.Middle.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "Top.*.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.*.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.*.*", function () {
var result = bindingsResolver.compare( "*.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.Middle.*", function () {
var result = bindingsResolver.compare( "*.Middle.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.Middle.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding *.*.*", function () {
var result = bindingsResolver.compare( "*.*.*", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["*.*.*"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With '#' wildcards", function () {
// Passing matches
// # at beginning of binding
describe( "With topic Top.Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.SubTop.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Middle.Bottom" ),
cached = bindingsResolver.cache["Middle.Bottom"]["#.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// # in middle of binding
describe( "With topic Top.Middle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["Top.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// # at end of binding
describe( "With topic Top.Middle.Bottom and binding Top.Middle.#", function () {
var result = bindingsResolver.compare( "Top.Middle.#", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.Bottom and binding Top.SubTop.#", function () {
var result = bindingsResolver.compare( "Top.SubTop.#", "Top.SubTop.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.Bottom"]["Top.SubTop.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding Middle.#", function () {
var result = bindingsResolver.compare( "Middle.#", "Middle.Bottom" ),
cached = bindingsResolver.cache["Middle.Bottom"]["Middle.#"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing matches
// # at beginning of binding
describe( "With topic Top.Middle.SubMiddle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding #.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Middle.Bottom and binding #.Middle.SubMiddle.Bottom", function () {
var result = bindingsResolver.compare( "#.Middle.Bottom", "Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Middle.SubMiddle.Bottom"]["#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
// # in middle of binding
describe( "With topic Top.Middle.Bottom and binding Top.SubTop.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.SubTop.#.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"] && bindingsResolver.cache["Top.Middle.Bottom"]["Top.SubTop.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom.SubBottom and binding Top.#.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Bottom", "Top.Middle.SubMiddle.Bottom.SubBottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom.SubBottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom.SubBottom"]["Top.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Middle.SubMiddle.Bottom and binding Top.#.Middle.Bottom", function () {
var result = bindingsResolver.compare( "Top.#.Middle.Bottom", "Top.SubTop.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.SubTop.Middle.SubMiddle.Bottom"]["Top.#.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.SubTop.Bottom and binding SubTop.#.Bottom", function () {
var result = bindingsResolver.compare( "SubTop.#.Bottom", "Top.SubTop.Bottom" ),
cached = bindingsResolver.cache["Top.SubTop.Bottom"] && bindingsResolver.cache["Top.SubTop.Bottom"]["SubTop.#.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
// # at end of binding
describe( "With topic Top.Bottom and binding Top.Middle.#", function () {
var result = bindingsResolver.compare( "Top.Middle.#", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["Top.Middle.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.Bottom and binding Top.SubTop.#", function () {
var result = bindingsResolver.compare( "Top.SubTop.#", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"] && bindingsResolver.cache["Top.Middle.Bottom"]["Top.SubTop.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Bottom and binding Middle.#", function () {
var result = bindingsResolver.compare( "Middle.#", "Bottom" ),
cached = bindingsResolver.cache["Bottom"] && bindingsResolver.cache["Bottom"]["Middle.#"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With both '#' and '*' wildcards", function () {
// Passing matches
describe( "With topic Top.Middle.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"]["#.*.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding *.#.Bottom", function () {
var result = bindingsResolver.compare( "*.#.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["*.#.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
// Failing matches
describe( "With topic Bottom and binding #.*.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Bottom", "Bottom" ),
cached = bindingsResolver.cache["Bottom"] && bindingsResolver.cache["Bottom"]["#.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Middle.SubMiddle.Bottom and binding Top.Middle.SubMiddle.#.*.Bottom", function () {
var result = bindingsResolver.compare( "Top.Middle.SubMiddle.#.*.Bottom", "Top.Middle.SubMiddle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"] && bindingsResolver.cache["Top.Middle.SubMiddle.Bottom"]["Top.Middle.SubMiddle.#.*.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
describe( "With topic Top.Bottom and binding #.*.Middle.Bottom", function () {
var result = bindingsResolver.compare( "#.*.Middle.Bottom", "Top.Bottom" ),
cached = bindingsResolver.cache["Top.Bottom"] && bindingsResolver.cache["Top.Bottom"]["#.*.Middle.Bottom"];
it( "Result should be false", function () {
expect( result ).to.not.be.ok();
} );
it( "Should *not* create a resolver cache entry", function () {
expect( cached ).to.not.be.ok();
} );
} );
} );
describe( "With plain string matching", function () {
describe( "With topic Top.Middle.Bottom and binding Top.Middle.Bottom", function () {
var result = bindingsResolver.compare( "Top.Middle.Bottom", "Top.Middle.Bottom" ),
cached = bindingsResolver.cache["Top.Middle.Bottom"]["Top.Middle.Bottom"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic 'Topic' and binding 'Topic'", function () {
var result = bindingsResolver.compare( "Topic", "Topic" ),
cached = bindingsResolver.cache["Topic"]["Topic"];
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
describe( "With topic '/sample/topic' and binding '/sample/topic'", function () {
var result = bindingsResolver.compare( "/sample/topic", "/sample/topic" ),
cached = bindingsResolver.cache["/sample/topic"] ? bindingsResolver.cache["/sample/topic"]["/sample/topic"] : null;
it( "Result should be true", function () {
expect( result ).to.be.ok();
} );
it( "Should create a resolver cache entry", function () {
expect( cached ).to.be.ok();
} );
} );
} );
} );
} );
}());

View file

@ -0,0 +1,74 @@
/* global describe, postal, it, after, before, expect */
(function(){
var ChannelDefinition = postal.ChannelDefinition;
describe( "channel definition", function () {
describe( "When using global channel api", function () {
var gch;
describe( "With no channel name provided", function () {
describe( "Using string argument", function () {
before( function () {
gch = postal.channel( "SomeChannel" );
} );
after( function () {
gch = undefined;
} );
it( "channel should be of type ChannelDefinition", function () {
expect( gch instanceof ChannelDefinition ).to.be.ok();
} );
it( "should set channel name to SomeChannel", function () {
expect( gch.channel ).to.be( "SomeChannel" );
} );
} );
} );
} );
describe( "When initializing a channel definition", function () {
var chDef = new ChannelDefinition( "TestChannel" );
it( "should set channel to TestChannel", function () {
expect( chDef.channel ).to.be( "TestChannel" );
} );
} );
describe( "When calling subscribe", function () {
var ch = new ChannelDefinition( "TestChannel" ),
sub = ch.subscribe( "TestTopic", function () {} );
it( "subscription should be instance of SubscriptionDefinition", function () {
expect( sub instanceof postal.SubscriptionDefinition ).to.be.ok();
} );
} );
describe( "When publishing from a channel definition", function () {
var channel, subscription;
before( function () {
channel = postal.channel( "OhHai" );
} );
after( function () {
postal.utils.reset();
channel = undefined;
subscription = undefined;
} );
it( "Should allow a topic only to be used", function ( done ) {
subscription = channel.subscribe( "topic.only", function ( d, e ) {
expect( typeof d === "undefined" ).to.be( true );
expect( e.topic ).to.be( "topic.only" );
done();
} );
channel.publish( "topic.only" );
} );
it( "Should allow a topic and data argument to be used", function ( done ) {
subscription = channel.subscribe( "topic.and.data", function ( d, e ) {
expect( d ).to.be( "hai" );
expect( e.topic ).to.be( "topic.and.data" );
done();
} );
channel.publish( "topic.and.data", "hai" );
} );
it( "Should allow an envelope argument to be used", function ( done ) {
subscription = channel.subscribe( "envelope", function ( d, e ) {
expect( e.channel ).to.be( "OhHai" );
expect( e.data ).to.be( "hai" );
expect( e.foo ).to.be( "bar" );
done();
} );
channel.publish( { topic : "envelope", data : "hai", foo : "bar" } );
} );
} );
} );
}());

View file

@ -1,34 +1,25 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="../ext/mocha.css" />
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="mocha"></div>
<script type="text/javascript" src="../ext/jquery-1.7.1.js"></script>
<script type="text/javascript" src="../ext/underscore.js"></script>
<script type="text/javascript" src="../ext/expect.js"></script>
<script type="text/javascript" src="../ext/mocha.js"></script>
<script type="text/javascript" src="../node_modules/mocha/mocha.js"></script>
<script type="text/javascript">
mocha.setup({ ui: 'bdd', timeout: 60000 });
</script>
<script type="text/javascript" src="../ext/amplify.core.js"></script>
<script type="text/javascript" src="../ext/amplify.store.js"></script>
<script type="text/javascript" src="../ext/underscore.js"></script>
<script type="text/javascript" src="../src/DistinctPredicate.js"></script>
<script type="text/javascript" src="../src/ConsecutiveDistinctPredicate.js"></script>
<script type="text/javascript" src="../src/ChannelDefinition.js"></script>
<script type="text/javascript" src="../src/SubscriptionDefinition.js"></script>
<script type="text/javascript" src="../src/AmqpBindingsResolver.js"></script>
<script type="text/javascript" src="../src/LocalBus.js"></script>
<script type="text/javascript" src="../src/Api.js"></script>
<script type="text/javascript" src="DistinctPredicate.spec.js"></script>
<script type="text/javascript" src="ConsecutiveDistinctPredicate.spec.js"></script>
<script type="text/javascript" src="SubscriptionDefinition.spec.js"></script>
<script type="text/javascript" src="ChannelDefinition.spec.js"></script>
<script type="text/javascript" src="AmqpBindingsResolver.spec.js"></script>
<script type="text/javascript" src="Postal.spec.js"></script>
<script type="text/javascript" src="../lib/postal.js"></script>
<script type="text/javascript" src="postaljs.spec.js"></script>
<script type="text/javascript" src="utils.spec.js"></script>
<script type="text/javascript" src="linkedChannels.spec.js"></script>
<script type="text/javascript" src="bindingsResolver.spec.js"></script>
<script type="text/javascript" src="channelDefinition.spec.js"></script>
<script type="text/javascript" src="subscriptionDefinition.spec.js"></script>
<script type="text/javascript">
mocha.run();
</script>

View file

@ -1,122 +1,124 @@
/* global describe, postal, it, after, before, expect */
var subscription, linkages;
describe( "Linked Channels", function () {
describe( "When binding channel - one source to one destination", function () {
describe( "with only channel values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel" } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "Oh.Hai.There", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..." } );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "Oh.Hai.There" );
} );
} );
describe( "with channel and static topic values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel", topic : "Oh.Hai.There" }, { channel : "destinationChannel", topic : "kthxbye" } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "kthxbye", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "kthxbye" );
} );
} );
describe( "with channel and topic transform values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel", topic : function ( tpc ) {
return "NewTopic." + tpc;
} } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "NewTopic.Oh.Hai.There" );
} );
} );
} );
describe( "When binding channel - one source to multiple destinations", function () {
var destData = [],
destEnv = [],
callback = function ( data, env ) {
destData.push( data );
destEnv.push( env );
};
(function() {
var subscription;
describe("linked channels", function () {
describe( "When binding channel - one source to one destination", function () {
describe( "with only channel values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel" } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "Oh.Hai.There", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..." } );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "Oh.Hai.There" );
} );
} );
describe( "with channel and static topic values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel", topic : "Oh.Hai.There" }, { channel : "destinationChannel", topic : "kthxbye" } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "kthxbye", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "kthxbye" );
} );
} );
describe( "with channel and topic transform values provided", function () {
var destData = [],
destEnv = [],
linkages;
before( function () {
linkages = postal.linkChannels( { channel : "sourceChannel" }, { channel : "destinationChannel", topic : function ( tpc ) {
return "NewTopic." + tpc;
} } );
subscription = postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : function ( data, env ) {
destData.push( data );
destEnv.push( env );
}} );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
linkages[0].unsubscribe();
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscription should only have been invoked once", function () {
expect( destData.length ).to.be( 1 );
expect( destEnv.length ).to.be( 1 );
} );
it( "linked subscription data should match expected results", function () {
expect( destData[0] ).to.be( "I'm in yer bus, linkin' to yer subscriptionz..." );
} );
it( "linked subscription envelope should match expected results", function () {
expect( destEnv[0].channel ).to.be( "destinationChannel" );
expect( destEnv[0].topic ).to.be( "NewTopic.Oh.Hai.There" );
} );
} );
} );
describe( "When binding channel - one source to multiple destinations", function () {
var destData = [],
destEnv = [],
callback = function ( data, env ) {
destData.push( data );
destEnv.push( env );
};
before( function () {
linkages = postal.linkChannels(
{ channel : "sourceChannel", topic : "Oh.Hai.There" },
[
{ channel : "destinationChannel", topic : "NewTopic.Oh.Hai" },
{ channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There" }
] );
postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai", callback : callback} );
postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : callback } );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscriptions should each have been called once", function () {
expect( destData.length ).to.be( 2 );
expect( destEnv.length ).to.be( 2 );
} );
} );
} );
before( function () {
linkages = postal.linkChannels(
{ channel : "sourceChannel", topic : "Oh.Hai.There" },
[
{ channel : "destinationChannel", topic : "NewTopic.Oh.Hai" },
{ channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There" }
] );
postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai", callback : callback} );
postal.subscribe( { channel : "destinationChannel", topic : "NewTopic.Oh.Hai.There", callback : callback } );
postal.publish( { channel : "sourceChannel", topic : "Oh.Hai.There", data : "I'm in yer bus, linkin' to yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "linked subscriptions should each have been called once", function () {
expect( destData.length ).to.be( 2 );
expect( destEnv.length ).to.be( 2 );
} );
} );
} );
}());

694
spec/postaljs.spec.js Normal file
View file

@ -0,0 +1,694 @@
/* global describe, postal, it, after, before, expect */
(function() {
var subscription;
var sub;
var channel;
var caughtSubscribeEvent = false;
var caughtUnsubscribeEvent = false;
describe("subscription creation", function(){
describe( "When creating basic subscription", function () {
var systemSubscription = {};
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.created",
callback : function ( data, envelope ) {
if ( data.event &&
data.event == "subscription.created" &&
data.channel == "MyChannel" &&
data.topic == "MyTopic" ) {
caughtSubscribeEvent = true;
}
}
} );
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () {} );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
} );
after( function () {
systemSubscription.unsubscribe();
postal.utils.reset();
} );
it( "should create a channel called MyChannel", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"] !== undefined ).to.be.ok();
} );
it( "should create a topic under MyChannel called MyTopic", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"]["MyTopic"] !== undefined ).to.be.ok();
} );
it( "should have set subscription channel value", function () {
expect( sub.channel ).to.be( "MyChannel" );
} );
it( "should have set subscription topic value", function () {
expect( sub.topic ).to.be( "MyTopic" );
} );
it( "should have defaulted the subscription constraints array", function () {
expect( sub.constraints.length ).to.be( 0 );
} );
it( "should have defaulted the subscription context value", function () {
expect( sub.context ).to.be( null );
} );
it( "should have captured subscription creation event", function () {
expect( caughtSubscribeEvent ).to.be.ok();
} );
} );
describe( "When subscribing and ignoring duplicates", function () {
var subInvokedCnt = 0;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
subInvokedCnt++;
} )
.distinctUntilChanged();
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
subInvokedCnt = 0;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "subscription callback should be invoked once", function () {
expect( subInvokedCnt ).to.be( 1 );
} );
} );
describe( "When subscribing with a disposeAfter of 5", function () {
var msgReceivedCnt = 0, subExistsAfter, systemSubscription;
before( function () {
caughtUnsubscribeEvent = false;
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function () {
msgReceivedCnt++;
}).disposeAfter( 5 );
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.*",
callback : function ( data, env ) {
if ( data.event &&
data.event === "subscription.removed" &&
data.channel === "MyChannel" &&
data.topic === "MyTopic" ) {
caughtUnsubscribeEvent = true;
}
}
} );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
subExistsAfter = postal.configuration.bus.subscriptions.MyChannel.MyTopic.length !== 0;
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked 5 times", function () {
expect( msgReceivedCnt ).to.be( 5 );
} );
it( "subscription should not exist after unsubscribe", function () {
expect( subExistsAfter ).to.not.be.ok();
} );
it( "should have captured unsubscription creation event", function () {
expect( caughtUnsubscribeEvent ).to.be.ok();
} );
it( "postal.getSubscribersFor('MyChannel', 'MyTopic') should not return any subscriptions", function () {
expect( postal.utils.getSubscribersFor("MyChannel", "MyTopic").length ).to.be(0);
} );
} );
describe( "When subscribing with a hierarchical binding, no wildcards", function () {
var count = 0, channelB, channelC;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.MiddleTopic", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback only once", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with a hierarchical binding, using #", function () {
var count, channelB, channelC, channelD, channelE;
before( function () {
count = 0;
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.#.SubTopic", function ( data, env ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubMiddle.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 2 );
} );
} );
describe( "When subscribing with a hierarchical binding, using *", function () {
var count = 0, channelB, channelC, channelD;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.MiddleTopic.*", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with a hierarchical binding, using # and *", function () {
var count = 0, channelB, channelC, channelD, channelE;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic.#.*", function ( data ) {
count++;
} );
channel.publish( "MyTopic.MiddleTopic.SubTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic", "Testing123" );
channel.publish( "MyTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
channel.publish( "OtherTopic.MiddleTopic.SubTopic.YetAnother", "Testing123" );
} );
after( function () {
postal.utils.reset();
count = 0;
} );
it( "should have invoked subscription callback twice", function () {
expect( count ).to.be( 3 );
} );
} );
describe( "When subscribing with debounce", function () {
var results = [], debouncedChannel;
before( function () {
debouncedChannel = postal.channel( "DebounceChannel" );
subscription = debouncedChannel.subscribe( "MyTopic",
function ( data ) {
results.push( data );
} ).withDebounce( 800 );
} );
after( function () {
postal.utils.reset();
} );
it( "should have only invoked debounced callback once", function ( done ) {
debouncedChannel.publish( "MyTopic", 1 ); // starts the two second clock on debounce
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 2 );
}, 20 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 3 );
}, 80 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 4 );
}, 250 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 5 );
}, 500 ); // should not invoke callback
setTimeout( function () {
debouncedChannel.publish( "MyTopic", 6 );
}, 1000 ); // should invoke callback
setTimeout( function () {
expect( results[0] ).to.be( 6 );
expect( results.length ).to.be( 1 );
done();
}, 2400 );
} );
} );
describe( "When subscribing with defer", function () {
var results = [];
before( function () {
channel = postal.channel( "DeferChannel" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have met expected results", function ( done ) {
subscription = channel.subscribe( "MyTopic",
function ( data ) {
results.push( "second" );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
done();
} ).defer();
channel.publish( "MyTopic", "Testing123" );
results.push( "first" );
} );
} );
describe( "When subscribing with delay", function () {
var results = [];
before( function () {
channel = postal.channel( "DelayChannel" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have met expected results", function ( done ) {
subscription = channel.subscribe( "MyTopic",
function ( data ) {
results.push( "second" );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
done();
} ).withDelay( 500 );
channel.publish( "MyTopic", "Testing123" );
results.push( "first" );
} );
} );
describe( "When subscribing with once()", function () {
var msgReceivedCnt = 0;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic",function ( data ) {
msgReceivedCnt++;
} ).once();
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked 1 time", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
} );
describe( "When subscribing with multiple constraints returning true", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraints( [function () {
return true;
},
function () {
return true;
},
function () {
return true;
}] );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 3 );
} );
it( "should have invoked the callback", function () {
expect( recvd ).to.be.ok();
} );
} );
describe( "When subscribing with multiple constraints and one returning false", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraints( [function () {
return true;
},
function () {
return false;
},
function () {
return true;
}] );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 3 );
} );
it( "should not have invoked the callback", function () {
expect( recvd ).to.not.be.ok()
} );
} );
describe( "When subscribing with one constraint returning false", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraint( function () {
return false;
} );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "should not have invoked the subscription callback", function () {
expect( recvd ).to.not.be.ok();
} );
} );
describe( "When subscribing with one constraint returning true", function () {
var recvd = false;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
recvd = true;
} )
.withConstraint( function () {
return true;
} );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
recvd = false;
} );
it( "should have a constraint on the subscription", function () {
expect( postal.configuration.bus.subscriptions.MyChannel.MyTopic[0].constraints.length ).to.be( 1 );
} );
it( "should have invoked the subscription callback", function () {
expect( recvd ).to.be.ok();
} );
} );
describe( "When subscribing with the context being set", function () {
var count = 0,
obj = {
increment : function () {
count++;
}
};
before( function () {
channel = postal.channel( "ContextChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
this.increment();
} )
.withContext( obj );
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "should have called obj.increment", function () {
expect( count ).to.be( 1 );
} );
} );
describe( "When subscribing with throttle", function () {
var results = [], throttledChannel;
before( function () {
throttledChannel = postal.channel( "ThrottleChannel" );
subscription = throttledChannel.subscribe( "MyTopic",
function ( data ) {
results.push( data );
} ).withThrottle( 500 );
} );
after( function () {
postal.utils.reset();
} );
it( "should have only invoked throttled callback twice", function ( done ) {
throttledChannel.publish( "MyTopic", 1 ); // starts the two second clock on debounce
setTimeout( function () {
throttledChannel.publish( "MyTopic", 800 );
}, 800 ); // should invoke callback
for ( var i = 0; i < 20; i++ ) {
(function ( x ) {
throttledChannel.publish( "MyTopic", x );
})( i );
}
setTimeout( function () {
expect( results[0] ).to.be( 1 );
expect( results[1] ).to.be( 800 );
expect( results.length ).to.be( 2 );
done();
}, 1500 );
} );
} );
describe( "When using global subscribe api", function () {
before( function () {
subscription = postal.subscribe( {
channel : "MyChannel",
topic : "MyTopic",
callback : function () {
}
} );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
} );
after( function () {
postal.utils.reset();
} );
it( "subscription should be of type SubscriptionDefinition", function () {
expect( subscription instanceof postal.SubscriptionDefinition ).to.be.ok();
} );
it( "should create an channel called MyChannel", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"] !== undefined ).to.be.ok();
} );
it( "should create a topic under MyChannel called MyTopic", function () {
expect( postal.configuration.bus.subscriptions["MyChannel"]["MyTopic"] !== undefined ).to.be.ok();
} );
it( "should have set subscription channel value", function () {
expect( sub.channel ).to.be( "MyChannel" );
} );
it( "should have set subscription topic value", function () {
expect( sub.topic ).to.be( "MyTopic" );
} );
it( "should have defaulted the subscription constraints array", function () {
expect( sub.constraints.length ).to.be( 0 );
} );
it( "should have defaulted the subscription context value", function () {
expect( sub.context ).to.be( null );
} );
} );
});
describe("publishing", function() {
describe( "When publishing a message", function () {
var msgReceivedCnt = 0,
msgData;
before( function () {
channel = postal.channel( "MyChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
msgReceivedCnt++;
msgData = data;
} );
channel.publish( "MyTopic", "Testing123" );
subscription.unsubscribe();
channel.publish( "MyTopic", "Testing123" );
} );
after( function () {
postal.utils.reset();
} );
it( "subscription callback should be invoked once", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
it( "subscription callback should receive published data", function () {
expect( msgData ).to.be( "Testing123" );
} );
} );
describe( "When publishing on a channel where no subscribers exist", function () {
it( "should return expected results for MyChannel/MyTopic", function () {
var env = postal.publish( {
channel : "NoOneIsUsingThisOne",
topic : "This.Is.A.Lonely.Topic",
data : "Y U NO SUBSCRIBE TO ME?"
} );
expect( !_.isEmpty( env ) ).to.be( true );
} );
} );
describe( "When using global publish api", function () {
var msgReceivedCnt = 0,
msgData;
before( function () {
channel = postal.channel( "MyGlobalChannel" );
subscription = channel.subscribe( "MyTopic", function ( data ) {
msgReceivedCnt++;
msgData = data;
} );
postal.publish( { channel : "MyGlobalChannel", topic : "MyTopic", data : "Testing123" } );
subscription.unsubscribe();
postal.publish( { channel : "MyGlobalChannel", topic : "MyTopic", data : "Testing123" } );
} );
after( function () {
postal.utils.reset();
msgReceivedCnt = 0;
} );
it( "channel should be of type ChannelDefinition", function () {
expect( channel instanceof postal.ChannelDefinition ).to.be.ok();
} );
it( "subscription callback should be invoked once", function () {
expect( msgReceivedCnt ).to.be( 1 );
} );
it( "subscription callback should receive published data", function () {
expect( msgData ).to.be( "Testing123" );
} );
} );
});
describe("unsubscribing", function() {
describe( "With a single subscription", function () {
var subExistsBefore = false,
subExistsAfter = true;
var systemSubscription = {};
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.*",
callback : function ( data, env ) {
if ( data.event &&
data.event === "subscription.removed" &&
data.channel === "MyChannel" &&
data.topic === "MyTopic" ) {
caughtUnsubscribeEvent = true;
}
}
} );
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () { });
subExistsBefore = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0] !== undefined;
subscription.unsubscribe();
subExistsAfter = postal.configuration.bus.subscriptions.MyChannel.MyTopic.length !== 0;
} );
after( function () {
systemSubscription.unsubscribe();
postal.utils.reset();
} );
it( "subscription should exist before unsubscribe", function () {
expect( subExistsBefore ).to.be.ok();
} );
it( "subscription should not exist after unsubscribe", function () {
expect( subExistsAfter ).to.not.be.ok();
} );
it( "should have captured unsubscription creation event", function () {
expect( caughtUnsubscribeEvent ).to.be.ok();
} );
it( "postal.getSubscribersFor('MyChannel', 'MyTopic') should not return any subscriptions", function () {
expect( postal.utils.getSubscribersFor("MyChannel", "MyTopic").length ).to.be(0);
} );
} );
describe( "With multiple subscribers on one channel", function () {
var subscription1, subscription2, results = [];
before( function () {
channel = postal.channel();
subscription1 = channel.subscribe( 'test',function () {
results.push( '1 received message' );
} ).once();
subscription2 = channel.subscribe( 'test', function () {
results.push( '2 received message' );
} );
channel.publish( 'test' );
channel.publish( 'test' );
} );
after( function () {
subscription2.unsubscribe();
postal.utils.reset();
} );
it( "should produce expected messages", function () {
expect( results.length ).to.be( 3 );
expect( results[0] ).to.be( "1 received message" );
expect( results[1] ).to.be( "2 received message" );
expect( results[2] ).to.be( "2 received message" );
} );
} );
describe( "With nested publishing", function () {
var subscription1, subscription2, sysub, results = [];
before( function () {
channel = postal.channel();
sysub = postal.subscribe( {
channel : postal.configuration.SYSTEM_CHANNEL,
topic : "subscription.removed",
callback : function ( d, e ) {
results.push( "unsubscribed" );
}
} );
subscription1 = channel.subscribe( 'nest.test',function () {
results.push( '1 received message' );
channel.publish( "nest.test2", "Hai" );
} ).once();
subscription2 = channel.subscribe( 'nest.test2', function () {
results.push( '2 received message' );
} );
channel.publish( 'nest.test' );
channel.publish( 'nest.test' );
} );
after( function () {
//subscription2.unsubscribe();
sysub.unsubscribe();
postal.utils.reset();
} );
it( "should produce expected messages", function () {
expect( results.length ).to.be( 3 );
expect( results[0] ).to.be( "1 received message" );
expect( results[1] ).to.be( "2 received message" );
expect( results[2] ).to.be( "unsubscribed" );
} );
} );
});
describe("wiretaps", function() {
describe( "When subscribing and unsubscribing a wire tap", function () {
var wireTapData,
wireTapEnvelope,
wiretap;
before( function () {
caughtUnsubscribeEvent = false;
wireTapData = [];
wireTapEnvelope = [];
wiretap = postal.addWireTap( function ( msg, envelope ) {
wireTapData.push( msg );
wireTapEnvelope.push( envelope );
} );
postal.publish( { topic : "Oh.Hai.There", data : "I'm in yer bus, tappin' yer subscriptionz..."} );
wiretap();
postal.publish( { topic : "Oh.Hai.There", data : "I'm in yer bus, tappin' yer subscriptionz..."} );
} );
after( function () {
postal.utils.reset();
} );
it( "wire tap should have been invoked only once", function () {
expect( wireTapData.length ).to.be( 1 );
expect( wireTapEnvelope.length ).to.be( 1 );
} );
it( "wireTap data should match expected results", function () {
expect( wireTapData[0] ).to.be( "I'm in yer bus, tappin' yer subscriptionz..." );
} );
it( "wireTap envelope should match expected results", function () {
expect( wireTapEnvelope[0].channel ).to.be( postal.configuration.DEFAULT_CHANNEL );
expect( wireTapEnvelope[0].topic ).to.be( "Oh.Hai.There" );
} );
} );
});
}());

View file

@ -0,0 +1,278 @@
/* global describe, postal, it, after, before, expect */
(function() {
var NO_OP = function () {};
var SubscriptionDefinition = postal.SubscriptionDefinition;
describe( "SubscriptionDefinition", function () {
describe( "When initializing SubscriptionDefinition", function () {
var sDef,
caughtSubscribeEvent,
systemSubscription;
before( function () {
systemSubscription = postal.subscribe( {
channel : "postal",
topic : "subscription.created",
callback : function ( data, envelope ) {
if ( data.event &&
data.event === "subscription.created" &&
data.channel === "SubDefTestChannel" &&
data.topic === "SubDefTestTopic" ) {
caughtSubscribeEvent = true;
}
}
} );
sDef = new SubscriptionDefinition( "SubDefTestChannel", "SubDefTestTopic", NO_OP );
} );
after( function () {
sDef.unsubscribe();
systemSubscription.unsubscribe();
caughtSubscribeEvent = false;
} );
it( "should set the channel to SubDefTestChannel", function () {
expect( sDef.channel ).to.be( "SubDefTestChannel" );
} );
it( "should set the topic to SubDefTestTopic", function () {
expect( sDef.topic ).to.be( "SubDefTestTopic" );
} );
it( "should set the callback", function () {
expect( sDef.callback ).to.be( NO_OP );
} );
it( "should default the constraints", function () {
expect( sDef.constraints.length ).to.be( 0 );
} );
it( "should default the context", function () {
expect( sDef.context ).to.be( null );
} );
it( "should fire the subscription.created message", function () {
expect( caughtSubscribeEvent ).to.be( true );
} );
} );
describe( "When setting distinctUntilChanged", function () {
var sDefa = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).distinctUntilChanged();
it( "Should add a DistinctPredicate constraint to the configuration constraints", function () {
expect( sDefa.constraints.length ).to.be( 1 );
} );
} );
describe( "When adding a constraint", function () {
var sDefb = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).withConstraint( function () {
} );
it( "Should add a constraint", function () {
expect( sDefb.constraints.length ).to.be( 1 );
} );
} );
describe( "When adding multiple constraints", function () {
var sDefc = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ).withConstraints( [function () {
}, function () {
}, function () {
}] );
it( "Should add a constraint", function () {
expect( sDefc.constraints.length ).to.be( 3 );
} );
} );
describe( "When setting the context", function () {
var obj = { name : "Rose" },
name,
sDefd = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP )
.withContext( obj )
.withConstraint( function ( d, e ) {
name = this.name;
return true;
} );
postal.publish( { channel : "TestChannel", topic : "TestTopic", data : "Oh, hai"} )
it( "Should set context", function () {
expect( sDefd.context ).to.be( obj );
} );
it( "Should apply context to predicate/constraint", function () {
expect( name ).to.be( "Rose" );
} );
} );
describe( "When calling subscribe to set the callback", function () {
var sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", NO_OP ),
fn = function () {
};
sDefe.subscribe( fn );
it( "Should set the callback", function () {
expect( sDefe.callback ).to.be( fn );
} );
} );
describe( "When deferring the callback", function () {
var results = [], sDefe;
it( "Should defer the callback", function ( done ) {
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
results.push( data );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
expect( env.topic ).to.be( "TestTopic" );
done();
} ).defer();
sDefe.callback( "second", { topic : "TestTopic" } );
results.push( "first" );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).defer();
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
it( "Should keep the context intact when modified later", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "TestChannel", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).defer().withContext(context);
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
describe( "When throttling the callback", function () {
var results = [],
sDefe = new SubscriptionDefinition( "ThrottleTest", "TestTopic", function ( data ) {
results.push( data );
} ).withThrottle( 500 );
var timeout1, timeout2;
it( "should have only invoked throttled callback twice", function ( done ) {
sDefe.callback( 1 ); // starts the two second clock on debounce
timeout1 = setTimeout( function () {
sDefe.callback( 800 );
}, 900 ); // should invoke callback
for ( var i = 0; i < 20; i++ ) {
(function ( x ) {
sDefe.callback( x );
})( i );
}
timeout2 = setTimeout( function () {
expect( results[0] ).to.be( 1 );
expect( results[1] ).to.be( 800 );
expect( results.length ).to.be( 2 );
done();
}, 1500 );
} );
it( "Should keep the context intact", function( done ) {
var context = {
key : 'abcd'
};
sDefe = new SubscriptionDefinition( "ThrottleTest", "TestTopic", function( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext( context ).withThrottle( 500 );
sDefe.callback.call( sDefe.context, 1 );
} );
} );
describe( "When delaying the callback", function () {
var results = [], sDefe;
it( "Should delay the callback", function ( done ) {
sDefe = new SubscriptionDefinition( "DelayTest", "TestTopic", function ( data, env ) {
results.push( data );
expect( results[0] ).to.be( "first" );
expect( results[1] ).to.be( "second" );
expect( env.topic ).to.be( "TestTopic" );
done();
} ).withDelay( 200 );
sDefe.callback( "second", { topic : "TestTopic" } );
results.push( "first" );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 1234
};
sDefe = new SubscriptionDefinition( "DelayTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).withDelay( 200 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
describe( "When debouncing the callback", function () {
var results = [],
sDefe = new SubscriptionDefinition( "DebounceTest", "TestTopic", function ( data ) {
results.push( data );
} ).withDebounce( 800 );
it( "should have only invoked debounced callback once", function ( done ) {
sDefe.callback( 1 ); // starts the two second clock on debounce
setTimeout( function () {
sDefe.callback( 2 );
}, 20 ); // should not invoke callback
setTimeout( function () {
sDefe.callback( 3 );
}, 80 ); // should not invoke callback
setTimeout( function () {
sDefe.callback( 6 );
}, 800 ); // should invoke callback
setTimeout( function () {
expect( results[0] ).to.be( 6 );
expect( results.length ).to.be( 1 );
done();
}, 2300 );
} );
it( "Should keep the context intact", function ( done ) {
var context = {
key : 5678
};
var timeout;
sDefe = new SubscriptionDefinition( "DebounceTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
clearTimeout(timeout);
done();
} ).withContext(context).withDebounce( 100 );
sDefe.callback.call( sDefe.context, 1 );
timeout = setTimeout( function () {
sDefe.callback.call( sDefe.context, 2 );
}, 200 ); // should invoke callback
});
} );
describe( "When self disposing", function () {
var context = {
key : 'abcd'
};
it( "Should be inactive", function () {
var sDefe = new SubscriptionDefinition( "DisposeTest", "TestTopic", function ( data, env ) {
} ).withContext(context).disposeAfter( 1 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
expect( sDefe.inactive ).to.be( true );
} );
it( "Should keep the context intact", function ( done ) {
var sDefe = new SubscriptionDefinition( "DisposeTest", "TestTopic", function ( data, env ) {
expect( this ).to.be( context );
done();
} ).withContext(context).disposeAfter( 200 );
sDefe.callback.call( sDefe.context, "stuff", { topic : "TestTopic" } );
} );
} );
} );
}());

63
spec/utils.spec.js Normal file
View file

@ -0,0 +1,63 @@
/* global describe, postal, it, after, before, expect */
(function() {
var NO_OP = function() {};
var subscription;
var sub;
describe("postal.utils", function() {
describe( "When calling postal.utils.getSubscribersFor", function () {
var subs = [], i;
before( function () {
i = 10;
var ch1 = postal.channel( "MyChannel" ),
ch2 = postal.channel( "MyChannel2" );
while ( i ) {
subs.push( ch1.subscribe( "MyTopic", NO_OP) );
subs.push( ch2.subscribe( "MyTopic2", NO_OP) );
i--;
}
} );
after( function () {
subs = [];
postal.utils.reset();
} );
it( "should return expected results for MyChannel/MyTopic", function () {
var results = postal.utils.getSubscribersFor( { channel : "MyChannel", topic : "MyTopic" } );
expect( results.length ).to.be( 10 );
} );
it( "should return expected results for MyChannel2/MyTopic2", function () {
var results = postal.utils.getSubscribersFor( { channel : "MyChannel2", topic : "MyTopic2" } );
expect( results.length ).to.be( 10 );
} );
} );
describe( "When calling postal.utils.reset", function () {
var resolver;
before( function () {
postal.utils.reset();
subscription = postal.channel( "MyChannel" ).subscribe( "MyTopic", function () {});
postal.channel( "MyChannel" ).publish( "MyTopic", "Oh Hai!" );
sub = postal.configuration.bus.subscriptions.MyChannel.MyTopic[0];
resolver = postal.configuration.resolver.cache.MyTopic;
postal.utils.reset();
} );
after( function () {
} );
it( "should have created a subscription definition", function () {
expect( sub.channel ).to.be( "MyChannel" );
expect( sub.topic ).to.be( "MyTopic" );
expect( sub.constraints.length ).to.be( 0 );
expect( sub.context ).to.be( null );
} );
it( "should have created a resolver cache entry", function () {
expect( _.isEmpty( resolver ) ).to.not.be.ok()
expect( resolver.MyTopic ).to.be.ok();
} );
it( "subscriptions cache should now be empty", function () {
expect( _.isEmpty( postal.configuration.bus.subscriptions ) ).to.be.ok();
} );
it( "resolver cache should now be empty", function () {
expect( _.isEmpty( postal.configuration.resolver.cache ) ).to.be.ok();
} );
} );
});
}());