From 9e65e6bf470cf65bd589c1162df985f9f488c476 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Wed, 15 Feb 2017 16:43:29 +0100 Subject: [PATCH 1/3] Tests for mounting class and factory components --- api/tests/test-mount.js | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/api/tests/test-mount.js b/api/tests/test-mount.js index 210a0627..038f470a 100644 --- a/api/tests/test-mount.js +++ b/api/tests/test-mount.js @@ -32,7 +32,17 @@ o.spec("mount", function() { o(threw).equals(true) }) - o("renders into `root`", function() { + o("throws on invalid component", function() { + var threw = false + try { + mount(root, {}) + } catch (e) { + threw = true + } + o(threw).equals(true) + }) + + o("renders into `root` (POJO component)", function() { mount(root, { view : function() { return m("div") @@ -42,6 +52,26 @@ o.spec("mount", function() { o(root.firstChild.nodeName).equals("DIV") }) + o("renders into `root` (class component)", function() { + function Cmp(){} + Cmp.prototype.view = function(){return m("div")} + mount(root, Cmp) + + o(root.firstChild.nodeName).equals("DIV") + }) + + o("renders into `root` factory (factory component)", function() { + mount(root, function(){ + return { + view : function() { + return m("div") + } + } + }) + + o(root.firstChild.nodeName).equals("DIV") + }) + o("mounting null unmounts", function() { mount(root, { view : function() { From f4fb5ac4be524de3fcbe416a672cea136a5461e9 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Wed, 15 Feb 2017 21:48:28 +0100 Subject: [PATCH 2/3] Tests for routes that resolve to class and factory components --- api/tests/test-router.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/api/tests/test-router.js b/api/tests/test-router.js index 9b8705da..c624789b 100644 --- a/api/tests/test-router.js +++ b/api/tests/test-router.js @@ -51,7 +51,7 @@ o.spec("route", function() { o(root.firstChild.nodeName).equals("DIV") }) - o("routed mount points can redraw synchronously (#1275)", function() { + o("routed mount points can redraw synchronously (POJO component)", function() { var view = o.spy() $window.location.href = prefix + "/" @@ -65,6 +65,39 @@ o.spec("route", function() { }) + o("routed mount points can redraw synchronously (constructible component)", function() { + var view = o.spy() + + var Cmp = function(){} + Cmp.prototype.view = view + + $window.location.href = prefix + "/" + route(root, "/", {"/":Cmp}) + + o(view.callCount).equals(1) + + redrawService.redraw() + + o(view.callCount).equals(2) + + }) + + o("routed mount points can redraw synchronously (factory component)", function() { + var view = o.spy() + + function Cmp() {return {view: view}} + + $window.location.href = prefix + "/" + route(root, "/", {"/":Cmp}) + + o(view.callCount).equals(1) + + redrawService.redraw() + + o(view.callCount).equals(2) + + }) + o("default route doesn't break back button", function(done) { $window.location.href = "http://old.com" $window.location.href = "http://new.com" From f273b012c948c4efe9fead69fbfa7850b5af82c2 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Wed, 15 Feb 2017 21:58:10 +0100 Subject: [PATCH 3/3] =?UTF-8?q?Add=20support=20for=20(m.mount=20+=20m.rout?= =?UTF-8?q?e)=20=C3=97=20(factory=20+=20constructible)=20components?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/mount.js | 2 +- api/router.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/api/mount.js b/api/mount.js index 0cebfcf6..2178505a 100644 --- a/api/mount.js +++ b/api/mount.js @@ -10,7 +10,7 @@ module.exports = function(redrawService) { return } - if (component.view == null) throw new Error("m.mount(element, component) expects a component, not a vnode") + if (component.view == null && typeof component !== "function") throw new Error("m.mount(element, component) expects a component, not a vnode") var run = function() { redrawService.render(root, Vnode(component)) diff --git a/api/router.js b/api/router.js index b4690966..c182008f 100644 --- a/api/router.js +++ b/api/router.js @@ -21,11 +21,12 @@ module.exports = function($window, redrawService) { routeService.defineRoutes(routes, function(payload, params, path) { var update = lastUpdate = function(routeResolver, comp) { if (update !== lastUpdate) return - component = comp != null && typeof comp.view === "function" ? comp : "div", attrs = params, currentPath = path, lastUpdate = null + component = comp != null && (typeof comp.view === "function" || typeof comp === "function")? comp : "div" + attrs = params, currentPath = path, lastUpdate = null render = (routeResolver.render || identity).bind(routeResolver) run() } - if (payload.view) update({}, payload) + if (payload.view || typeof payload === "function") update({}, payload) else { if (payload.onmatch) { Promise.resolve(payload.onmatch(params, path)).then(function(resolved) {