From 749ff74bea33f2fc25bbb3704fdd3364ec1917b1 Mon Sep 17 00:00:00 2001 From: Pat Cavit Date: Thu, 19 May 2016 11:35:45 -0700 Subject: [PATCH] Test both async types when using router --- api/tests/test-router.js | 327 ++++++++++++++++++++------------------- 1 file changed, 167 insertions(+), 160 deletions(-) diff --git a/api/tests/test-router.js b/api/tests/test-router.js index 5c17ac73..ce8c558d 100644 --- a/api/tests/test-router.js +++ b/api/tests/test-router.js @@ -12,170 +12,177 @@ o.spec("m.route", function() { var $window, root, router [ - "#", - "?", - "#!", - "?!", - "" - ].forEach(function(prefix) { - var spec = prefix ? "prefix " + prefix : "pushstate"; - - o.spec(spec, function() { - o.beforeEach(function() { - var dom = domMock() - var location = pushStateMock() + "setTimeout", + "requestAnimationFrame" + ].forEach(function(timing) { + o.spec(timing, function() { + [ + "#", + "?", + "#!", + "?!", + "" + ].forEach(function(prefix) { + var spec = prefix ? "prefix " + prefix : "pushstate"; - // Generate a DOM + Location mock - Object.keys(location).forEach(function(key) { - dom[key] = location[key] - }) - - $window = dom - async.setTimeout($window) - root = $window.document.body - }) - - o("is a function", function() { - o(typeof createRouter).equals("function") - }) - - o("returns a function after invocation", function() { - o(typeof createRouter($window)).equals("function") - }) - - o("updates passed in redraw object", function() { - var redraw = {} - var router = createRouter($window, redraw) - - router.prefix(prefix) - - router(root, "/", { - "/" : { - view: function() { - return m("div") - } - } - }) - - o(typeof redraw.run).equals("function") - }) - - o("renders into `root`", function() { - var router = createRouter($window, {}) - - router.prefix(prefix) - - router(root, "/", { - "/" : { - view: function() { - return m("div") - } - } - }) - - o(root.firstChild.nodeName).equals("DIV") - }) - - o("redraws on redraw.run()", function(done) { - var onupdate = o.spy() - var oninit = o.spy() - var redraw = {} - var router = createRouter($window, redraw) - - router.prefix(prefix) - - router(root, "/", { - "/" : { - view: function() { - return m("div", { - oninit: oninit, - onupdate: onupdate - }) - } - } - }) - - o(oninit.callCount).equals(1) - - redraw.run() - - // Wrapped to give time for the rate-limited redraw to fire - setTimeout(function() { - o(onupdate.callCount).equals(1) + o.spec(spec, function() { + o.beforeEach(function() { + var dom = domMock() + var location = pushStateMock() + + // Generate a DOM + Location mock + Object.keys(location).forEach(function(key) { + dom[key] = location[key] + }) + + $window = dom + async[timing]($window) + root = $window.document.body + }) - done() - }, 20) - }) - - o("redraws on events", function(done, timeout) { - var onupdate = o.spy() - var oninit = o.spy() - var onclick = o.spy() - var router = createRouter($window, {}) - var e = $window.document.createEvent("MouseEvents") - - e.initEvent("click", true, true) - - router.prefix(prefix) - - router(root, "/", { - "/" : { - view: function() { - return m("div", { - oninit: oninit, - onupdate: onupdate, - onclick: onclick, - }) - } - } - }) - - root.firstChild.dispatchEvent(e) - - o(oninit.callCount).equals(1) - - o(onclick.callCount).equals(1) - o(onclick.this).equals(root.firstChild) - o(onclick.args[0].type).equals("click") - o(onclick.args[0].target).equals(root.firstChild) - - // Wrapped to give time for the rate-limited redraw to fire - setTimeout(function() { - o(onupdate.callCount).equals(1) + o("is a function", function() { + o(typeof createRouter).equals("function") + }) - done() - }, 20) - }) - - o("changes location on route.link", function() { - var router = createRouter($window, {}) - var e = $window.document.createEvent("MouseEvents") - - e.initEvent("click", true, true) - - router.prefix(prefix) - - router(root, "/", { - "/" : { - view: function() { - return m("a", { - href: "/test", - oncreate: router.link - }) - } - }, - "/test" : { - view : function() { - return m("div") - } - } + o("returns a function after invocation", function() { + o(typeof createRouter($window)).equals("function") + }) + + o("updates passed in redraw object", function() { + var redraw = {} + var router = createRouter($window, redraw) + + router.prefix(prefix) + + router(root, "/", { + "/" : { + view: function() { + return m("div") + } + } + }) + + o(typeof redraw.run).equals("function") + }) + + o("renders into `root`", function() { + var router = createRouter($window, {}) + + router.prefix(prefix) + + router(root, "/", { + "/" : { + view: function() { + return m("div") + } + } + }) + + o(root.firstChild.nodeName).equals("DIV") + }) + + o("redraws on redraw.run()", function(done) { + var onupdate = o.spy() + var oninit = o.spy() + var redraw = {} + var router = createRouter($window, redraw) + + router.prefix(prefix) + + router(root, "/", { + "/" : { + view: function() { + return m("div", { + oninit: oninit, + onupdate: onupdate + }) + } + } + }) + + o(oninit.callCount).equals(1) + + redraw.run() + + // Wrapped to give time for the rate-limited redraw to fire + setTimeout(function() { + o(onupdate.callCount).equals(1) + + done() + }, 20) + }) + + o("redraws on events", function(done, timeout) { + var onupdate = o.spy() + var oninit = o.spy() + var onclick = o.spy() + var router = createRouter($window, {}) + var e = $window.document.createEvent("MouseEvents") + + e.initEvent("click", true, true) + + router.prefix(prefix) + + router(root, "/", { + "/" : { + view: function() { + return m("div", { + oninit: oninit, + onupdate: onupdate, + onclick: onclick, + }) + } + } + }) + + root.firstChild.dispatchEvent(e) + + o(oninit.callCount).equals(1) + + o(onclick.callCount).equals(1) + o(onclick.this).equals(root.firstChild) + o(onclick.args[0].type).equals("click") + o(onclick.args[0].target).equals(root.firstChild) + + // Wrapped to give time for the rate-limited redraw to fire + setTimeout(function() { + o(onupdate.callCount).equals(1) + + done() + }, 20) + }) + + o("changes location on route.link", function() { + var router = createRouter($window, {}) + var e = $window.document.createEvent("MouseEvents") + + e.initEvent("click", true, true) + + router.prefix(prefix) + + router(root, "/", { + "/" : { + view: function() { + return m("a", { + href: "/test", + oncreate: router.link + }) + } + }, + "/test" : { + view : function() { + return m("div") + } + } + }) + + o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/" : "")) + + root.firstChild.dispatchEvent(e) + + o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/test" : "test")) + }) }) - - o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/" : "")) - - root.firstChild.dispatchEvent(e) - - o($window.location.href).equals("http://localhost/" + (prefix ? prefix + "/test" : "test")) }) }) })