From 679099d86b05f2849239199e23e3454a43af6e23 Mon Sep 17 00:00:00 2001 From: Eli Knebel Date: Mon, 11 Nov 2024 18:55:28 -0500 Subject: [PATCH] add some reducer test coverage --- test/sprocket/internal/reducer_test.gleam | 83 ++++++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/test/sprocket/internal/reducer_test.gleam b/test/sprocket/internal/reducer_test.gleam index bb44a02..cd5f693 100644 --- a/test/sprocket/internal/reducer_test.gleam +++ b/test/sprocket/internal/reducer_test.gleam @@ -1,4 +1,5 @@ import sprocket/internal/reducer +import sprocket/test_helpers type Model = Int @@ -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) @@ -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) }