diff --git a/AGENTS.md b/AGENTS.md index 585a2ef..da6cf6e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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, diff --git a/test/rhp/chart_test.mlx b/test/rhp/chart_test.mlx new file mode 100644 index 0000000..ddaf141 --- /dev/null +++ b/test/rhp/chart_test.mlx @@ -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 +
+ +
) + +let tick = + Slat.make (fun (d : Scale.tick Row.t) -> + let t = Row.data d in +
) + +let fruits = + [| { name = "Apples"; sold = 5. }; { name = "Bananas"; sold = 15. } |] + +let chart rows = + H.render (fun () -> + + + (Row.data d).name) render=fruit /> + ) + +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" |])))) diff --git a/test/rhp/dune b/test/rhp/dune new file mode 100644 index 0000000..6854f49 --- /dev/null +++ b/test/rhp/dune @@ -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))) diff --git a/test/rhp/harness.ml b/test/rhp/harness.ml new file mode 100644 index 0000000..39452c0 --- /dev/null +++ b/test/rhp/harness.ml @@ -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 diff --git a/vitest.config.ts b/vitest.config.ts index 53156e9..716e118 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -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", ], }, },