Skip to content

Commit 18f17fb

Browse files
committed
more actions tests
1 parent 5743653 commit 18f17fb

File tree

2 files changed

+112
-4
lines changed

2 files changed

+112
-4
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
});

chartlets.js/packages/lib/src/actions/configureFramework.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { isObject } from "@/utils/isObject";
1111
import { handleHostStoreChange } from "./handleHostStoreChange";
1212
import { configureLogging } from "./helpers/configureLogging";
1313

14-
export function configureFramework(options: FrameworkOptions) {
14+
export function configureFramework(options?: FrameworkOptions) {
15+
options = options || {};
1516
if (options.logging) {
1617
configureLogging(options.logging);
1718
}
@@ -26,16 +27,18 @@ export function configureFramework(options: FrameworkOptions) {
2627
}
2728
}
2829

29-
function resolvePlugin(plugin: PluginLike) {
30+
export function resolvePlugin(plugin: PluginLike): Promise<Plugin | undefined> {
3031
if (isPromise<PluginLike>(plugin)) {
31-
plugin.then(resolvePlugin);
32+
return plugin.then(resolvePlugin);
3233
} else if (isFunction(plugin)) {
33-
resolvePlugin(plugin());
34+
return resolvePlugin(plugin());
3435
} else if (isObject(plugin) && plugin.components) {
3536
(plugin.components as ComponentRegistration[]).forEach(
3637
([name, component]) => {
3738
registry.register(name, component);
3839
},
3940
);
41+
return Promise.resolve(plugin);
4042
}
43+
return Promise.resolve(undefined);
4144
}

0 commit comments

Comments
 (0)