config-v2.test.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /** @jsxImportSource @opentui/solid */
  2. import { testRender } from "@opentui/solid"
  3. import { expect, test } from "bun:test"
  4. import {
  5. resolve,
  6. ConfigProvider,
  7. useConfig,
  8. type Interface,
  9. } from "../src/config"
  10. test("resolves nested config and keybind defaults", () => {
  11. const config = resolve(
  12. {
  13. keybinds: { leader: "ctrl+o" },
  14. leader: { timeout: 500 },
  15. scroll: { speed: 2, acceleration: true },
  16. diffs: { view: "split" },
  17. debug: { devtools: true },
  18. },
  19. { terminalSuspend: true },
  20. )
  21. expect(config.leader.timeout).toBe(500)
  22. expect(config.keybinds.get("leader")?.[0]?.key).toBe("ctrl+o")
  23. expect(config.scroll).toEqual({ speed: 2, acceleration: true })
  24. expect(config.diffs).toEqual({ view: "split" })
  25. expect(config.debug).toEqual({ devtools: true })
  26. })
  27. test("provides config and its host interface", async () => {
  28. const config = resolve({}, { terminalSuspend: true })
  29. let current = {}
  30. const service: Interface = {
  31. get: async () => current,
  32. update: async (update) => {
  33. const draft: Record<string, any> = { ...current }
  34. update(draft)
  35. current = draft
  36. return draft
  37. },
  38. }
  39. let context: ReturnType<typeof useConfig> | undefined
  40. function Consumer() {
  41. context = useConfig()
  42. return <text>{`${context.data.mouse ? "mouse" : "none"} ${context.data.keybinds.get("leader")?.[0]?.key}`}</text>
  43. }
  44. const app = await testRender(() => (
  45. <ConfigProvider config={config} service={service}>
  46. <Consumer />
  47. </ConfigProvider>
  48. ))
  49. try {
  50. await app.renderOnce()
  51. expect(app.captureCharFrame()).toContain("mouse ctrl+x")
  52. if (!context) throw new Error("Config context was not provided")
  53. await context.update((draft) => {
  54. draft.mouse = false
  55. draft.keybinds = { leader: "ctrl+o" }
  56. })
  57. await app.renderOnce()
  58. expect(app.captureCharFrame()).toContain("none ctrl+o")
  59. } finally {
  60. app.renderer.destroy()
  61. }
  62. })