-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.js
111 lines (90 loc) · 1.94 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import test from 'ava'
import { picoapp, component } from './dist/picoapp.js'
const createNode = attr => ({
dataset: {
props: '{"hello": "World"}'
},
getAttribute () {
return attr
},
removeAttribute () {}
})
global.document = {
querySelectorAll () {
return [
createNode('foo'),
createNode('bar')
]
},
}
test('init', t => {
const app = picoapp({ foo: 'foo' }, { bar: true })
t.truthy(app.on)
t.truthy(app.emit)
t.truthy(app.getState)
t.truthy(app.add)
t.truthy(app.hydrate)
t.truthy(app.hydrate({}))
t.truthy(app.mount)
t.truthy(app.unmount)
t.is(app.getState().bar, true)
})
test('events', t => {
const app = picoapp({ foo: 'foo' }, { bar: true })
t.plan(5)
app.on('a', () => t.truthy(1))
app.on(['a', 'b'], () => t.truthy(1))
app.on('*', () => t.truthy(1))
app.emit('a')
app.emit('b')
})
test('mount', t => {
t.plan(1)
const app = picoapp({
foo: component((node, ctx) => {
const u = ctx.on('foo', () => {
t.pass()
u() // unsub itself
})
})
}, { bar: true })
app.mount()
app.emit('foo')
app.unmount()
app.emit('foo')
})
test('unmount', t => {
t.plan(4)
const app = picoapp({
foo: component((node, ctx) => {
ctx.on('foo', () => t.truthy(1))
return () => t.truthy(1)
}),
bar: component((node, ctx) => {
ctx.on('foo', () => t.truthy(1))
})
})
app.mount()
app.emit('foo')
app.unmount()
app.emit('foo')
})
test('plugins', t => {
t.plan(2)
function testContext(ctx) {
const internals = ['getState', 'hydrate', 'on', 'emit']
const preserved = internals.every(key => typeof ctx[key] === 'function')
t.true(preserved)
}
const app = picoapp({
foo: component((node, ctx) => {
t.true(ctx.props.hello === 'World')
testContext(ctx);
})
})
app.use((node, ctx) => ({
getState: undefined,
props: JSON.parse(node.dataset.props || '{}')
}))
app.mount()
})