From 7de012433936ce5a29db5cad4bbb81491eea554b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre-Yves=20G=C3=A9rardy?= Date: Tue, 13 Jun 2017 16:29:02 +0200 Subject: [PATCH] Tests for m.redraw.sync() --- api/tests/test-mount.js | 32 ++++++++++++++++++++++++++- api/tests/test-redraw.js | 48 ++++++++++++++++++++++++++++++++++++++++ tests/test-api.js | 8 +++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/api/tests/test-mount.js b/api/tests/test-mount.js index e115711b..80ade907 100644 --- a/api/tests/test-mount.js +++ b/api/tests/test-mount.js @@ -52,7 +52,7 @@ o.spec("mount", function() { o(threw).equals(true) }) - o("renders into `root`", function() { + o("renders into `root` synchronoulsy", function() { mount(root, createComponent({ view : function() { return m("div") @@ -74,6 +74,36 @@ o.spec("mount", function() { o(root.childNodes.length).equals(0) }) + o("Mounting a second root doesn't cause the first one to redraw", function() { + var view = o.spy(function() { + return m("div") + }) + + render(root, [ + m("#child0"), + m("#child1") + ]) + + mount(root.childNodes[0], createComponent({ + view : view + })) + + o(root.firstChild.nodeName).equals("DIV") + o(view.callCount).equals(1) + + mount(root.childNodes[1], createComponent({ + view : function() { + return m("div") + } + })) + + o(view.callCount).equals(1) + + throttleMock.fire() + + o(view.callCount).equals(1) + }) + o("redraws on events", function() { var onupdate = o.spy() var oninit = o.spy() diff --git a/api/tests/test-redraw.js b/api/tests/test-redraw.js index 80a768dc..68b1a911 100644 --- a/api/tests/test-redraw.js +++ b/api/tests/test-redraw.js @@ -106,6 +106,25 @@ o.spec("redrawService", function() { }, 20) }) + o("should stop running after unsubscribe, even if it occurs after redraw is requested", function(done) { + var spy = o.spy(function() { + throw new Error("This shouldn't have been called") + }) + + redrawService.subscribe(root, spy) + + redrawService.redraw() + + redrawService.unsubscribe(root, spy) + + o(spy.callCount).equals(0) + setTimeout(function() { + o(spy.callCount).equals(0) + + done() + }, 20) + }) + o("does nothing on invalid unsubscribe", function(done) { var spy = o.spy() @@ -120,4 +139,33 @@ o.spec("redrawService", function() { done() }, 20) }) + + o("redraw.sync() redraws all roots synchronously", function() { + var el1 = $document.createElement("div") + var el2 = $document.createElement("div") + var el3 = $document.createElement("div") + var spy1 = o.spy() + var spy2 = o.spy() + var spy3 = o.spy() + + redrawService.subscribe(el1, spy1) + redrawService.subscribe(el2, spy2) + redrawService.subscribe(el3, spy3) + + o(spy1.callCount).equals(0) + o(spy2.callCount).equals(0) + o(spy3.callCount).equals(0) + + redrawService.redraw.sync() + + o(spy1.callCount).equals(1) + o(spy2.callCount).equals(1) + o(spy3.callCount).equals(1) + + redrawService.redraw.sync() + + o(spy1.callCount).equals(2) + o(spy2.callCount).equals(2) + o(spy3.callCount).equals(2) + }) }) diff --git a/tests/test-api.js b/tests/test-api.js index d0c2cf81..49938d82 100644 --- a/tests/test-api.js +++ b/tests/test-api.js @@ -173,6 +173,14 @@ o.spec("api", function() { done() }, FRAME_BUDGET) }) + o("sync", function() { + var root = window.document.createElement("div") + var view = o.spy() + m.mount(root, createComponent({view: view})) + o(view.callCount).equals(1) + m.redraw.sync() + o(view.callCount).equals(2) + }) }) }) })