Skip to content

Commit

Permalink
One test also for the situation when handler is executed in init()
Browse files Browse the repository at this point in the history
  • Loading branch information
Vesa Poikajärvi committed May 11, 2012
1 parent 8ac12b1 commit 1c8e595
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 55 deletions.
23 changes: 14 additions & 9 deletions test/browser/helpers/api.js
Expand Up @@ -2,12 +2,17 @@ module("Director.js", {
setup: function() {
window.location.hash = "";
shared = {};
shared.fired = [];
// Init needed keys earlier because of in HTML5 mode the route handler
// is executed upon Router.init() and due to that setting shared.fired
// in the param test of createTest is too late
if (HTML5TEST) {
shared.fired = [];
shared.fired_count = 0;
}
},
teardown: function() {
window.location.hash = "";
shared = {};
shared.fired = [];
}
});

Expand All @@ -19,15 +24,15 @@ function createTest(name, config, use, test) {
use = undefined;
}

var innerTimeout = 0;
if (HTML5TEST) {
if (use === undefined) {
use = {};
}
use.html5history = true;
innerTimeout = 500; // Because of the use of setTimeout when defining onpopstate
if (HTML5TEST && use === undefined) {
use = {};
use.html5history = true;
use.run_handler_in_init = false;
}

// Because of the use of setTimeout when defining onpopstate
var innerTimeout = HTML5TEST === true ? 500 : 0;

asyncTest(name, function() {
setTimeout(function() {
var router = new Router(config),
Expand Down
1 change: 1 addition & 0 deletions test/browser/html5-routes-harness.html
Expand Up @@ -6,6 +6,7 @@
<link rel="stylesheet" type="text/css" href="/files/helpers/qunit.css" media="all">
</head>
<body>
<p>Note: in order to execute HTML5 mode test this file needs to be served with provided nodejs backend. Start the server from <span style="font-family: 'Courier New', monotype;">director/test/browser/backend</span> and go to <span style="font-family: Courier, monotype;">http://localhost:8080/</span> to launch the tests.</p>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
Expand Down
65 changes: 19 additions & 46 deletions test/browser/html5-routes-test.js
Expand Up @@ -8,7 +8,6 @@ createTest('Nested route with the many children as a tokens, callbacks should yi
}
}
}, function() {
shared.fired = [];
this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['/a/b/c', 'b', 'c']);
this.finish();
Expand All @@ -24,7 +23,6 @@ createTest('Nested route with the first child as a token, callback should yield
}
}
}, function() {
shared.fired = [];
this.navigate('/foo/a', function() {
this.navigate('/foo/b/c', function() {
deepEqual(shared.fired, ['/foo/a', 'a']);
Expand All @@ -42,7 +40,6 @@ createTest('Nested route with the first child as a regexp, callback should yield
}
}
}, function() {
shared.fired = [];
this.navigate('/foo/a', function() {
this.navigate('/foo/b/c', function() {
deepEqual(shared.fired, ['/foo/a', 'a']);
Expand All @@ -60,15 +57,12 @@ createTest('Nested route with the several regular expressions, callback should y
}
}
}, function() {
shared.fired = [];
this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['b', 'c']);
this.finish();
});
});



createTest('Single nested route with on member containing function value', {
'/a': {
'/b': {
Expand All @@ -78,7 +72,6 @@ createTest('Single nested route with on member containing function value', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a/b', function() {
deepEqual(shared.fired, ['/a/b']);
this.finish();
Expand All @@ -92,7 +85,6 @@ createTest('Single non-nested route with on member containing function value', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a/b', function() {
deepEqual(shared.fired, ['/a/b']);
this.finish();
Expand All @@ -107,7 +99,6 @@ createTest('Single nested route with on member containing array of function valu
}
}
}, function() {
shared.fired = [];
this.navigate('/a/b', function() {
deepEqual(shared.fired, ['/a/b', '/a/b']);
this.finish();
Expand All @@ -118,16 +109,15 @@ createTest('method should only fire once on the route.', {
'/a': {
'/b': {
once: function() {
shared.fired++;
shared.fired_count++;
}
}
}
}, function() {
shared.fired = 0;
this.navigate('/a/b', function() {
this.navigate('/a/b', function() {
this.navigate('/a/b', function() {
deepEqual(shared.fired, 1);
deepEqual(shared.fired_count, 1);
this.finish();
});
});
Expand All @@ -144,7 +134,6 @@ createTest('method should only fire once on the route, multiple nesting.', {
once: function() { shared.fired++; }
}
}, function() {
shared.fired = 0;
this.navigate('/a', function() {
this.navigate('/b', function() {
this.navigate('/a', function() {
Expand All @@ -165,7 +154,6 @@ createTest('overlapping routes with tokens.', {
shared.fired.push(location.pathname);
}
}, function() {
shared.fired = [];
this.navigate('/a/b/c', function() {

this.navigate('/a/b/c/d', function() {
Expand Down Expand Up @@ -196,8 +184,6 @@ createTest('Nested routes with no recursion', {
}
}
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['c']);
this.finish();
Expand All @@ -223,8 +209,6 @@ createTest('Nested routes with backward recursion', {
}, {
recurse: 'backward'
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['c', 'b', 'a']);
this.finish();
Expand All @@ -251,8 +235,6 @@ createTest('Breaking out of nested routes with backward recursion', {
}, {
recurse: 'backward'
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['c', 'b']);
this.finish();
Expand All @@ -278,8 +260,6 @@ createTest('Nested routes with forward recursion', {
}, {
recurse: 'forward'
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['a', 'b', 'c']);
this.finish();
Expand Down Expand Up @@ -308,8 +288,6 @@ createTest('Nested routes with forward recursion, single route with an after eve
}, {
recurse: 'forward'
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
this.navigate('/a/b', function() {
deepEqual(shared.fired, ['a', 'b', 'c', 'c-after', 'a', 'b']);
Expand Down Expand Up @@ -338,8 +316,6 @@ createTest('Breaking out of nested routes with forward recursion', {
}, {
recurse: 'forward'
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
deepEqual(shared.fired, ['a', 'b']);
this.finish();
Expand Down Expand Up @@ -379,8 +355,6 @@ createTest('All global event should fire after every route', {
shared.fired.push('b');
}
}, function() {
shared.fired = [];

this.navigate('/a', function() {
this.navigate('/b/c', function() {
this.navigate('/d/e', function() {
Expand Down Expand Up @@ -408,8 +382,6 @@ createTest('Not found.', {
shared.fired.push('notfound');
}
}, function() {
shared.fired = [];

this.navigate('/c', function() {
this.navigate('/d', function() {
deepEqual(shared.fired, ['notfound', 'notfound']);
Expand All @@ -434,8 +406,6 @@ createTest('On all.', {
shared.fired.push('c');
}
}, function() {
shared.fired = [];

this.navigate('/a', function() {
this.navigate('/b', function() {
deepEqual(shared.fired, ['a', 'c', 'b', 'c']);
Expand All @@ -461,8 +431,6 @@ createTest('After all.', {
shared.fired.push('c');
}
}, function() {
shared.fired = [];

this.navigate('/a', function() {
this.navigate('/b', function() {
deepEqual(shared.fired, ['a', 'c', 'b']);
Expand Down Expand Up @@ -492,8 +460,6 @@ createTest('resource object.', {
}
}
}, function() {
shared.fired = [];

this.navigate('/a/b/c', function() {
this.navigate('/d', function() {
deepEqual(shared.fired, ['f1-c', 'f1-undefined', 'f2']);
Expand All @@ -509,7 +475,6 @@ createTest('argument matching should be case agnostic', {
}
}
}, function() {
shared.fired = [];
this.navigate('/fooBar/tesTing', function() {
deepEqual(shared.fired, ['fooBar-tesTing']);
this.finish();
Expand All @@ -528,7 +493,6 @@ createTest('sanity test', {
}
}
}, function() {
shared.fired = [];
this.navigate('/is/there/sanity', function() {
deepEqual(shared.fired, ['yes there is sanity']);
this.finish();
Expand All @@ -547,7 +511,6 @@ createTest('`/` route should be navigable from the routing table', {
}
}
}, function() {
shared.fired = [];
this.navigate('/', function root() {
deepEqual(shared.fired, ['/']);
this.finish();
Expand All @@ -566,7 +529,6 @@ createTest('`/` route should not override a `/:token` route', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a', function afunc() {
deepEqual(shared.fired, ['/a']);
this.finish();
Expand All @@ -580,7 +542,6 @@ createTest('should accept the root as a token.', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a', function root() {
deepEqual(shared.fired, ['/a']);
this.finish();
Expand All @@ -594,7 +555,6 @@ createTest('routes should allow wildcards.', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a/bcd', function root() {
deepEqual(shared.fired, ['/a/bcd']);
this.finish();
Expand All @@ -608,7 +568,6 @@ createTest('functions should have |this| context of the router instance.', {
}
}
}, function() {
shared.fired = [];
this.navigate('/', function root() {
deepEqual(shared.fired, [true]);
this.finish();
Expand All @@ -623,7 +582,6 @@ createTest('setRoute with a single parameter should change location correctly',
}
}, function() {
var self = this;
shared.fired = [];
this.router.setRoute('/bonk');
setTimeout(function() {
deepEqual(shared.fired, ['/bonk']);
Expand All @@ -638,10 +596,25 @@ createTest('route should accept _ and . within parameters', {
}
}
}, function() {
shared.fired = [];
this.navigate('/a_complex_route.co.uk', function root() {
this.navigate('/a_complex_route.co.uk', function root() {
deepEqual(shared.fired, ['/a_complex_route.co.uk']);
this.finish();
});
});

createTest('Route handler should be executed in init() unless disabled', {
'/a': {
on: function() {
shared.fired.push(location.pathname);
}
}
},
{
html5history: true
}, function() {
this.navigate('/a', function() {
deepEqual(shared.fired, ['/a', '/a']);
this.finish();
});
}
);

0 comments on commit 1c8e595

Please sign in to comment.