| 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226 |
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
42x
4x
1x
1x
1x
1x
6x
6x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
4x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x | /* eslint-disable fp/no-mutating-methods */
const test = require('tape')
const manuel = require('../')
const m = require('mithril')
const mithrilQuery = require('mithril-query')
const UNUSED_KEY_CODE = 1
const KEY_UP = 38
const KEY_DOWN = 40
const {
compose
, append
, prepend
, split
, join
} = require('ramda')
const render =
require('mithril-node-render')
const renderStdout =
compose(
console.log
, join('\n')
, prepend('--HTML-BEGIN--')
, append('--HTML-END--')
, split('\n')
, render
)
function query(app){
const context =
mithrilQuery(app)
const {
first: $
, find: $$
} = context
return { context, $, $$ }
}
function Model(){
return {
model: {
list: [[]]
,input: ['']
,chosen: [null]
,open: [false]
,highlighted: [null]
}
,modelKeys: {
list: 'list'
,input: 'input'
,chosen: 'chosen'
,open: 'open'
,highlighted: 'highlighted'
}
}
}
function mithrilAutocomplete({model, modelKeys}){
return {
autocomplete:
manuel({
hyperscript: m
,get: k => model[k][0]
,set: (k,v) => model[k].unshift(v)
})
,model
,modelKeys
}
}
test('Mithril v0.2.x', function(t){
const { model, modelKeys, autocomplete } =
mithrilAutocomplete( Model() )
const App = {
controller: function Controller(){
return function view(){
return autocomplete(modelKeys)
}
}
,view: f => f()
}
const { $, $$, context } = query(App)
t.equals(
render(
m('.manuel-complete'
,m('input', { value: "" })
,m('ul')
)
)
, render(context.rootEl)
, 'Basic structure renders'
)
model.input.unshift('Che'); context.redraw()
t.equals(
render(
m('.manuel-complete.not-empty'
,m('input', { value: model.input[0]})
,m('ul')
)
)
, render(context.rootEl)
, 'Autocomplete updates to reflect model, not-empty classes added'
)
$('input').attrs.oninput({
currentTarget: { value: 'Cher' }
})
context.redraw()
t.equals(
model.input[0]
, 'Cher'
, 'Input event updated model'
)
t.equals(
context.rootEl.attrs.className
,'manuel-complete not-empty'
,`Typing with an empty list did not show the drawer
but did add not-empty class to input
`
)
t.equals(
model.open[0]
, true
,'Typing set open to true, even though the list is empty'
)
model.list.unshift([
'Cher'
,'Cherry'
,'Cherries'
,'Chereth Cutestory'
])
context.redraw()
t.equals(
render(
m('.manuel-complete.open.not-empty.loaded'
,m('input', { value: model.input[0]})
,m('ul', model.list[0].map(
x => m(
'li', { class: ""}, [
m('mark', 'Cher')
, x.slice(4)
]
)
) )
)
)
, render(context.rootEl)
, `After adding items to the list:
open and loaded are both added to the root classnames
and matching text is highlighted
`
)
context.redraw()
const prevented = []
context.rootEl.attrs.onkeydown({
keyCode: UNUSED_KEY_CODE
,preventDefault(){
prevented.unshift(UNUSED_KEY_CODE)
}
})
t.equals(
prevented.length
, 0
, 'Pressing an unsed key does not e.preventDefault('
)
context.rootEl.attrs.onkeydown({
keyCode: KEY_DOWN
,preventDefault(){
prevented.unshift(KEY_DOWN)
}
})
t.deepEquals(
prevented
,[KEY_DOWN]
,'Pressing a used key (KEY_DOWN) did prevent default'
)
t.equals(
model.list[0][0]
,model.highlighted[0]
,'First item on a KEY DOWN led to the first item being highlighted'
)
context.redraw()
t.equals(
render($('.highlight'))
,render(
m('li.highlight'
,m('mark', model.highlighted[0] )
)
)
,'Highlight class added to element'
)
t.end()
})
process.on('unhandledRejection', r => console.error(r)); |