|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import { configureFramework, resolvePlugin } from "./configureFramework"; |
| 3 | +import { store } from "@/store"; |
| 4 | +import { registry } from "@/components/registry"; |
| 5 | +import { ComponentProps } from "@/components/Component"; |
| 6 | +import { FC } from "react"; |
| 7 | + |
| 8 | +function getComponents() { |
| 9 | + interface DivProps extends ComponentProps { |
| 10 | + text: string; |
| 11 | + } |
| 12 | + const Div: FC<DivProps> = ({ text }) => <div>{text}</div>; |
| 13 | + return [ |
| 14 | + ["A", Div as FC<ComponentProps>], |
| 15 | + ["B", Div as FC<ComponentProps>], |
| 16 | + ]; |
| 17 | +} |
| 18 | + |
| 19 | +describe("configureFramework", () => { |
| 20 | + it("should accept no arg", () => { |
| 21 | + configureFramework(); |
| 22 | + expect(store.getState().configuration).toEqual({}); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should accept empty arg", () => { |
| 26 | + configureFramework({}); |
| 27 | + expect(store.getState().configuration).toEqual({}); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should enable logging", () => { |
| 31 | + configureFramework({ |
| 32 | + logging: { |
| 33 | + enabled: true, |
| 34 | + }, |
| 35 | + }); |
| 36 | + expect(store.getState().configuration).toEqual({ |
| 37 | + logging: { enabled: true }, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("should subscribe to host store", () => { |
| 42 | + const listeners = []; |
| 43 | + const hostStore = { |
| 44 | + get: (_key: string) => null, |
| 45 | + subscribe: (l: () => void) => { |
| 46 | + listeners.push(l); |
| 47 | + }, |
| 48 | + }; |
| 49 | + configureFramework({ |
| 50 | + hostStore, |
| 51 | + }); |
| 52 | + expect(listeners.length).toBe(1); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should install plugins", () => { |
| 56 | + expect(registry.types.length).toBe(0); |
| 57 | + configureFramework({ |
| 58 | + plugins: [{ components: getComponents() }], |
| 59 | + }); |
| 60 | + expect(registry.types.length).toBe(2); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("resolvePlugin", () => { |
| 65 | + beforeEach(() => { |
| 66 | + registry.clear(); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + registry.clear(); |
| 71 | + }); |
| 72 | + |
| 73 | + it("should resolve a object", async () => { |
| 74 | + const pluginObj = { components: getComponents() }; |
| 75 | + expect(registry.types.length).toBe(0); |
| 76 | + const result = await resolvePlugin(pluginObj); |
| 77 | + expect(result).toBe(pluginObj); |
| 78 | + expect(registry.types.length).toBe(2); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should resolve a function", async () => { |
| 82 | + const pluginObj = { components: getComponents() }; |
| 83 | + const pluginFunction = () => pluginObj; |
| 84 | + expect(registry.types.length).toBe(0); |
| 85 | + const result = await resolvePlugin(pluginFunction); |
| 86 | + expect(result).toBe(pluginObj); |
| 87 | + expect(registry.types.length).toBe(2); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should resolve a promise", async () => { |
| 91 | + const pluginObj = { components: getComponents() }; |
| 92 | + const pluginPromise = Promise.resolve(pluginObj); |
| 93 | + expect(registry.types.length).toBe(0); |
| 94 | + const result = await resolvePlugin(pluginPromise); |
| 95 | + expect(result).toBe(pluginObj); |
| 96 | + expect(registry.types.length).toBe(2); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should resolve undefined", async () => { |
| 100 | + expect(registry.types.length).toBe(0); |
| 101 | + const result = await resolvePlugin(undefined); |
| 102 | + expect(result).toBe(undefined); |
| 103 | + expect(registry.types.length).toBe(0); |
| 104 | + }); |
| 105 | +}); |
0 commit comments