Current section
Files
Jump to
Current section
Files
src/novdom_testing/testing.gleam
import novdom/component.{type Component}
import novdom/hotkey
import novdom/internals/parameter.{type Event, type Parameter, Listener}
import novdom/motion
import novdom/render
pub opaque type Callee {
Callee(id: String)
}
pub fn init(test_fn: fn() -> a) -> Nil {
init_js()
test_fn()
Nil
}
pub fn render(component: fn() -> Component) -> Nil {
// TODO: Move to init()??
motion.init()
hotkey.init()
component()
|> render.add_to_viewport("_app_")
// wait(5)
}
/// Create a callee type that can be used to test if a function was called
pub fn create_callee() -> Callee {
create_callee_js()
|> Callee
}
/// Call a callee to later check if it was called (see `callee_count`).
/// Use this function to simulate a function call.
pub fn call_callee(callee: Callee) {
call_callee_js(callee.id)
}
/// Check how often a callee was called
pub fn callee_count(callee: Callee) -> Int {
callee_count_js(callee.id)
}
/// Trigger an event on a component.
///
/// Example to trigger a click event:
/// ```gleam
/// trigger_event(my_component, onclick)
/// ```
///
pub fn trigger_event(
component: Component,
listener: fn(fn(Event) -> Nil) -> Parameter,
) -> Component {
let assert Listener(name, _) = listener(fn(_) { Nil })
trigger_event_js(component, name)
component
}
pub fn trigger_unrender(component: Component) -> Component {
trigger_event_js(component, "unrender")
component
}
pub fn step(component: Component, step: String) -> Component {
set_step(step)
component
}
/// Set a description for a component to be used in error messages
@external(javascript, "../testing_ffi.mjs", "set_description")
pub fn description(component: Component, desc: String) -> Component
@external(javascript, "../testing_ffi.mjs", "current_step")
fn set_step(step: String) -> Nil
@external(javascript, "../testing_ffi.mjs", "trigger_event")
fn trigger_event_js(component: Component, event: String) -> Nil
@external(javascript, "../testing_ffi.mjs", "create_callee")
fn create_callee_js() -> String
@external(javascript, "../testing_ffi.mjs", "call_callee")
fn call_callee_js(id: String) -> Nil
@external(javascript, "../testing_ffi.mjs", "callee_count")
fn callee_count_js(id: String) -> Int
@external(javascript, "../testing_ffi.mjs", "init")
fn init_js() -> Nil