Skip to content

Commit

Permalink
add some reducer test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
eliknebel committed Nov 11, 2024
1 parent 8f8d213 commit 679099d
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion test/sprocket/internal/reducer_test.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sprocket/internal/reducer
import sprocket/test_helpers

type Model =
Int
Expand All @@ -21,7 +22,7 @@ fn update(model, msg) -> #(Model, List(reducer.Cmd(Msg))) {
}
}

pub fn reducer_should_dispatch() {
pub fn reducer_should_dispatch_test() {
let assert Ok(reducer_actor) = reducer.start(init(0), update, fn(_) { Nil })

reducer.dispatch(reducer_actor, Increment)
Expand All @@ -41,4 +42,84 @@ pub fn reducer_should_dispatch() {
let state = reducer.get_state(reducer_actor)

let assert True = state == 0

reducer.shutdown(reducer_actor)
}

fn init_with_commands(value, cmds) -> #(Model, List(reducer.Cmd(Msg))) {
#(value, cmds)
}

fn increment_cmd() -> reducer.Cmd(Msg) {
fn(dispatch) { dispatch(Increment) }
}

pub fn reducer_should_process_initial_cmd_test() {
let assert Ok(reducer_actor) =
reducer.start(init_with_commands(0, [increment_cmd()]), update, fn(_) {
Nil
})

let state = reducer.get_state(reducer_actor)

let assert True = state == 0

reducer.process_commands(reducer_actor)

test_helpers.wait_until(
fn() {
let state = reducer.get_state(reducer_actor)

state == 1
},
1000,
)

let state = reducer.get_state(reducer_actor)

let assert True = state == 1

reducer.dispatch(reducer_actor, Reset)

let state = reducer.get_state(reducer_actor)

let assert True = state == 0

reducer.shutdown(reducer_actor)
}

pub fn reducer_should_process_multiple_cmds_test() {
let assert Ok(reducer_actor) =
reducer.start(
init_with_commands(0, [increment_cmd(), increment_cmd(), increment_cmd()]),
update,
fn(_) { Nil },
)

let state = reducer.get_state(reducer_actor)

let assert True = state == 0

reducer.process_commands(reducer_actor)

test_helpers.wait_until(
fn() {
let state = reducer.get_state(reducer_actor)

state == 3
},
1000,
)

let state = reducer.get_state(reducer_actor)

let assert True = state == 3

reducer.dispatch(reducer_actor, Reset)

let state = reducer.get_state(reducer_actor)

let assert True = state == 0

reducer.shutdown(reducer_actor)
}

0 comments on commit 679099d

Please sign in to comment.