Skip to content
This repository was archived by the owner on Jan 9, 2025. It is now read-only.

Commit 7cf88ae

Browse files
committed
upgrade tests to use TypeScript
1 parent 1a2bff2 commit 7cf88ae

File tree

8 files changed

+236
-230
lines changed

8 files changed

+236
-230
lines changed

test/directives.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {expect} from 'chai'
2+
import {html, render, directive} from '../lib/index.js'
3+
4+
describe('directives', () => {
5+
let surface: HTMLElement
6+
beforeEach(() => {
7+
surface = document.createElement('section')
8+
})
9+
10+
it('handles directives differently', () => {
11+
const setAsFoo = directive(() => part => {
12+
part.value = 'foo'
13+
})
14+
const main = () => html`<div class="${setAsFoo()}"></div>`
15+
render(main(), surface)
16+
expect(surface.innerHTML).to.equal('<div class="foo"></div>')
17+
})
18+
})

test/events.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {expect} from 'chai'
2+
import {html, render} from '../lib/index.js'
3+
4+
describe('events', () => {
5+
let surface: HTMLElement
6+
beforeEach(() => {
7+
surface = document.createElement('section')
8+
})
9+
10+
describe('event listeners', () => {
11+
it('handles event listeners properly', () => {
12+
let i = 0
13+
const main = () => html`<div onclick="${() => (i += 1)}"></div>`
14+
render(main(), surface)
15+
expect(surface.innerHTML).to.equal('<div></div>')
16+
expect(i).to.equal(0)
17+
surface.querySelector('div')?.click()
18+
expect(i).to.equal(1)
19+
surface.querySelector('div')?.dispatchEvent(new CustomEvent('click'))
20+
expect(i).to.equal(2)
21+
})
22+
23+
it('does not rebind event listeners multiple times', () => {
24+
let i = 0
25+
const main = () => html`<div onclick="${() => (i += 1)}"></div>`
26+
render(main(), surface)
27+
render(main(), surface)
28+
render(main(), surface)
29+
expect(surface.innerHTML).to.equal('<div></div>')
30+
expect(i).to.equal(0)
31+
surface.querySelector('div')?.click()
32+
expect(i).to.equal(1)
33+
surface.querySelector('div')?.dispatchEvent(new CustomEvent('click'))
34+
expect(i).to.equal(2)
35+
})
36+
37+
it('allows events to be driven by params', () => {
38+
let i = 0
39+
const main = (amt: number) => html`<div onclick="${() => (i += amt)}"></div>`
40+
render(main(1), surface)
41+
expect(surface.innerHTML).to.equal('<div></div>')
42+
expect(i).to.equal(0)
43+
surface.querySelector('div')?.click()
44+
expect(i).to.equal(1)
45+
render(main(4), surface)
46+
surface.querySelector('div')?.dispatchEvent(new CustomEvent('click'))
47+
expect(i).to.equal(5)
48+
})
49+
50+
it('will unbind event listeners by passing null', () => {
51+
let i = 0
52+
const main = (listener: Function | null) => html`<div onclick="${listener}"></div>`
53+
render(
54+
main(() => (i += 1)),
55+
surface
56+
)
57+
expect(surface.innerHTML).to.equal('<div></div>')
58+
expect(i).to.equal(0)
59+
surface.querySelector('div')?.click()
60+
expect(i).to.equal(1)
61+
render(main(null), surface)
62+
surface.querySelector('div')?.click()
63+
surface.querySelector('div')?.click()
64+
surface.querySelector('div')?.click()
65+
expect(i).to.equal(1)
66+
})
67+
68+
it('binds event handler objects', () => {
69+
const handler = {
70+
i: 0,
71+
handleEvent() {
72+
this.i += 1
73+
}
74+
}
75+
const main = () => html`<div onclick="${handler}"></div>`
76+
render(main(), surface)
77+
expect(surface.innerHTML).to.equal('<div></div>')
78+
expect(handler.i).to.equal(0)
79+
surface.querySelector('div')?.click()
80+
expect(handler.i).to.equal(1)
81+
surface.querySelector('div')?.dispatchEvent(new CustomEvent('click'))
82+
expect(handler.i).to.equal(2)
83+
})
84+
})
85+
})

test/html.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {expect} from 'chai'
12
import {html} from '../lib/index.js'
23

34
describe('html', () => {

test/iterables.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
})

test/karma.config.cjs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const resolve = require('@rollup/plugin-node-resolve').default
2-
31
process.env.CHROME_BIN = require('chromium').path
42

53
module.exports = function (config) {
@@ -9,18 +7,13 @@ module.exports = function (config) {
97
files: [
108
{pattern: 'lib/*.js', type: 'module', included: false},
119
{pattern: 'node_modules/**', type: 'module', included: false},
12-
{pattern: 'test/*', type: 'module', included: true, watched: false}
10+
{pattern: 'test/*.ts', type: 'module', included: true, watched: false}
1311
],
1412
preprocessors: {
15-
'test/*.ts': ['rollup']
13+
'test/*.ts': ['esbuild']
1614
},
17-
rollupPreprocessor: {
18-
plugins: [resolve()],
19-
output: {
20-
format: 'iife',
21-
name: 'test',
22-
sourcemap: 'inline'
23-
}
15+
esbuild: {
16+
target: 'es2019'
2417
},
2518
reporters: ['mocha'],
2619
port: 9876,

0 commit comments

Comments
 (0)