|
| 1 | +import {expect} from 'chai' |
| 2 | +import {html, render, TemplateResult} from '../lib/index.js' |
| 3 | + |
| 4 | +describe('iterables', () => { |
| 5 | + let surface: HTMLElement |
| 6 | + beforeEach(() => { |
| 7 | + surface = document.createElement('section') |
| 8 | + }) |
| 9 | + |
| 10 | + it('supports arrays of strings in nodes', () => { |
| 11 | + const main = (list: string[]) => html`<div>${list}</div>` |
| 12 | + render(main(['one', 'two', 'three']), surface) |
| 13 | + expect(surface.innerHTML).to.equal('<div>onetwothree</div>') |
| 14 | + render(main(['four', 'five', 'six']), surface) |
| 15 | + expect(surface.innerHTML).to.equal('<div>fourfivesix</div>') |
| 16 | + }) |
| 17 | + |
| 18 | + it('supports iterables of Sub Templates with text nodes', () => { |
| 19 | + const main = (list: Iterable<TemplateResult>) => html`<div>${list}</div>` |
| 20 | + let fragments = ['one', 'two', 'three'].map(text => html`${text}`) |
| 21 | + render(main(fragments), surface) |
| 22 | + expect(surface.innerHTML).to.equal('<div>onetwothree</div>') |
| 23 | + fragments = ['four', 'five', 'six'].map(text => html`${text}`) |
| 24 | + render(main(fragments), surface) |
| 25 | + expect(surface.innerHTML).to.equal('<div>fourfivesix</div>') |
| 26 | + }) |
| 27 | + |
| 28 | + it('supports iterables of fragments with text nodes', () => { |
| 29 | + const main = (list: Iterable<DocumentFragment>) => html`<div>${list}</div>` |
| 30 | + let fragments = ['one', 'two', 'three'].map(text => { |
| 31 | + const fragment = document.createDocumentFragment() |
| 32 | + fragment.append(new Text(text)) |
| 33 | + return fragment |
| 34 | + }) |
| 35 | + render(main(fragments), surface) |
| 36 | + expect(surface.innerHTML).to.equal('<div>onetwothree</div>') |
| 37 | + fragments = ['four', 'five', 'six'].map(text => { |
| 38 | + const fragment = document.createDocumentFragment() |
| 39 | + fragment.append(new Text(text)) |
| 40 | + return fragment |
| 41 | + }) |
| 42 | + render(main(fragments), surface) |
| 43 | + expect(surface.innerHTML).to.equal('<div>fourfivesix</div>') |
| 44 | + }) |
| 45 | + |
| 46 | + it('supports other strings iterables in nodes', () => { |
| 47 | + const main = (list: Iterable<string>) => html`<div>${list}</div>` |
| 48 | + render(main(new Set(['one', 'two', 'three'])), surface) |
| 49 | + expect(surface.innerHTML).to.equal('<div>onetwothree</div>') |
| 50 | + render( |
| 51 | + main( |
| 52 | + new Map([ |
| 53 | + [4, 'four'], |
| 54 | + [5, 'five'], |
| 55 | + [6, 'six'] |
| 56 | + ]).values() |
| 57 | + ), |
| 58 | + surface |
| 59 | + ) |
| 60 | + expect(surface.innerHTML).to.equal('<div>fourfivesix</div>') |
| 61 | + }) |
| 62 | + |
| 63 | + it('supports iterables of strings in attributes', () => { |
| 64 | + const main = (list: Iterable<string>) => html`<div class="${list}"></div>` |
| 65 | + render(main(['one', 'two', 'three']), surface) |
| 66 | + expect(surface.innerHTML).to.equal('<div class="one two three"></div>') |
| 67 | + render(main(new Set(['four', 'five', 'six'])), surface) |
| 68 | + expect(surface.innerHTML).to.equal('<div class="four five six"></div>') |
| 69 | + }) |
| 70 | + |
| 71 | + it('supports nested iterables of document fragments', () => { |
| 72 | + // prettier-ignore |
| 73 | + const main = (list: DocumentFragment[]) => html`<ul>${list}</ul>` |
| 74 | + render( |
| 75 | + main( |
| 76 | + ['One', 'Two'].map(text => { |
| 77 | + const f = document.createDocumentFragment() |
| 78 | + const li = document.createElement('li') |
| 79 | + li.textContent = text |
| 80 | + f.append(li) |
| 81 | + return f |
| 82 | + }) |
| 83 | + ), |
| 84 | + surface |
| 85 | + ) |
| 86 | + expect(surface.innerHTML).to.equal('<ul><li>One</li><li>Two</li></ul>') |
| 87 | + }) |
| 88 | + |
| 89 | + it('supports nested iterables of templates', () => { |
| 90 | + const child = (item: Record<string, unknown>) => html`<li>${item.name}</li>` |
| 91 | + // prettier-ignore |
| 92 | + const main = (list: Array<Record<string, unknown>>) => html`<ul>${list.map(child)}</ul>` |
| 93 | + render(main([{name: 'One'}, {name: 'Two'}, {name: 'Three'}]), surface) |
| 94 | + expect(surface.innerHTML).to.equal('<ul><li>One</li><li>Two</li><li>Three</li></ul>') |
| 95 | + render(main([{name: 'Two'}, {name: 'Three'}, {name: 'Four'}]), surface) |
| 96 | + expect(surface.innerHTML).to.equal('<ul><li>Two</li><li>Three</li><li>Four</li></ul>') |
| 97 | + }) |
| 98 | +}) |
0 commit comments