diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-01-27 17:19:31 +0000 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-01-27 17:19:31 +0000 |
commit | afb57d6dbdeb1628d420411a41fdbbd479b6732f (patch) | |
tree | 7d057f44d5c9d5ffe6376542e102446cc4760fa8 /test | |
parent | 5510116297d5da07269b776a0e30766e221569bd (diff) | |
download | lustre-afb57d6dbdeb1628d420411a41fdbbd479b6732f.tar.gz lustre-afb57d6dbdeb1628d420411a41fdbbd479b6732f.zip |
:alembic: Set up some basic snapshot tests.
Diffstat (limited to 'test')
-rw-r--r-- | test/apps/counter.gleam | 41 | ||||
-rw-r--r-- | test/apps/static.gleam | 17 | ||||
-rw-r--r-- | test/lustre_test.gleam | 100 |
3 files changed, 158 insertions, 0 deletions
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) +} |