aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--birdie_snapshots/can_compute_a_diff_from_one_render_to_the_next.accepted5
-rw-r--r--birdie_snapshots/can_render_an_application's_initial_state.accepted5
-rw-r--r--birdie_snapshots/can_render_an_application's_state_after_some_updates.accepted5
-rw-r--r--birdie_snapshots/can_render_static_html.accepted5
-rw-r--r--test/apps/counter.gleam41
-rw-r--r--test/apps/static.gleam17
-rw-r--r--test/lustre_test.gleam100
7 files changed, 178 insertions, 0 deletions
diff --git a/birdie_snapshots/can_compute_a_diff_from_one_render_to_the_next.accepted b/birdie_snapshots/can_compute_a_diff_from_one_render_to_the_next.accepted
new file mode 100644
index 0000000..5a7dad0
--- /dev/null
+++ b/birdie_snapshots/can_compute_a_diff_from_one_render_to_the_next.accepted
@@ -0,0 +1,5 @@
+---
+version: 1.0.1
+title: Can compute a diff from one render to the next
+---
+[[["000",{"content":"3"}]],[],[]] \ No newline at end of file
diff --git a/birdie_snapshots/can_render_an_application's_initial_state.accepted b/birdie_snapshots/can_render_an_application's_initial_state.accepted
new file mode 100644
index 0000000..4545fcb
--- /dev/null
+++ b/birdie_snapshots/can_render_an_application's_initial_state.accepted
@@ -0,0 +1,5 @@
+---
+version: 1.0.1
+title: Can render an application's initial state.
+---
+<div><p>0</p><button>-</button><button>+</button></div> \ No newline at end of file
diff --git a/birdie_snapshots/can_render_an_application's_state_after_some_updates.accepted b/birdie_snapshots/can_render_an_application's_state_after_some_updates.accepted
new file mode 100644
index 0000000..0922d44
--- /dev/null
+++ b/birdie_snapshots/can_render_an_application's_state_after_some_updates.accepted
@@ -0,0 +1,5 @@
+---
+version: 1.0.1
+title: Can render an application's state after some updates.
+---
+<div><p>3</p><button>-</button><button>+</button></div> \ No newline at end of file
diff --git a/birdie_snapshots/can_render_static_html.accepted b/birdie_snapshots/can_render_static_html.accepted
new file mode 100644
index 0000000..5fe52b6
--- /dev/null
+++ b/birdie_snapshots/can_render_static_html.accepted
@@ -0,0 +1,5 @@
+---
+version: 1.0.1
+title: Can render static HTML
+---
+<html><head><title>Hello, World!</title></head><body><h1>Hello, World!</h1><img src="https://source.unsplash.com/random"></body></html> \ No newline at end of file
diff --git a/test/apps/counter.gleam b/test/apps/counter.gleam
new file mode 100644
index 0000000..b58b2ee
--- /dev/null
+++ b/test/apps/counter.gleam
@@ -0,0 +1,41 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import gleam/int
+import lustre/element.{text}
+import lustre/element/html.{button, div, p}
+import lustre/event
+
+// MODEL -----------------------------------------------------------------------
+
+pub type Model =
+ Int
+
+pub fn init(count) {
+ count
+}
+
+// UPDATE ----------------------------------------------------------------------
+
+pub type Msg {
+ Increment
+ Decrement
+}
+
+pub fn update(model, msg) {
+ case msg {
+ Increment -> model + 1
+ Decrement -> model - 1
+ }
+}
+
+// VIEW ------------------------------------------------------------------------
+
+pub fn view(model) {
+ let count = int.to_string(model)
+
+ div([], [
+ p([], [text(count)]),
+ button([event.on_click(Decrement)], [text("-")]),
+ button([event.on_click(Increment)], [text("+")]),
+ ])
+}
diff --git a/test/apps/static.gleam b/test/apps/static.gleam
new file mode 100644
index 0000000..d41964a
--- /dev/null
+++ b/test/apps/static.gleam
@@ -0,0 +1,17 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import lustre/attribute.{src}
+import lustre/element.{text}
+import lustre/element/html.{body, h1, head, html, img, title}
+
+// VIEW ------------------------------------------------------------------------
+
+pub fn view() {
+ html([], [
+ head([], [title([], "Hello, World!")]),
+ body([], [
+ h1([], [text("Hello, World!")]),
+ img([src("https://source.unsplash.com/random")]),
+ ]),
+ ])
+}
diff --git a/test/lustre_test.gleam b/test/lustre_test.gleam
new file mode 100644
index 0000000..c90dc8b
--- /dev/null
+++ b/test/lustre_test.gleam
@@ -0,0 +1,100 @@
+// IMPORTS ---------------------------------------------------------------------
+
+import apps/counter
+import apps/static
+import birdie
+import gleam/erlang/process
+import gleam/function
+import gleam/json
+import gleeunit
+import lustre
+import lustre/element
+import lustre/internals/patch
+import lustre/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)
+}
+
+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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(Debug),
+ 100,
+ )
+
+ birdie.snap(element.to_string(el), title)
+ process.send(runtime, Shutdown)
+}
+
+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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(Debug),
+ 100,
+ )
+
+ birdie.snap(element.to_string(el), title)
+ process.send(runtime, Shutdown)
+}
+
+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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(Debug),
+ 100,
+ )
+
+ let diff = patch.elements(prev, next)
+
+ birdie.snap(json.to_string(patch.element_diff_to_json(diff)), title)
+ process.send(runtime, Shutdown)
+}