aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorJacob Scearcy <jacobscearcy@gmail.com>2024-04-19 19:46:34 +1000
committerGitHub <noreply@github.com>2024-04-19 10:46:34 +0100
commit48b04d9dc06f3f6190c8aafcb85ca12737634234 (patch)
tree63484354a21b3207b43c830730a46467ea64040c /test
parent825c52c431b3768e563b2b595f3fb703e37ebdf4 (diff)
downloadlustre-48b04d9dc06f3f6190c8aafcb85ca12737634234.tar.gz
lustre-48b04d9dc06f3f6190c8aafcb85ca12737634234.zip
🔀 Add support for element fragments. (#99)
* #16 add fragment POC * WIP * 16 add fragment support to morph * add keys for fragments * ensure proper fragment order, refactor to handle fragment/element more similarly * fix comment typo, incorrect child typo, simplify key check * fix comment typo * add snapshot tests * flatten fragment using fold right, appending elements * doc update --------- Co-authored-by: Hayleigh Thompson <me@hayleigh.dev>
Diffstat (limited to 'test')
-rw-r--r--test/apps/fragment.gleam45
-rw-r--r--test/lustre_test.gleam51
2 files changed, 96 insertions, 0 deletions
diff --git a/test/apps/fragment.gleam b/test/apps/fragment.gleam
new file mode 100644
index 0000000..d20400e
--- /dev/null
+++ b/test/apps/fragment.gleam
@@ -0,0 +1,45 @@
+// Similar to count app, with fragments and edge cases
+
+// IMPORTS ---------------------------------------------------------------------
+
+import gleam/int
+import lustre/element.{text}
+import lustre/element/html.{button, 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)
+ element.fragment([
+ element.fragment([p([], [element.text("start fragment")])]),
+ element.fragment([p([], [element.text("middle fragment")])]),
+ element.fragment([p([], [element.text(count)])]),
+ button([event.on_click(Decrement)], [text("-")]),
+ button([event.on_click(Increment)], [text("+")]),
+ p([], [element.text("order check, last element")]),
+ ])
+}
diff --git a/test/lustre_test.gleam b/test/lustre_test.gleam
index 9e47a01..f3a2993 100644
--- a/test/lustre_test.gleam
+++ b/test/lustre_test.gleam
@@ -1,6 +1,7 @@
// IMPORTS ---------------------------------------------------------------------
import apps/counter
+import apps/fragment
import apps/static
import birdie
import gleam/erlang/process
@@ -98,3 +99,53 @@ pub fn counter_diff_test() {
birdie.snap(json.to_string(patch.element_diff_to_json(diff)), title)
process.send(runtime, Shutdown)
}
+
+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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(Debug),
+ 100,
+ )
+
+ birdie.snap(element.to_string(el), title)
+ process.send(runtime, Shutdown)
+}
+
+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,
+ function.curry2(process.send)
+ |> function.compose(View)
+ |> function.compose(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,
+ 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)
+}