-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_helpers.gleam
102 lines (90 loc) · 2.49 KB
/
test_helpers.gleam
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
import gleam/io
import gleam/list
import gleam/option.{None, Some}
import sprocket/runtime.{type Runtime}
import sprocket/context.{Updater}
import sprocket/internal/reconcile.{
type ReconciledElement, ReconciledAttribute, ReconciledElement,
ReconciledEventHandler,
}
import sprocket/internal/reconcilers/recursive
import sprocket/internal/render.{renderer}
import sprocket/internal/renderers/html.{html_renderer}
pub fn live(view) {
let assert Ok(spkt) = runtime.start(view, Updater(fn(_) { Ok(Nil) }), None)
spkt
}
pub fn render_html(spkt) {
use render_html <- renderer(html_renderer())
let html =
runtime.reconcile_immediate(spkt)
|> render_html()
#(spkt, html)
}
pub type Event {
ClickEvent
}
pub fn render_event(spkt: Runtime, event: Event, html_id: String) {
case runtime.get_reconciled(spkt) {
Some(reconciled) -> {
let found =
recursive.find(reconciled, fn(el: ReconciledElement) {
case el {
ReconciledElement(_tag, _key, attrs, _children) -> {
// try and find id attr that matches the given id
let matching_id_attr =
attrs
|> list.find(fn(attr) {
case attr {
ReconciledAttribute("id", id) if id == html_id -> True
_ -> False
}
})
case matching_id_attr {
Ok(_) -> True
_ -> False
}
}
_ -> False
}
})
case found {
Ok(ReconciledElement(_tag, _key, attrs, _children)) -> {
let event_kind = case event {
ClickEvent -> "click"
}
// find click event handler id
let rendered_event_handler =
attrs
|> list.find(fn(attr) {
case attr {
ReconciledEventHandler(kind, _id) if kind == event_kind -> True
_ -> False
}
})
case rendered_event_handler {
Ok(ReconciledEventHandler(_kind, event_id)) -> {
case runtime.process_event_immediate(spkt, event_id, None) {
Ok(_) -> spkt
_ -> panic
}
}
_ -> {
io.debug("no event handler")
panic
}
}
}
_ -> {
io.debug("no match")
panic
}
}
}
None -> {
io.debug("no reconciled")
panic
}
}
spkt
}