var tape = require('tape')
var html = require('nanohtml')
var nanomorph = require('../')
module.exports = abstractMorph
function abstractMorph (morph) {
tape('abstract morph', function (t) {
t.test('root level', function (t) {
t.test('should replace a node', function (t) {
t.plan(1)
var a = html`
hello world
`
var b = html`
hello world
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should replace a component', function (t) {
t.plan(2)
function ComponentA () {
return html`
hello world
`
}
function ComponentB () {
return html`
bye moon
`
}
var a = ComponentA()
var b = ComponentB()
var expectedA = a.outerHTML
var expectedB = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expectedB, 'result was expected')
t.equal(a.outerHTML, expectedA, 'did not mutate input tree')
})
t.test('should morph a node', function (t) {
t.plan(1)
var a = html`
hello world
`
var b = html`
hello you
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should morph a node with namespaced attribute', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should ignore if node is same', function (t) {
t.plan(1)
var a = html`
hello world
`
var expected = a.outerHTML
var res = morph(a, a)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.end()
})
t.test('nested', function (t) {
t.test('should replace a node', function (t) {
t.plan(1)
var a = html`
hello world
`
var b = html`
hello world
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should replace a node', function (t) {
t.plan(1)
var a = html`
hello world
`
var b = html`
hello you
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should replace a node', function (t) {
t.plan(1)
var a = html`
hello world
`
var res = morph(a, a)
var expected = a.outerHTML
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should append a node', function (t) {
t.plan(1)
var a = html``
var b = html`
hello you
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should remove a node', function (t) {
t.plan(1)
var a = html`
hello you
`
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should update child nodes', function (t) {
t.plan(1)
var a = html`
hello world
`
var b = html`
hello you
`
var expected = '
hello you
'
var res = morph(a, b, { childrenOnly: true })
t.equal(res.outerHTML, expected, 'result was expected')
})
t.end()
})
t.test('values', function (t) {
t.test('if new tree has no value and old tree does, remove value', function (t) {
t.plan(4)
var a = html``
var b = html``
var res = morph(a, b)
t.equal(res.getAttribute('value'), null)
t.equal(res.value, '')
a = html``
b = html``
res = morph(a, b)
t.equal(res.getAttribute('value'), null)
t.equal(res.value, '')
})
t.test('if new tree has value and old tree does too, set value from new tree', function (t) {
t.plan(4)
var a = html``
var b = html``
var res = morph(a, b)
t.equal(res.value, 'hi')
a = html``
a.value = 'howdy'
b = html``
b.value = 'hi'
res = morph(a, b)
t.equal(res.value, 'hi')
a = html``
b = html``
b.value = 'hi'
res = morph(a, b)
t.equal(res.value, 'hi')
a = html``
a.value = 'howdy'
b = html``
res = morph(a, b)
t.equal(res.value, 'hi')
})
t.end()
})
function booleanPropertyTest (property) {
return function (t) {
t.test(`if new tree has no ${property} and old tree does, remove value`, function (t) {
t.plan(1)
var a = html``
var b = html``
var res = morph(a, b)
t.equal(res[property], false)
})
t.test(`if new tree has ${property} and old tree does not, add value`, function (t) {
t.plan(1)
var a = html``
var b = html``
var res = morph(a, b)
t.equal(res[property], true)
})
t.test(`if new tree has ${property} and old tree does too, set value from new tree`, function (t) {
t.plan(6)
var a = html``
var b = html``
var res = morph(a, b)
t.equal(res[property], true)
a = html``
b = html``
res = morph(a, b)
t.equal(res[property], false)
a = html``
b = html``
b[property] = true
res = morph(a, b)
t.equal(res[property], true)
a = html``
b = html``
b[property] = true
res = morph(a, b)
t.equal(res[property], true)
a = html``
b = html``
b[property] = false
res = morph(a, b)
t.equal(res[property], false)
a = html``
b = html``
res = morph(a, b)
t.equal(res[property], true)
})
t.end()
}
}
t.test('checked', booleanPropertyTest('checked'))
t.test('disabled', booleanPropertyTest('disabled'))
t.test('indeterminate', function (t) {
t.plan(2)
var a = html``
var b = html``
b.indeterminate = true
var res = morph(a, b)
t.equal(res.indeterminate, true)
a = html``
b = html``
a.indeterminate = true
res = morph(a, b)
t.equal(res.indeterminate, false)
})
t.test('isSameNode', function (t) {
t.test('should return a if true', function (t) {
t.plan(1)
var a = html`
YOLO
`
var b = html`
FOMO
`
b.isSameNode = function (el) {
return true
}
var res = morph(a, b)
t.equal(res.childNodes[0].data, 'YOLO')
})
t.test('should return b if false', function (t) {
t.plan(1)
var a = html`
YOLO
`
var b = html`
FOMO
`
b.isSameNode = function (el) {
return false
}
var res = morph(a, b)
t.equal(res.childNodes[0].data, 'FOMO')
})
t.end()
})
t.test('lists', function (t) {
t.test('should append nodes', function (t) {
t.plan(1)
var a = html`
`
var b = html`
1
2
3
4
5
`
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should remove nodes', function (t) {
t.plan(1)
var a = html`
1
2
3
4
5
`
var b = html`
`
var res = morph(a, b)
var expected = b.outerHTML
t.equal(res.outerHTML, expected, 'result was expected')
})
t.end()
})
t.test('selectables', function (t) {
t.test('should append nodes', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should append nodes (including optgroups)', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should remove nodes', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should remove nodes (including optgroups)', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should add selected', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should add selected (xhtml)', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.test('should switch selected', function (t) {
t.plan(1)
var a = html``
var b = html``
var expected = b.outerHTML
var res = morph(a, b)
t.equal(res.outerHTML, expected, 'result was expected')
})
t.end()
})
t.test('should replace nodes', function (t) {
t.plan(1)
var a = html`
1
2
3
4
5
`
var b = html`
1
2
3
4
5
`
var expected = b.outerHTML
a = morph(a, b)
t.equal(a.outerHTML, expected, 'result was expected')
})
t.test('should replace nodes after multiple iterations', function (t) {
t.plan(2)
var a = html`
`
var b = html`
1
2
3
4
5
`
var expected = b.outerHTML
a = morph(a, b)
t.equal(a.outerHTML, expected, 'result was expected')
b = html`
1
2
3
4
5
`
expected = b.outerHTML
a = morph(a, b)
t.equal(a.outerHTML, expected, 'result was expected')
})
t.end()
})
}
tape('use id as a key hint', function (t) {
t.test('append an element', function (t) {
var a = html`
`
var b = html`
`
var target = b.outerHTML
var oldFirst = a.children[0]
var oldSecond = a.children[1]
var oldThird = a.children[2]
var c = nanomorph(a, b)
t.equal(oldFirst, c.children[0], 'first is equal')
t.equal(oldSecond, c.children[2], 'moved second is equal')
t.equal(oldThird, c.children[3], 'moved third is equal')
t.equal(c.outerHTML, target)
t.end()
})
t.test('handle non-id elements', function (t) {
var a = html`
`
var b = html`
`
var target = b.outerHTML
var oldSecond = a.children[1]
var oldThird = a.children[2]
var oldForth = a.children[3]
var c = nanomorph(a, b)
t.equal(oldSecond, c.children[1], 'second is equal')
t.equal(oldThird, c.children[3], 'moved third is equal')
t.equal(oldForth, c.children[4], 'moved forth is equal')
t.equal(c.outerHTML, target)
t.end()
})
t.test('copy over children', function (t) {
var a = html`'hello'`
var b = html``
var expected = b.outerHTML
var c = nanomorph(a, b)
t.equal(c.outerHTML, expected, expected)
t.end()
})
t.test('remove an element', function (t) {
var a = html`
`
var b = html`
`
var oldFirst = a.children[0]
var oldThird = a.children[2]
var expected = b.outerHTML
var c = nanomorph(a, b)
t.equal(c.children[0], oldFirst, 'first is equal')
t.equal(c.children[1], oldThird, 'second untouched')
t.equal(c.outerHTML, expected)
t.end()
})
t.test('swap proxy elements', function (t) {
var nodeA = html``
var placeholderA = html``
placeholderA.isSameNode = function (el) {
return el === nodeA
}
var nodeB = html``
var placeholderB = html``
placeholderB.isSameNode = function (el) {
return el === nodeB
}
var a = html`
${nodeA}${nodeB}
`
var b = html`
${placeholderB}${placeholderA}
`
var c = nanomorph(a, b)
t.equal(c.children[0], nodeB, 'c.children[0] === nodeB')
t.equal(c.children[1], nodeA, 'c.children[1] === nodeA')
t.end()
})
t.test('id match still morphs', function (t) {
var a = html`
FOO
`
var b = html`
BAR
`
var target = b.outerHTML
var c = nanomorph(a, b)
t.equal(c.outerHTML, target)
t.end()
})
t.test('remove orphaned keyed nodes', function (t) {
var a = html`
1
a
`
var b = html`
2
b
`
var expected = b.outerHTML
var c = nanomorph(a, b)
t.equal(c.outerHTML, expected)
t.end()
})
t.test('whitespace', function (t) {
var a = html`
`
var b = html`
`
var expected = b.outerHTML
var c = nanomorph(a, b)
t.equal(c.outerHTML, expected)
t.end()
})
t.test('nested with id', function (t) {
var child = html``
var placeholder = html``
placeholder.isSameNode = function (el) { return el === child }
var a = html`
${child}
`
var b = html`
${placeholder}
`
var c = nanomorph(a, b)
t.equal(c.children[0].children[0], child, 'is the same node')
t.end()
})
t.test('nested without id', function (t) {
var child = html`
child
`
var placeholder = html`
placeholder
`
placeholder.isSameNode = function (el) { return el === child }
var a = html`
${child}
`
var b = html`
${placeholder}
`
var c = nanomorph(a, b)
t.equal(c.children[0].children[0], child, 'is the same node')
t.end()
})
t.end()
})
tape('fragments', function (t) {
t.test('disallow document fragments', function (t) {
var a = html`
a
`
var b = html`
a
b
`
t.throws(nanomorph.bind(null, a, b), /newTree should have one root node/, 'no fragments')
t.end()
})
t.test('allow document fragments with `childrenOnly`', function (t) {
var a = html`
a
`
var b = html`
a
b
`
var c = nanomorph(a, b, { childrenOnly: true })
t.equals(c.outerHTML, '