From 0a767722834761801c07773cf1679f1e8d77dd1b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Gerardy Date: Mon, 27 Mar 2017 18:27:21 +0200 Subject: [PATCH] Add tests for components that overwrite their state in `oninit` --- render/tests/test-component.js | 91 ++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/render/tests/test-component.js b/render/tests/test-component.js index 2f59feef..94863236 100644 --- a/render/tests/test-component.js +++ b/render/tests/test-component.js @@ -764,6 +764,97 @@ o.spec("component", function() { o(attrs[hook].callCount).equals(methods[hook].callCount)(hook) }) }) + o("lifecycle timing megatest (for a single component with the state overwritten)", function() { + var methods = { + view: o.spy(function(vnode) { + o(vnode.state).equals(1) + return "" + }) + } + var attrs = {} + var hooks = [ + "oninit", "oncreate", "onbeforeupdate", + "onupdate", "onbeforeremove", "onremove" + ] + hooks.forEach(function(hook) { + // the `attrs` hooks are called before the component ones + attrs[hook] = o.spy(function(vnode) { + o(vnode.state).equals(1) + o(attrs[hook].callCount).equals(methods[hook].callCount + 1) + }) + methods[hook] = o.spy(function(vnode) { + o(vnode.state).equals(1) + o(attrs[hook].callCount).equals(methods[hook].callCount) + }) + }) + + var attrsOninit = attrs.oninit + var methodsOninit = methods.oninit + attrs.oninit = o.spy(function(vnode){ + vnode.state = 1 + return attrsOninit.call(this, vnode) + }) + methods.oninit = o.spy(function(vnode){ + vnode.state = 1 + return methodsOninit.call(this, vnode) + }) + + var component = createComponent(methods) + + o(methods.view.callCount).equals(0) + o(methods.oninit.callCount).equals(0) + o(methods.oncreate.callCount).equals(0) + o(methods.onbeforeupdate.callCount).equals(0) + o(methods.onupdate.callCount).equals(0) + o(methods.onbeforeremove.callCount).equals(0) + o(methods.onremove.callCount).equals(0) + + hooks.forEach(function(hook) { + o(attrs[hook].callCount).equals(methods[hook].callCount)(hook) + }) + + render(root, [{tag: component, attrs: attrs}]) + + o(methods.view.callCount).equals(1) + o(methods.oninit.callCount).equals(1) + o(methods.oncreate.callCount).equals(1) + o(methods.onbeforeupdate.callCount).equals(0) + o(methods.onupdate.callCount).equals(0) + o(methods.onbeforeremove.callCount).equals(0) + o(methods.onremove.callCount).equals(0) + + hooks.forEach(function(hook) { + o(attrs[hook].callCount).equals(methods[hook].callCount)(hook) + }) + + render(root, [{tag: component, attrs: attrs}]) + + o(methods.view.callCount).equals(2) + o(methods.oninit.callCount).equals(1) + o(methods.oncreate.callCount).equals(1) + o(methods.onbeforeupdate.callCount).equals(1) + o(methods.onupdate.callCount).equals(1) + o(methods.onbeforeremove.callCount).equals(0) + o(methods.onremove.callCount).equals(0) + + hooks.forEach(function(hook) { + o(attrs[hook].callCount).equals(methods[hook].callCount)(hook) + }) + + render(root, []) + + o(methods.view.callCount).equals(2) + o(methods.oninit.callCount).equals(1) + o(methods.oncreate.callCount).equals(1) + o(methods.onbeforeupdate.callCount).equals(1) + o(methods.onupdate.callCount).equals(1) + o(methods.onbeforeremove.callCount).equals(1) + o(methods.onremove.callCount).equals(1) + + hooks.forEach(function(hook) { + o(attrs[hook].callCount).equals(methods[hook].callCount)(hook) + }) + }) o("hook state and arguments validation", function(){ var methods = { view: o.spy(function(vnode) {