renderer.test.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { describe, it, expect } from "vitest";
  2. import React from "react";
  3. import { createRendererFromCatalog } from "./renderer";
  4. // Mock catalog object that matches the Catalog interface
  5. const mockCatalog = {
  6. name: "test",
  7. componentNames: ["text", "button"],
  8. actionNames: [],
  9. functionNames: [],
  10. validation: "strict" as const,
  11. components: {},
  12. actions: {},
  13. functions: {},
  14. elementSchema: {} as never,
  15. specSchema: {} as never,
  16. hasComponent: () => true,
  17. hasAction: () => false,
  18. hasFunction: () => false,
  19. validateElement: () => ({ success: true }),
  20. validateSpec: () => ({ success: true }),
  21. };
  22. describe("createRendererFromCatalog", () => {
  23. it("creates a React component from catalog and registry", () => {
  24. const registry = {
  25. text: ({ element }: { element: { props: { content: string } } }) =>
  26. React.createElement("span", null, element.props.content),
  27. button: ({ element }: { element: { props: { label: string } } }) =>
  28. React.createElement("button", null, element.props.label),
  29. };
  30. const CatalogRenderer = createRendererFromCatalog(mockCatalog, registry);
  31. expect(typeof CatalogRenderer).toBe("function");
  32. });
  33. it("returned component renders null for null spec", () => {
  34. const registry = {
  35. text: () => React.createElement("span"),
  36. };
  37. const CatalogRenderer = createRendererFromCatalog(mockCatalog, registry);
  38. // The component should handle null spec gracefully
  39. const element = React.createElement(CatalogRenderer, { spec: null });
  40. expect(element).toBeDefined();
  41. });
  42. it("returned component accepts loading prop", () => {
  43. const registry = {
  44. text: () => React.createElement("span"),
  45. };
  46. const CatalogRenderer = createRendererFromCatalog(mockCatalog, registry);
  47. const element = React.createElement(CatalogRenderer, {
  48. spec: null,
  49. loading: true,
  50. });
  51. expect(element.props.loading).toBe(true);
  52. });
  53. it("returned component accepts fallback prop", () => {
  54. const registry = {
  55. text: () => React.createElement("span"),
  56. };
  57. const Fallback = () =>
  58. React.createElement("div", null, "Unknown component");
  59. const CatalogRenderer = createRendererFromCatalog(mockCatalog, registry);
  60. const element = React.createElement(CatalogRenderer, {
  61. spec: null,
  62. fallback: Fallback,
  63. });
  64. expect(element.props.fallback).toBe(Fallback);
  65. });
  66. });