-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
124 lines (122 loc) · 4.58 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
112
113
114
115
116
117
118
119
120
121
122
123
124
const requireEsm = require('esm')(module)
const o = require('ospec')
const mh = require('./dist/microh.es5.min')
const react = require('react')
const preact = require('preact')
const hyperapp = requireEsm('hyperapp')
const fakeH = (tag, attrs, ...children) => ({ tag, attrs, children })
const h = mh(fakeH)
const reactH = mh(react.createElement)
const preactH = mh(preact.h, 'class')
const hyperH = mh((tag, props, ...children) =>
typeof tag === 'function'
? tag(props, children)
: hyperapp.h(
tag,
props,
[]
.concat(...children)
.map(child =>
typeof child === 'string' || typeof child === 'number' ? hyperapp.text(child) : child
)
)
)
o.spec('microh', () => {
o('works', () =>
o(h('a.test', { href: 'test' }, 'test')).deepEquals({
tag: 'a',
attrs: { href: 'test', className: 'test' },
children: ['test']
})
)
o('no attrs', () => o(h('div', 'test')).deepEquals({ tag: 'div', attrs: {}, children: ['test'] }))
o('no attrs, vnode first child', () =>
o(h('ul', h('li'))).deepEquals({
tag: 'ul',
attrs: {},
children: [{ tag: 'li', attrs: {}, children: [] }]
})
)
o('no tagname', () => o(h('')).deepEquals({ tag: 'div', attrs: {}, children: [] }))
o('multiple classes', () =>
o(h('aside.one.two.three four')).deepEquals({
tag: 'aside',
attrs: { className: 'one two three four' },
children: []
})
)
o('combines className from attrs', () =>
o(h('.one two', { className: 'three' }, ['child'])).deepEquals({
tag: 'div',
attrs: { className: 'one two three' },
children: [['child']]
})
)
o('preact works - attrs + single child', () => {
const { type, props } = preactH('div.test', { title: 'test' }, 'test')
o(type).equals('div')
// preact/react single child not wrapped in array
o(props).deepEquals({ title: 'test', class: 'test', children: 'test' })
})
o('preact works - attrs + multi child', () => {
const { type, props } = preactH('div.test', { title: 'test' }, 'test', ' ', 'test2')
o(type).equals('div')
o(props).deepEquals({ title: 'test', class: 'test', children: ['test', ' ', 'test2'] })
})
o('preact works - no attrs + child', () => {
const { type, props } = preactH('div.test', 'test')
o(type).equals('div')
o(props).deepEquals({ class: 'test', children: 'test' })
})
o('react works - attrs + single child', () => {
const { type, props } = reactH('div.test', { title: 'test' }, 'test')
o(type).equals('div')
o(props).deepEquals({ title: 'test', className: 'test', children: 'test' })
})
o('react works - attrs + multi child', () => {
const { type, props } = reactH('div.test', { title: 'test' }, 'test', ' ', 'test2')
o(type).equals('div')
o(props).deepEquals({ title: 'test', className: 'test', children: ['test', ' ', 'test2'] })
})
o('react works - no attrs + child', () => {
const { type, props } = reactH('div.test', 'test')
o(type).equals('div')
o(props).deepEquals({ className: 'test', children: 'test' })
})
o('hyperapp works - attrs + single child', () => {
const { type, props, children } = hyperH('div.test', { title: 'test' }, 'test')
o(type).equals('div')
o(props).deepEquals({ title: 'test', className: 'test' })
o(children[0].type).equals('test')
})
o('hyperapp works - attrs + multi child', () => {
const {
type,
props,
children: [one, two, three] // hyperapp converts text into vnodes with type == 3
} = hyperH('div.test', { title: 'test' }, 'test', ' ', 'test2')
o(type).equals('div')
o(props).deepEquals({ title: 'test', className: 'test' })
o([one.type, two.type, three.type]).deepEquals(['test', ' ', 'test2'])
})
o('hyperapp works - no attrs + child', () => {
const { type, props, children } = hyperH('div.test', 'test')
o(type).equals('div')
o(props).deepEquals({ className: 'test' })
o(children[0].type).equals('test')
})
o('can pass components as tag', () => {
o(typeof h({}).tag).equals('object')
o(typeof h(class Cmp {}).tag).equals('function')
o(typeof h(() => {}).tag).equals('function')
})
o('can use either tag or attr key in props but not both', () => {
o(reactH('input', { type: 'number' }).props).deepEquals({ type: 'number' })
o(preactH('input', { nodeName: 'div' }).props).deepEquals({ nodeName: 'div' })
o(hyperH('input', { attributes: 'hello' }).props).deepEquals({ attributes: 'hello' })
o(reactH('input', { type: 'number', props: 'some' }).props.children).deepEquals({
type: 'number',
props: 'some'
})
})
})