Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ test/kobalte/ each Kobalte component in OCaml beside its TSX twin in
test/headless/ each headless component as a caller meets it: its roles,
keys and state, and an axe scan; each of Kobalte's
fixed, failing on Kobalte's own
test/rhp/ rhp's charts as a caller meets them: rows plotted by
their slat, a scale's range and ticks, a changed row
redrawn in place on the next frame
test/heroui/ each HeroUI port's classes and behaviour, in OCaml, run
by vitest, and its state beside the same component in
React, its twin in twins/; and the variant functions,
Expand Down
81 changes: 81 additions & 0 deletions test/rhp/chart_test.mlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(* rhp's chart through the binding, as a caller meets it: each row plotted
by its slat, the chart scaled to its range with a tick at each step, and a
row whose field changes redrawn in place, the others untouched. *)

module Bar = Solidtype_rhp.Bar
module Chart = Solidtype_rhp.Chart
module Jsx = Solidtype.Jsx
module Label = Solidtype_rhp.Label
module Plot = Solidtype_rhp.Plot
module Row = Solidtype_rhp.Row
module Scale = Solidtype_rhp.Scale
module Slat = Solidtype_rhp.Slat
module Ticks = Solidtype_rhp.Ticks
module S = Solidtype.Solid
module E = Jest.Expect
module H = Harness
open Harness.Syntax

type fruit = { name : string; sold : float }

let fruit =
Slat.make ~thickness:(Slat.number 32.) (fun d ->
let f = Row.data d in
<div>
<Label edge="start">(Jsx.string f.name)</Label> <Bar to_=f.sold />
</div>)

let tick =
Slat.make (fun (d : Scale.tick Row.t) ->
let t = Row.data d in
<div><Label at=t.at>(Jsx.string (Js.Float.toString t.at))</Label></div>)

let fruits =
[| { name = "Apples"; sold = 5. }; { name = "Bananas"; sold = 15. } |]

let chart rows =
H.render (fun () ->
<Chart scale=(0., 20.) label="Sold">
<Scale ticks=(Ticks.custom (Solidtype_rhp.every 10. ())) render=tick />
<Plot rows=(S.get rows) key=(fun d -> (Row.data d).name) render=fruit />
</Chart>)

let bars () = H.query_all "[role=list] .rhp-bar"

let () =
Jest.describe "rhp's chart" (fun () ->
Jest.afterEach H.cleanup;
Jest.test "plots each row by its slat, a bar to its value" (fun () ->
let rows, _ = S.create_signal fruits in
chart rows;
E.(
expect
( Array.map H.text (H.query_all "[role=listitem] .rhp-label"),
Array.map (H.custom "--rhp-to") (bars ()) )
|> toEqual ([| "Apples"; "Bananas" |], [| "5"; "15" |])));
Jest.test "scales the chart to its range, a tick at each step" (fun () ->
let rows, _ = S.create_signal fruits in
chart rows;
let ticks = H.query_all ".rhp-scale .rhp-label" in
let range = H.query ".rhp-chart" in
E.(
expect
( Array.map H.text ticks,
Array.map (H.custom "--rhp-at") ticks,
(H.custom "--rhp-min" range, H.custom "--rhp-max" range) )
|> toEqual
([| "0"; "10"; "20" |], [| "0"; "10"; "20" |], ("0", "20"))));
Jest.testPromise
"redraws a row whose field changes, in place, the others not" (fun () ->
let rows, set_rows = S.create_signal fruits in
chart rows;
let before = bars () in
S.set set_rows [| fruits.(0); { (fruits.(1)) with sold = 18. } |];
let* () = H.frame () in
let after = bars () in
Js.Promise.resolve
E.(
expect
( Array.for_all2 ( == ) before after,
Array.map (H.custom "--rhp-to") after )
|> toEqual (true, [| "5"; "18" |]))))
16 changes: 16 additions & 0 deletions test/rhp/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; rhp's charts through the binding, as a caller meets them: rows plotted,
; a scale's ticks drawn, and a row that changes redrawn alone.

(library
(name rhp_test)
(modes melange)
(libraries solidtype solidtype-rhp melange-jest.jest melange.dom)
(preprocess
(pps melange.ppx ppx_solidtype)))

(melange.emit
(target out)
(modules)
(libraries rhp_test)
(module_systems
(es6 jsx)))
38 changes: 38 additions & 0 deletions test/rhp/harness.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(* What the tests of rhp's charts reach: Solid's Testing Library, the page,
and a frame, which rhp draws a changed row on. *)

module Syntax = struct
let ( let* ) p f = Js.Promise.then_ f p
end

external promise : ((('a -> unit) -> unit)[@mel.uncurry]) -> 'a Js.Promise.t
= "Promise"
[@@mel.new]

external request_frame : ((float -> unit)[@mel.uncurry]) -> int
= "requestAnimationFrame"

let frame () =
promise (fun resolve -> ignore (request_frame (fun _ -> resolve ()) : int))

external render : (unit -> Solidtype.Jsx.element) -> unit = "render"
[@@mel.module "@solidjs/testing-library"]

external cleanup : unit -> unit = "cleanup"
[@@mel.module "@solidjs/testing-library"]

external query : string -> Dom.element = "querySelector"
[@@mel.scope "document"]

external query_all : string -> Dom.element array = "querySelectorAll"
[@@mel.scope "document"]

external text : Dom.element -> string = "textContent" [@@mel.get]
external style : Dom.element -> Dom.cssStyleDeclaration = "style" [@@mel.get]

external property : Dom.cssStyleDeclaration -> string -> string
= "getPropertyValue"
[@@mel.send]

(* An element's custom property, the value rhp places it by. *)
let custom name e = property (style e) name
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default defineConfig({
"_build/default/test/kobalte/out/test/kobalte/*_test.jsx",
"_build/default/test/headless/out/test/headless/*_test.jsx",
"_build/default/test/heroui/out/test/heroui/*_test.jsx",
"_build/default/test/rhp/out/test/rhp/*_test.jsx",
],
},
},
Expand Down
Loading