| 1 |
/* eslint-disable @typescript-eslint/no-empty-function */ |
| 2 |
// Copyright (c) Jupyter Development Team. |
| 3 |
// Distributed under the terms of the Modified BSD License. |
| 4 |
/*----------------------------------------------------------------------------- |
| 5 |
| Copyright (c) 2014-2017, PhosphorJS Contributors |
| 6 |
| |
| 7 |
| Distributed under the terms of the BSD 3-Clause License. |
| 8 |
| |
| 9 |
| The full license is in the file LICENSE, distributed with this software. |
| 10 |
|----------------------------------------------------------------------------*/ |
| 11 |
import { expect } from 'chai'; |
| 12 |
|
| 13 |
import { h, VirtualDOM, VirtualElement, VirtualText } from '@lumino/virtualdom'; |
| 14 |
|
| 15 |
describe('@lumino/virtualdom', () => { |
| 16 |
describe('VirtualText', () => { |
| 17 |
describe('#constructor()', () => { |
| 18 |
it('should create a virtual text node', () => { |
| 19 |
let vnode = new VirtualText('foo'); |
| 20 |
expect(vnode).to.be.an.instanceof(VirtualText); |
| 21 |
}); |
| 22 |
}); |
| 23 |
|
| 24 |
describe('#type', () => { |
| 25 |
it('should be `text`', () => { |
| 26 |
let vnode = new VirtualText('foo'); |
| 27 |
expect(vnode.type).to.equal('text'); |
| 28 |
}); |
| 29 |
}); |
| 30 |
|
| 31 |
describe('#content', () => { |
| 32 |
it('should be the text content', () => { |
| 33 |
let vnode = new VirtualText('foo'); |
| 34 |
expect(vnode.content).to.equal('foo'); |
| 35 |
}); |
| 36 |
}); |
| 37 |
}); |
| 38 |
|
| 39 |
describe('VirtualElement', () => { |
| 40 |
describe('#constructor()', () => { |
| 41 |
it('should create a virtual element node', () => { |
| 42 |
let vnode = new VirtualElement('img', {}, []); |
| 43 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 44 |
}); |
| 45 |
}); |
| 46 |
|
| 47 |
describe('#type', () => { |
| 48 |
it('should be `element`', () => { |
| 49 |
let vnode = new VirtualElement('img', {}, []); |
| 50 |
expect(vnode.type).to.equal('element'); |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
describe('#tag', () => { |
| 55 |
it('should be the element tag name', () => { |
| 56 |
let vnode = new VirtualElement('img', {}, []); |
| 57 |
expect(vnode.tag).to.equal('img'); |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
describe('#attrs', () => { |
| 62 |
it('should be the element attrs', () => { |
| 63 |
let attrs = { className: 'bar' }; |
| 64 |
let vnode = new VirtualElement('img', attrs, []); |
| 65 |
expect(vnode.attrs).to.deep.equal(attrs); |
| 66 |
}); |
| 67 |
}); |
| 68 |
|
| 69 |
describe('#children', () => { |
| 70 |
it('should be the element children', () => { |
| 71 |
let children = [h.a(), h.img()]; |
| 72 |
let vnode = new VirtualElement('div', {}, children); |
| 73 |
expect(vnode.children).to.equal(children); |
| 74 |
}); |
| 75 |
}); |
| 76 |
}); |
| 77 |
|
| 78 |
describe('VirtualElement with custom .renderer', () => { |
| 79 |
let mockRenderer = { |
| 80 |
render: (host: HTMLElement) => {}, |
| 81 |
unrender: (host: HTMLElement) => {} |
| 82 |
}; |
| 83 |
|
| 84 |
describe('#constructor()', () => { |
| 85 |
it('should create a virtual element node', () => { |
| 86 |
let vnode = new VirtualElement('div', {}, [], mockRenderer); |
| 87 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 88 |
}); |
| 89 |
}); |
| 90 |
|
| 91 |
describe('#type', () => { |
| 92 |
it('should be `element`', () => { |
| 93 |
let vnode = new VirtualElement('div', {}, [], mockRenderer); |
| 94 |
expect(vnode.type).to.equal('element'); |
| 95 |
}); |
| 96 |
}); |
| 97 |
|
| 98 |
describe('#tag', () => { |
| 99 |
it('should be the element tag name', () => { |
| 100 |
let vnode = new VirtualElement('img', {}, [], mockRenderer); |
| 101 |
expect(vnode.tag).to.equal('img'); |
| 102 |
}); |
| 103 |
}); |
| 104 |
|
| 105 |
describe('#attrs', () => { |
| 106 |
it('should be the element attrs', () => { |
| 107 |
let attrs = { className: 'baz' }; |
| 108 |
let vnode = new VirtualElement('img', attrs, [], mockRenderer); |
| 109 |
expect(vnode.attrs).to.deep.equal(attrs); |
| 110 |
}); |
| 111 |
}); |
| 112 |
|
| 113 |
describe('#renderer', () => { |
| 114 |
it('should be the element children renderer', () => { |
| 115 |
let vnode = new VirtualElement('div', {}, [], mockRenderer); |
| 116 |
expect(vnode.renderer!.render).to.equal(mockRenderer.render); |
| 117 |
expect(vnode.renderer!.unrender).to.equal(mockRenderer.unrender); |
| 118 |
}); |
| 119 |
}); |
| 120 |
}); |
| 121 |
|
| 122 |
describe('h()', () => { |
| 123 |
it('should create a new virtual element node', () => { |
| 124 |
let vnode = h('a'); |
| 125 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 126 |
}); |
| 127 |
|
| 128 |
it('should accept string literals for children and convert them to text nodes', () => { |
| 129 |
let vnode = h('div', {}, ['foo', 'bar']); |
| 130 |
expect(vnode.children[0]).to.be.an.instanceof(VirtualText); |
| 131 |
expect(vnode.children[1]).to.be.an.instanceof(VirtualText); |
| 132 |
expect(vnode.children[0].type).to.equal('text'); |
| 133 |
expect(vnode.children[1].type).to.equal('text'); |
| 134 |
expect((vnode.children[0] as VirtualText).content).to.equal('foo'); |
| 135 |
expect((vnode.children[1] as VirtualText).content).to.equal('bar'); |
| 136 |
}); |
| 137 |
|
| 138 |
it('should accept other virtual DOM nodes for children', () => { |
| 139 |
let children = [h('a'), h('img')]; |
| 140 |
let vnode = h('div', {}, children); |
| 141 |
expect(vnode.children[0]).to.equal(children[0]); |
| 142 |
expect(vnode.children[1]).to.equal(children[1]); |
| 143 |
expect(vnode.children[0].type).to.equal('element'); |
| 144 |
expect(vnode.children[1].type).to.equal('element'); |
| 145 |
expect((vnode.children[0] as VirtualElement).tag).to.equal('a'); |
| 146 |
expect((vnode.children[1] as VirtualElement).tag).to.equal('img'); |
| 147 |
}); |
| 148 |
|
| 149 |
it('should accept a mix of string literals and virtual DOM nodes', () => { |
| 150 |
let children = ['foo', h('img')]; |
| 151 |
let vnode = h('div', {}, children); |
| 152 |
expect(vnode.children[1]).to.equal(children[1]); |
| 153 |
expect(vnode.children[0].type).to.equal('text'); |
| 154 |
expect((vnode.children[0] as VirtualText).content).to.equal('foo'); |
| 155 |
expect(vnode.children[1].type).to.equal('element'); |
| 156 |
expect((vnode.children[1] as VirtualElement).tag).to.equal('img'); |
| 157 |
}); |
| 158 |
|
| 159 |
it('should ignore `null` child values', () => { |
| 160 |
let children = ['foo', null, h('img')]; |
| 161 |
let vnode = h('div', {}, children); |
| 162 |
expect(vnode.children[1]).to.equal(children[2]); |
| 163 |
expect(vnode.children[0].type).to.equal('text'); |
| 164 |
expect((vnode.children[0] as VirtualText).content).to.equal('foo'); |
| 165 |
expect(vnode.children[1].type).to.equal('element'); |
| 166 |
expect((vnode.children[1] as VirtualElement).tag).to.equal('img'); |
| 167 |
}); |
| 168 |
|
| 169 |
it('should accept a string as the second argument', () => { |
| 170 |
let vnode = h('div', 'foo'); |
| 171 |
expect(vnode.children[0].type).to.equal('text'); |
| 172 |
expect((vnode.children[0] as VirtualText).content).to.equal('foo'); |
| 173 |
}); |
| 174 |
|
| 175 |
it('should accept a virtual node as the second argument', () => { |
| 176 |
let vnode = h('div', h('a')); |
| 177 |
expect(vnode.children[0].type).to.equal('element'); |
| 178 |
expect((vnode.children[0] as VirtualElement).tag).to.equal('a'); |
| 179 |
}); |
| 180 |
|
| 181 |
it('should accept an array as the second argument', () => { |
| 182 |
let children = [h('a'), h('img')]; |
| 183 |
let vnode = h('div', children); |
| 184 |
expect(vnode.children[0]).to.equal(children[0]); |
| 185 |
expect(vnode.children[0].type).to.equal('element'); |
| 186 |
expect((vnode.children[0] as VirtualElement).tag).to.equal('a'); |
| 187 |
expect(vnode.children[1].type).to.equal('element'); |
| 188 |
expect((vnode.children[1] as VirtualElement).tag).to.equal('img'); |
| 189 |
}); |
| 190 |
|
| 191 |
it('should accept other nodes as variadic args', () => { |
| 192 |
let vnode = h('div', h('a'), h('img')); |
| 193 |
expect(vnode.children[0].type).to.equal('element'); |
| 194 |
expect((vnode.children[0] as VirtualElement).tag).to.equal('a'); |
| 195 |
expect(vnode.children[1].type).to.equal('element'); |
| 196 |
expect((vnode.children[1] as VirtualElement).tag).to.equal('img'); |
| 197 |
}); |
| 198 |
|
| 199 |
it('should set the attrs directly', () => { |
| 200 |
let attrs = { style: { color: 'red' }, dataset: { a: '1' } }; |
| 201 |
let vnode = h('img', attrs); |
| 202 |
expect(vnode.attrs).to.deep.equal(attrs); |
| 203 |
}); |
| 204 |
}); |
| 205 |
|
| 206 |
describe('h', () => { |
| 207 |
it('should create the appropriate element tag', () => { |
| 208 |
expect(h.a().tag).to.equal('a'); |
| 209 |
expect(h.abbr().tag).to.equal('abbr'); |
| 210 |
expect(h.address().tag).to.equal('address'); |
| 211 |
expect(h.area().tag).to.equal('area'); |
| 212 |
expect(h.article().tag).to.equal('article'); |
| 213 |
expect(h.aside().tag).to.equal('aside'); |
| 214 |
expect(h.audio().tag).to.equal('audio'); |
| 215 |
expect(h.b().tag).to.equal('b'); |
| 216 |
expect(h.bdi().tag).to.equal('bdi'); |
| 217 |
expect(h.bdo().tag).to.equal('bdo'); |
| 218 |
expect(h.blockquote().tag).to.equal('blockquote'); |
| 219 |
expect(h.br().tag).to.equal('br'); |
| 220 |
expect(h.button().tag).to.equal('button'); |
| 221 |
expect(h.canvas().tag).to.equal('canvas'); |
| 222 |
expect(h.caption().tag).to.equal('caption'); |
| 223 |
expect(h.cite().tag).to.equal('cite'); |
| 224 |
expect(h.code().tag).to.equal('code'); |
| 225 |
expect(h.col().tag).to.equal('col'); |
| 226 |
expect(h.colgroup().tag).to.equal('colgroup'); |
| 227 |
expect(h.data().tag).to.equal('data'); |
| 228 |
expect(h.datalist().tag).to.equal('datalist'); |
| 229 |
expect(h.dd().tag).to.equal('dd'); |
| 230 |
expect(h.del().tag).to.equal('del'); |
| 231 |
expect(h.dfn().tag).to.equal('dfn'); |
| 232 |
expect(h.div().tag).to.equal('div'); |
| 233 |
expect(h.dl().tag).to.equal('dl'); |
| 234 |
expect(h.dt().tag).to.equal('dt'); |
| 235 |
expect(h.em().tag).to.equal('em'); |
| 236 |
expect(h.embed().tag).to.equal('embed'); |
| 237 |
expect(h.fieldset().tag).to.equal('fieldset'); |
| 238 |
expect(h.figcaption().tag).to.equal('figcaption'); |
| 239 |
expect(h.figure().tag).to.equal('figure'); |
| 240 |
expect(h.footer().tag).to.equal('footer'); |
| 241 |
expect(h.form().tag).to.equal('form'); |
| 242 |
expect(h.h1().tag).to.equal('h1'); |
| 243 |
expect(h.h2().tag).to.equal('h2'); |
| 244 |
expect(h.h3().tag).to.equal('h3'); |
| 245 |
expect(h.h4().tag).to.equal('h4'); |
| 246 |
expect(h.h5().tag).to.equal('h5'); |
| 247 |
expect(h.h6().tag).to.equal('h6'); |
| 248 |
expect(h.header().tag).to.equal('header'); |
| 249 |
expect(h.hr().tag).to.equal('hr'); |
| 250 |
expect(h.i().tag).to.equal('i'); |
| 251 |
expect(h.iframe().tag).to.equal('iframe'); |
| 252 |
expect(h.img().tag).to.equal('img'); |
| 253 |
expect(h.input().tag).to.equal('input'); |
| 254 |
expect(h.ins().tag).to.equal('ins'); |
| 255 |
expect(h.kbd().tag).to.equal('kbd'); |
| 256 |
expect(h.label().tag).to.equal('label'); |
| 257 |
expect(h.legend().tag).to.equal('legend'); |
| 258 |
expect(h.li().tag).to.equal('li'); |
| 259 |
expect(h.main().tag).to.equal('main'); |
| 260 |
expect(h.map().tag).to.equal('map'); |
| 261 |
expect(h.mark().tag).to.equal('mark'); |
| 262 |
expect(h.meter().tag).to.equal('meter'); |
| 263 |
expect(h.nav().tag).to.equal('nav'); |
| 264 |
expect(h.noscript().tag).to.equal('noscript'); |
| 265 |
expect(h.object().tag).to.equal('object'); |
| 266 |
expect(h.ol().tag).to.equal('ol'); |
| 267 |
expect(h.optgroup().tag).to.equal('optgroup'); |
| 268 |
expect(h.option().tag).to.equal('option'); |
| 269 |
expect(h.output().tag).to.equal('output'); |
| 270 |
expect(h.p().tag).to.equal('p'); |
| 271 |
expect(h.param().tag).to.equal('param'); |
| 272 |
expect(h.pre().tag).to.equal('pre'); |
| 273 |
expect(h.progress().tag).to.equal('progress'); |
| 274 |
expect(h.q().tag).to.equal('q'); |
| 275 |
expect(h.rp().tag).to.equal('rp'); |
| 276 |
expect(h.rt().tag).to.equal('rt'); |
| 277 |
expect(h.ruby().tag).to.equal('ruby'); |
| 278 |
expect(h.s().tag).to.equal('s'); |
| 279 |
expect(h.samp().tag).to.equal('samp'); |
| 280 |
expect(h.section().tag).to.equal('section'); |
| 281 |
expect(h.select().tag).to.equal('select'); |
| 282 |
expect(h.small().tag).to.equal('small'); |
| 283 |
expect(h.source().tag).to.equal('source'); |
| 284 |
expect(h.span().tag).to.equal('span'); |
| 285 |
expect(h.strong().tag).to.equal('strong'); |
| 286 |
expect(h.sub().tag).to.equal('sub'); |
| 287 |
expect(h.summary().tag).to.equal('summary'); |
| 288 |
expect(h.sup().tag).to.equal('sup'); |
| 289 |
expect(h.table().tag).to.equal('table'); |
| 290 |
expect(h.tbody().tag).to.equal('tbody'); |
| 291 |
expect(h.td().tag).to.equal('td'); |
| 292 |
expect(h.textarea().tag).to.equal('textarea'); |
| 293 |
expect(h.tfoot().tag).to.equal('tfoot'); |
| 294 |
expect(h.th().tag).to.equal('th'); |
| 295 |
expect(h.thead().tag).to.equal('thead'); |
| 296 |
expect(h.time().tag).to.equal('time'); |
| 297 |
expect(h.title().tag).to.equal('title'); |
| 298 |
expect(h.tr().tag).to.equal('tr'); |
| 299 |
expect(h.track().tag).to.equal('track'); |
| 300 |
expect(h.u().tag).to.equal('u'); |
| 301 |
expect(h.ul().tag).to.equal('ul'); |
| 302 |
expect(h.var_().tag).to.equal('var'); |
| 303 |
expect(h.video().tag).to.equal('video'); |
| 304 |
expect(h.wbr().tag).to.equal('wbr'); |
| 305 |
}); |
| 306 |
}); |
| 307 |
|
| 308 |
describe('h() with IRenderer param', () => { |
| 309 |
let tag = 'div'; |
| 310 |
let attrs = { className: 'baz' }; |
| 311 |
let mockRenderer = { |
| 312 |
render: (host: HTMLElement) => {}, |
| 313 |
unrender: (host: HTMLElement) => {} |
| 314 |
}; |
| 315 |
|
| 316 |
it('should create a new virtual element with custom renderer', () => { |
| 317 |
let vnode = h(tag, attrs, mockRenderer); |
| 318 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 319 |
expect(vnode.tag).to.equal(tag); |
| 320 |
expect(vnode.attrs).to.deep.equal(attrs); |
| 321 |
expect(vnode.renderer!.render).to.equal(mockRenderer.render); |
| 322 |
expect(vnode.renderer!.unrender).to.equal(mockRenderer.unrender); |
| 323 |
}); |
| 324 |
|
| 325 |
it('should create a virtual element with custom renderer and without attrs', () => { |
| 326 |
let vnode = h('div', mockRenderer); |
| 327 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 328 |
expect(vnode.tag).to.equal('div'); |
| 329 |
expect(vnode.attrs).to.deep.equal({}); |
| 330 |
expect(vnode.renderer!.render).to.equal(mockRenderer.render); |
| 331 |
expect(vnode.renderer!.unrender).to.equal(mockRenderer.unrender); |
| 332 |
}); |
| 333 |
|
| 334 |
it('should create a virtual element without custom renderer and with attrs', () => { |
| 335 |
let vnode = h('div', attrs); |
| 336 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 337 |
expect(vnode.tag).to.equal(tag); |
| 338 |
expect(vnode.attrs).to.deep.equal(attrs); |
| 339 |
expect(vnode.renderer).to.equal(undefined); |
| 340 |
}); |
| 341 |
|
| 342 |
it('should create a virtual element without custom renderer or attrs', () => { |
| 343 |
let vnode = h('div'); |
| 344 |
expect(vnode).to.be.an.instanceof(VirtualElement); |
| 345 |
expect(vnode.tag).to.equal('div'); |
| 346 |
expect(vnode.attrs).to.deep.equal({}); |
| 347 |
expect(vnode.renderer).to.equal(undefined); |
| 348 |
}); |
| 349 |
}); |
| 350 |
|
| 351 |
describe('VirtualDOM', () => { |
| 352 |
describe('realize()', () => { |
| 353 |
it('should create a real DOM node from a virtual DOM node', () => { |
| 354 |
let node = VirtualDOM.realize(h.div([h.a(), h.img()])); |
| 355 |
expect(node.nodeName.toLowerCase()).to.equal('div'); |
| 356 |
expect(node.children[0].nodeName.toLowerCase()).to.equal('a'); |
| 357 |
expect(node.children[1].nodeName.toLowerCase()).to.equal('img'); |
| 358 |
}); |
| 359 |
}); |
| 360 |
|
| 361 |
describe('render()', () => { |
| 362 |
it('should render virtual DOM content into a host elememnt', () => { |
| 363 |
let host = document.createElement('div'); |
| 364 |
VirtualDOM.render(h.img(), host); |
| 365 |
expect(host.children[0].nodeName.toLowerCase()).to.equal('img'); |
| 366 |
}); |
| 367 |
|
| 368 |
it('should render the delta from the previous rendering', () => { |
| 369 |
let host = document.createElement('div'); |
| 370 |
let children = [h.a(), h.span(), h.img()]; |
| 371 |
VirtualDOM.render(children, host); |
| 372 |
let first = host.children[0]; |
| 373 |
let last = host.children[2]; |
| 374 |
expect(first.nodeName.toLowerCase()).to.equal('a'); |
| 375 |
expect(last.nodeName.toLowerCase()).to.equal('img'); |
| 376 |
children = [children[0], h.div(), children[1]]; |
| 377 |
VirtualDOM.render(children, host); |
| 378 |
expect(host.children[0]).to.equal(first); |
| 379 |
expect(host.children[2]).to.not.equal(last); |
| 380 |
expect(host.children[2].nodeName.toLowerCase()).to.equal('span'); |
| 381 |
}); |
| 382 |
|
| 383 |
it('should clear the rendering if `null` content is provided', () => { |
| 384 |
let host = document.createElement('div'); |
| 385 |
VirtualDOM.render(h('div', ['bar', 'foo']), host); |
| 386 |
expect(host.children[0].childNodes.length).to.equal(2); |
| 387 |
VirtualDOM.render(null, host); |
| 388 |
expect(host.children.length).to.equal(0); |
| 389 |
}); |
| 390 |
|
| 391 |
it('should update attributes', () => { |
| 392 |
let host = document.createElement('div'); |
| 393 |
let attrs1 = { |
| 394 |
alt: 'foo', |
| 395 |
height: '100', |
| 396 |
style: { color: 'white' }, |
| 397 |
dataset: { foo: '2', bar: '2' }, |
| 398 |
onload: () => {}, |
| 399 |
srcset: 'foo' |
| 400 |
}; |
| 401 |
let attrs2 = { |
| 402 |
alt: 'bar', |
| 403 |
width: '100', |
| 404 |
style: { border: '1px' }, |
| 405 |
dataset: { bar: '1', baz: '3' }, |
| 406 |
sizes: 'baz' |
| 407 |
}; |
| 408 |
VirtualDOM.render([h.a(), h.img(attrs1)], host); |
| 409 |
VirtualDOM.render([h.a(), h.img(attrs2)], host); |
| 410 |
expect((host.children[1] as HTMLImageElement).alt).to.equal('bar'); |
| 411 |
}); |
| 412 |
|
| 413 |
it('should not recreate a DOM node that moves if it has a key id', () => { |
| 414 |
let host = document.createElement('div'); |
| 415 |
let children1 = [ |
| 416 |
h.span({ key: '1' }), |
| 417 |
h.span({ key: '2' }), |
| 418 |
h.span({ key: '3' }), |
| 419 |
h.span({ key: '4' }) |
| 420 |
]; |
| 421 |
let children2 = [ |
| 422 |
h.span({ key: '1' }), |
| 423 |
h.span({ key: '3' }), |
| 424 |
h.span({ key: '2' }), |
| 425 |
h.span({ key: '4' }) |
| 426 |
]; |
| 427 |
VirtualDOM.render(children1, host); |
| 428 |
let child1 = host.children[1]; |
| 429 |
let child2 = host.children[2]; |
| 430 |
VirtualDOM.render(children2, host); |
| 431 |
expect(host.children[1]).to.equal(child2); |
| 432 |
expect(host.children[2]).to.equal(child1); |
| 433 |
}); |
| 434 |
|
| 435 |
it('should still recreate the DOM node if the node type changes', () => { |
| 436 |
let host = document.createElement('div'); |
| 437 |
let children1 = [ |
| 438 |
h.span({ key: '1' }), |
| 439 |
h.span({ key: '2' }), |
| 440 |
h.span({ key: '3' }), |
| 441 |
h.span({ key: '4' }) |
| 442 |
]; |
| 443 |
let children2 = [ |
| 444 |
h.span({ key: '1' }), |
| 445 |
h.div({ key: '3' }), |
| 446 |
h.span({ key: '2' }), |
| 447 |
h.span({ key: '4' }) |
| 448 |
]; |
| 449 |
VirtualDOM.render(children1, host); |
| 450 |
VirtualDOM.render(children2, host); |
| 451 |
expect(host.children[1].nodeName.toLowerCase()).to.equal('div'); |
| 452 |
}); |
| 453 |
|
| 454 |
it('should handle a new keyed item', () => { |
| 455 |
let host = document.createElement('div'); |
| 456 |
let children1 = [ |
| 457 |
h.span({ key: '1' }), |
| 458 |
h.span({ key: '2' }), |
| 459 |
h.span({ key: '3' }), |
| 460 |
h.span({ key: '4' }) |
| 461 |
]; |
| 462 |
let children2 = [ |
| 463 |
h.span({ key: '1' }), |
| 464 |
h.span({ key: '2' }), |
| 465 |
h.span({ key: '3' }), |
| 466 |
h.div({ key: '5' }) |
| 467 |
]; |
| 468 |
VirtualDOM.render(children1, host); |
| 469 |
VirtualDOM.render(children2, host); |
| 470 |
expect(host.children[3].nodeName.toLowerCase()).to.equal('div'); |
| 471 |
}); |
| 472 |
|
| 473 |
it('should update the text of a text node', () => { |
| 474 |
let host = document.createElement('div'); |
| 475 |
VirtualDOM.render(h.div('foo'), host); |
| 476 |
let div = host.children[0]; |
| 477 |
expect(div.textContent).to.equal('foo'); |
| 478 |
VirtualDOM.render(h.div('bar'), host); |
| 479 |
expect(host.children[0]).to.equal(div); |
| 480 |
expect(div.textContent).to.equal('bar'); |
| 481 |
}); |
| 482 |
}); |
| 483 |
}); |
| 484 |
|
| 485 |
describe('VirtualDOM with custom renderer', () => { |
| 486 |
const rendererClosure = (record: any = {}) => { |
| 487 |
return { |
| 488 |
render: (host: HTMLElement) => { |
| 489 |
const renderNode = document.createElement('div'); |
| 490 |
renderNode.className = 'lm-render'; |
| 491 |
host.appendChild(renderNode); |
| 492 |
record.child = renderNode; |
| 493 |
}, |
| 494 |
unrender: (host: HTMLElement) => { |
| 495 |
host.removeChild(host.lastChild as HTMLElement); |
| 496 |
record.cleanedUp = true; |
| 497 |
} |
| 498 |
}; |
| 499 |
}; |
| 500 |
|
| 501 |
describe('realize()', () => { |
| 502 |
it('should realize successfully', () => { |
| 503 |
let node = VirtualDOM.realize(h('span', rendererClosure())); |
| 504 |
expect(node.tagName.toLowerCase()).to.equal('span'); |
| 505 |
expect(node.children[0].tagName.toLowerCase()).to.equal('div'); |
| 506 |
expect(node.children[0].className).to.equal('lm-render'); |
| 507 |
}); |
| 508 |
}); |
| 509 |
|
| 510 |
describe('render()', () => { |
| 511 |
it('should render successfully at top of tree', () => { |
| 512 |
let host = document.createElement('div'); |
| 513 |
|
| 514 |
VirtualDOM.render(h('span', rendererClosure()), host); |
| 515 |
expect(host.children[0].tagName.toLowerCase()).to.equal('span'); |
| 516 |
expect(host.children[0].children[0].tagName.toLowerCase()).to.equal( |
| 517 |
'div' |
| 518 |
); |
| 519 |
expect(host.children[0].children[0].className).to.equal('lm-render'); |
| 520 |
}); |
| 521 |
|
| 522 |
it('should render child node', () => { |
| 523 |
let host = document.createElement('div'); |
| 524 |
let record: any = { child: undefined, cleanedUp: false }; |
| 525 |
|
| 526 |
let children = [ |
| 527 |
h.a(), |
| 528 |
h.span(), |
| 529 |
h.div(h.div(), h('span', rendererClosure(record)), h.div()) |
| 530 |
]; |
| 531 |
VirtualDOM.render(children, host); |
| 532 |
expect(host.children[2].children[1].children[0]).to.equal(record.child); |
| 533 |
expect(host.children[2].children[1].children[0].className).to.equal( |
| 534 |
'lm-render' |
| 535 |
); |
| 536 |
}); |
| 537 |
|
| 538 |
it('should cleanup child node', () => { |
| 539 |
let host = document.createElement('div'); |
| 540 |
let record: any = { child: undefined, cleanedUp: false }; |
| 541 |
|
| 542 |
// first pass, render the custom children |
| 543 |
let children0 = [ |
| 544 |
h.a(), |
| 545 |
h.span(), |
| 546 |
h.div(h.div(), h('span', rendererClosure(record)), h.div()) |
| 547 |
]; |
| 548 |
VirtualDOM.render(children0, host); |
| 549 |
|
| 550 |
// second pass, explicitly unrender the custom children |
| 551 |
let children1 = [h.a(), h.span(), h.label()]; |
| 552 |
VirtualDOM.render(children1, host); |
| 553 |
expect(record.cleanedUp).to.equal(true); |
| 554 |
}); |
| 555 |
}); |
| 556 |
}); |
| 557 |
}); |