From 5149d77acaa6eca91d703a85572c8dfd92a8a4d3 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 9 Jan 2015 04:57:55 +0100 Subject: [PATCH] Add tests that fail due to router processing stale transitions --- modules/__tests__/Router-test.js | 114 +++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/modules/__tests__/Router-test.js b/modules/__tests__/Router-test.js index 604e288a48..0c41e6f6c7 100644 --- a/modules/__tests__/Router-test.js +++ b/modules/__tests__/Router-test.js @@ -186,6 +186,66 @@ describe('Router', function () { }); }); }); + + it('stops waiting on router.transitionTo after another asynchronous transition ended ', function (done) { + var LongAsync = React.createClass({ + statics: { + delay: Async.delay * 2, + + willTransitionTo: function (transition, params, query, callback) { + setTimeout(callback, this.delay); + } + }, + + render: function () { + return
Async2
; + } + }); + + TestLocation.history = [ '/foo' ]; + var routes = [ + , + , + , + + ]; + + var div = document.createElement('div'); + var steps = []; + var router = Router.create({ + routes: routes, + location: TestLocation + }); + + steps.push(function () { + router.transitionTo('/async1'); + setTimeout(function () { + router.transitionTo('/async2'); + expect(div.innerHTML).toMatch(/Foo/); + setTimeout(function () { + expect(div.innerHTML).toMatch(/Foo/); + router.transitionTo('/bar'); + expect(div.innerHTML).toMatch(/Bar/); + }, Async.delay); + }, Async.delay / 2); + }); + + steps.push(function () { + setTimeout(function () { + expect(div.innerHTML).toMatch(/Bar/); + done(); + }, Async.delay); + }); + + steps.push(function () { + }); + + router.run(function (Handler, state) { + React.render(, div, function () { + steps.shift()(); + }); + }); + }); }); describe('transition.redirect', function () { @@ -521,6 +581,60 @@ describe('Router', function () { }); }); }); + + it('ignores aborting asynchronously in willTransitionTo when aborted before router.transitionTo', function (done) { + var AbortAsync2 = React.createClass({ + statics: { + willTransitionTo: function (transition, params, query, callback) { + transition.abort(); + setTimeout(callback, Async.delay); + } + }, + + render: function () { + return
Abort
; + } + }); + + TestLocation.history = [ '/foo' ]; + var routes = [ + , + , + + ]; + + var div = document.createElement('div'); + var steps = []; + var router = Router.create({ + routes: routes, + location: TestLocation + }); + + steps.push(function () { + router.transitionTo('/abort'); + expect(div.innerHTML).toMatch(/Foo/); + + router.transitionTo('/bar'); + expect(div.innerHTML).toMatch(/Bar/); + }); + + steps.push(function () { + setTimeout(function () { + expect(div.innerHTML).toMatch(/Bar/); + expect(TestLocation.history).toEqual(['/foo', '/bar']); + done(); + }, Async.delay + 10); + }); + + steps.push(function () { + }); + + router.run(function (Handler, state) { + React.render(, div, function () { + steps.shift()(); + }); + }); + }); }); });