aboutsummaryrefslogtreecommitdiff
path: root/test/lustre_test.gleam
blob: bae6c0fae9f6bc82abaeaeb4be2af12e7e49d6ae (plain)
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
// 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/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)
}

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)
}