forked from lustre-labs/lustre
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlustre_test.gleam
175 lines (148 loc) · 4.09 KB
/
lustre_test.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
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
// IMPORTS ---------------------------------------------------------------------
import apps/counter
import apps/fragment
import apps/static
import birdie
import gleam/erlang/process
import gleam/json
import gleeunit
import lustre
import lustre/element
import lustre/internals/patch
import lustre/internals/runtime.{Debug, Dispatch, Shutdown, View}
// MAIN ------------------------------------------------------------------------
pub fn main() {
gleeunit.main()
}
// TESTS -----------------------------------------------------------------------
pub fn static_test() {
let title = "Can render static HTML"
let el = static.view()
birdie.snap(element.to_string(el), title)
}
@target(erlang)
pub fn counter_init_test() {
let title = "Can render an application's initial state."
let app = lustre.simple(counter.init, counter.update, counter.view)
let assert Ok(runtime) = lustre.start_actor(app, 0)
let el =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
birdie.snap(element.to_string(el), title)
process.send(runtime, Shutdown)
}
@target(erlang)
pub fn counter_update_test() {
let title = "Can render an application's state after some updates."
let app = lustre.simple(counter.init, counter.update, counter.view)
let assert Ok(runtime) = lustre.start_actor(app, 0)
process.send(runtime, Dispatch(counter.Increment))
process.send(runtime, Dispatch(counter.Increment))
process.send(runtime, Dispatch(counter.Increment))
let el =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
birdie.snap(element.to_string(el), title)
process.send(runtime, Shutdown)
}
@target(erlang)
pub fn counter_diff_test() {
let title = "Can compute a diff from one render to the next"
let app = lustre.simple(counter.init, counter.update, counter.view)
let assert Ok(runtime) = lustre.start_actor(app, 0)
let prev =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
process.send(runtime, Dispatch(counter.Increment))
process.send(runtime, Dispatch(counter.Increment))
process.send(runtime, Dispatch(counter.Increment))
let next =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
let diff = patch.elements(prev, next)
birdie.snap(json.to_string(patch.element_diff_to_json(diff)), title)
process.send(runtime, Shutdown)
}
@target(erlang)
pub fn fragment_init_test() {
let title = "Can render an application's initial state when using fragments"
let app = lustre.simple(fragment.init, fragment.update, fragment.view)
let assert Ok(runtime) = lustre.start_actor(app, 0)
let el =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
birdie.snap(element.to_string(el), title)
process.send(runtime, Shutdown)
}
@target(erlang)
pub fn fragment_counter_diff_test() {
let title = "Can compute a diff from one render to the next with fragments"
let app = lustre.simple(fragment.init, fragment.update, fragment.view)
let assert Ok(runtime) = lustre.start_actor(app, 0)
let prev =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
process.send(runtime, Dispatch(fragment.Increment))
process.send(runtime, Dispatch(fragment.Increment))
process.send(runtime, Dispatch(fragment.Increment))
let next =
process.call(
runtime,
fn(reply) {
process.send(reply, _)
|> View
|> Debug
},
100,
)
let diff = patch.elements(prev, next)
birdie.snap(json.to_string(patch.element_diff_to_json(diff)), title)
process.send(runtime, Shutdown)
}
pub fn escaped_attribute_test() {
let title = "Can safely escape dangerous symbols in attributes"
let el = static.escaped_attribute()
birdie.snap(element.to_string(el), title)
}