Fix state initialization (#1652)

* Add test-utils/components.js and its tests

* Tests: group together tests with/without components

* Tests: factory => closure components

* Tests: add dummy forEach blocks around sections with components

* Tests: render/test-components tweaks

* Tests: Wrap some component definitions in `createComponent()` calls

These are the ones that would have been tedious to automate.
jscodeshift will handle the remaining ones

* Tests: wrap the rest of the components definitions

* Tests: enable the three kind of components in all related files but api/tests/test-route.js

* Add test-utils/components.js to index.html where needed

* Tests: Some more render/tests/test-component.js refactoring

* Tests: enable tests for #1638

* fix #1638

* Test: ensure that recycled components get a fresh state

* Tests: add a test for onbeforeupdate and recycled nodes

* Fix recycled components initialization
fix #1641
This commit is contained in:
Pierre-Yves Gérardy 2017-02-25 21:53:51 +01:00 committed by Isiah Meadows
parent 3786373b58
commit 60e8f307f1
15 changed files with 1615 additions and 1406 deletions

View file

@ -2,6 +2,7 @@
var o = require("../ospec/ospec")
var browserMock = require("../test-utils/browserMock")
var components = require("../test-utils/components")
o.spec("api", function() {
var m
@ -68,95 +69,6 @@ o.spec("api", function() {
o(query).equals("a=1&b=2")
})
})
o.spec("m.render", function() {
o("works", function() {
var root = window.document.createElement("div")
m.render(root, m("div"))
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
})
})
o.spec("m.mount", function() {
o("works", function() {
var root = window.document.createElement("div")
m.mount(root, {view: function() {return m("div")}})
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
})
})
o.spec("m.route", function() {
o("works", function(done) {
var root = window.document.createElement("div")
m.route(root, "/a", {
"/a": {view: function() {return m("div")}}
})
setTimeout(function() {
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
})
o("m.route.prefix", function(done) {
var root = window.document.createElement("div")
m.route.prefix("#")
m.route(root, "/a", {
"/a": {view: function() {return m("div")}}
})
setTimeout(function() {
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
})
o("m.route.get", function(done) {
var root = window.document.createElement("div")
m.route(root, "/a", {
"/a": {view: function() {return m("div")}}
})
setTimeout(function() {
o(m.route.get()).equals("/a")
done()
}, FRAME_BUDGET)
})
o("m.route.set", function(done, timeout) {
timeout(100)
var root = window.document.createElement("div")
m.route(root, "/a", {
"/:id": {view: function() {return m("div")}}
})
setTimeout(function() {
m.route.set("/b")
setTimeout(function() {
o(m.route.get()).equals("/b")
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET)
})
})
o.spec("m.redraw", function() {
o("works", function(done) {
var count = 0
var root = window.document.createElement("div")
m.mount(root, {view: function() {count++}})
setTimeout(function() {
m.redraw()
o(count).equals(2)
done()
}, FRAME_BUDGET)
})
})
o.spec("m.request", function() {
o("works", function() {
o(typeof m.request).equals("function") // TODO improve
@ -167,4 +79,99 @@ o.spec("api", function() {
o(typeof m.jsonp).equals("function") // TODO improve
})
})
})
o.spec("m.render", function() {
o("works", function() {
var root = window.document.createElement("div")
m.render(root, m("div"))
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
})
})
components.forEach(function(cmp){
o.spec(cmp.kind, function(){
var createComponent = cmp.create
o.spec("m.mount", function() {
o("works", function() {
var root = window.document.createElement("div")
m.mount(root, createComponent({view: function() {return m("div")}}))
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
})
})
o.spec("m.route", function() {
o("works", function(done) {
var root = window.document.createElement("div")
m.route(root, "/a", {
"/a": createComponent({view: function() {return m("div")}})
})
setTimeout(function() {
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
})
o("m.route.prefix", function(done) {
var root = window.document.createElement("div")
m.route.prefix("#")
m.route(root, "/a", {
"/a": createComponent({view: function() {return m("div")}})
})
setTimeout(function() {
o(root.childNodes.length).equals(1)
o(root.firstChild.nodeName).equals("DIV")
done()
}, FRAME_BUDGET)
})
o("m.route.get", function(done) {
var root = window.document.createElement("div")
m.route(root, "/a", {
"/a": createComponent({view: function() {return m("div")}})
})
setTimeout(function() {
o(m.route.get()).equals("/a")
done()
}, FRAME_BUDGET)
})
o("m.route.set", function(done, timeout) {
timeout(100)
var root = window.document.createElement("div")
m.route(root, "/a", {
"/:id": createComponent({view: function() {return m("div")}})
})
setTimeout(function() {
m.route.set("/b")
setTimeout(function() {
o(m.route.get()).equals("/b")
done()
}, FRAME_BUDGET)
}, FRAME_BUDGET)
})
})
o.spec("m.redraw", function() {
o("works", function(done) {
var count = 0
var root = window.document.createElement("div")
m.mount(root, createComponent({view: function() {count++}}))
setTimeout(function() {
m.redraw()
o(count).equals(2)
done()
}, FRAME_BUDGET)
})
})
})
})
})