Skip to content

Commit d597c8e

Browse files
committed
Fixed tests; split options type module
1 parent 3ba54dc commit d597c8e

File tree

10 files changed

+110
-104
lines changed

10 files changed

+110
-104
lines changed

chartlets.js/packages/lib/src/actions/configureFramework.test.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import type { ComponentType, FC } from "react";
12
import { describe, it, expect, beforeEach, afterEach } from "vitest";
23
import { configureFramework, resolvePlugin } from "./configureFramework";
34
import { store } from "@/store";
45
import { registry } from "@/components/registry";
5-
import { ComponentProps } from "@/components/Component";
6-
import { FC } from "react";
6+
import type { HostStore } from "@/types/state/host";
7+
import type { Plugin } from "@/types/state/plugin";
8+
import type { ComponentProps } from "@/components/Component";
79

8-
function getComponents() {
10+
function getComponents(): [string, ComponentType<ComponentProps>][] {
911
interface DivProps extends ComponentProps {
1012
text: string;
1113
}
@@ -40,7 +42,7 @@ describe("configureFramework", () => {
4042

4143
it("should subscribe to host store", () => {
4244
const listeners = [];
43-
const hostStore = {
45+
const hostStore: HostStore = {
4446
get: (_key: string) => null,
4547
subscribe: (l: () => void) => {
4648
listeners.push(l);
@@ -71,7 +73,7 @@ describe("resolvePlugin", () => {
7173
});
7274

7375
it("should resolve a object", async () => {
74-
const pluginObj = { components: getComponents() };
76+
const pluginObj: Plugin = { components: getComponents() };
7577
expect(registry.types.length).toBe(0);
7678
const result = await resolvePlugin(pluginObj);
7779
expect(result).toBe(pluginObj);
@@ -98,7 +100,7 @@ describe("resolvePlugin", () => {
98100

99101
it("should resolve undefined", async () => {
100102
expect(registry.types.length).toBe(0);
101-
const result = await resolvePlugin(undefined);
103+
const result = await resolvePlugin(undefined as unknown as Plugin);
102104
expect(result).toBe(undefined);
103105
expect(registry.types.length).toBe(0);
104106
});

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { store } from "@/store";
2+
import type { FrameworkOptions } from "@/types/state/options";
23
import type {
34
ComponentRegistration,
4-
FrameworkOptions,
5+
Plugin,
56
PluginLike,
6-
} from "@/types/state/options";
7+
} from "@/types/state/plugin";
78
import { registry } from "@/components/registry";
89
import { isPromise } from "@/utils/isPromise";
910
import { isFunction } from "@/utils/isFunction";
@@ -38,7 +39,7 @@ export function resolvePlugin(plugin: PluginLike): Promise<Plugin | undefined> {
3839
registry.register(name, component);
3940
},
4041
);
41-
return Promise.resolve(plugin);
42+
return Promise.resolve(plugin as Plugin);
4243
}
4344
return Promise.resolve(undefined);
4445
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { store } from "@/store";
21
import type {
32
CallbackRef,
43
CallbackRequest,
@@ -10,8 +9,8 @@ import { getInputValues } from "@/actions/helpers/getInputValues";
109
import { formatObjPath } from "@/utils/objPath";
1110
import { invokeCallbacks } from "@/actions/helpers/invokeCallbacks";
1211
import type { ContributionState } from "@/types/state/contribution";
13-
import type { HostStore } from "@/types/state/options";
14-
import type { store } from "@/store";
12+
import type { HostStore } from "@/types/state/host";
13+
import { store } from "@/store";
1514

1615
/**
1716
* A reference to a property of an input of a callback of a contribution.

chartlets.js/packages/lib/src/actions/helpers/applyStateChangeRequests.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ import {
1515
normalizeObjPath,
1616
setValue,
1717
} from "@/utils/objPath";
18-
import {
19-
isMutableHostStore,
20-
type MutableHostStore,
21-
} from "@/types/state/options";
18+
import { isMutableHostStore, type MutableHostStore } from "@/types/state/host";
2219
import {
2320
isHostChannel,
2421
isComponentChannel,

chartlets.js/packages/lib/src/actions/helpers/getInputValues.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from "@/types/state/component";
1313
import { formatObjPath, getValue, type ObjPathLike } from "@/utils/objPath";
1414
import { isObject } from "@/utils/isObject";
15-
import type { HostStore } from "@/types/state/options";
15+
import type { HostStore } from "@/types/state/host";
1616

1717
export function getInputValues(
1818
inputs: Input[],

chartlets.js/packages/lib/src/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ export {
2929
} from "@/hooks";
3030

3131
// Application interface
32-
export type {
33-
FrameworkOptions,
34-
HostStore,
35-
MutableHostStore,
36-
Plugin,
37-
PluginLike,
38-
} from "@/types/state/options";
32+
export type { HostStore, MutableHostStore } from "@/types/state/host";
33+
export type { Plugin, PluginLike } from "@/types/state/plugin";
34+
export type { FrameworkOptions } from "@/types/state/options";

chartlets.js/packages/lib/src/types/state/options.test.ts renamed to chartlets.js/packages/lib/src/types/state/host.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
type MutableHostStore,
55
isHostStore,
66
isMutableHostStore,
7-
} from "./options";
7+
} from "./host";
88

99
const hostStore: HostStore = {
1010
get: (name: string) => name,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { isObject } from "@/utils/isObject";
2+
import { isFunction } from "@/utils/isFunction";
3+
4+
/**
5+
* The host store represents an interface to the state of
6+
* the application that is using Chartlets.
7+
*/
8+
export interface HostStore {
9+
/**
10+
* Let Chartlets listen to changes in the host store that may
11+
* cause different values to be returned from the `get()` method.
12+
*
13+
* @param listener A listener that is called when the
14+
* host store changes
15+
*/
16+
subscribe: (listener: () => void) => void;
17+
18+
/**
19+
* Get a property value from the host state.
20+
*
21+
* @param property The property name.
22+
* @returns The property value.
23+
*/
24+
get: (property: string) => unknown;
25+
26+
/**
27+
* **UNSTABLE API**
28+
*
29+
* Set a property value in the host state.
30+
*
31+
* @param property The property name.
32+
* @param value The new property value.
33+
*/
34+
set?: (property: string, value: unknown) => void;
35+
}
36+
37+
/**
38+
* A mutable host store implements the `set()` method.
39+
*/
40+
export interface MutableHostStore extends HostStore {
41+
/**
42+
* **UNSTABLE API**
43+
*
44+
* Set a property value in the host state.
45+
*
46+
* @param property The property name.
47+
* @param value The new property value.
48+
*/
49+
set: (property: string, value: unknown) => void;
50+
}
51+
52+
export function isHostStore(value: unknown): value is HostStore {
53+
return (
54+
isObject(value) && isFunction(value.get) && isFunction(value.subscribe)
55+
);
56+
}
57+
58+
export function isMutableHostStore(value: unknown): value is MutableHostStore {
59+
return isHostStore(value) && isFunction(value.set);
60+
}

chartlets.js/packages/lib/src/types/state/options.ts

Lines changed: 2 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,7 @@
1-
import type { ComponentType } from "react";
2-
31
import type { ApiOptions } from "@/types/api";
2+
import type { PluginLike } from "@/types/state/plugin";
3+
import type { HostStore } from "@/types/state/host";
44
import type { LoggingOptions } from "@/actions/helpers/configureLogging";
5-
import type { ComponentProps } from "@/components/Component";
6-
import { isObject } from "@/utils/isObject";
7-
import { isFunction } from "@/utils/isFunction";
8-
9-
/**
10-
* The host store represents an interface to the state of
11-
* the application that is using Chartlets.
12-
*/
13-
export interface HostStore {
14-
/**
15-
* Let Chartlets listen to changes in the host store that may
16-
* cause different values to be returned from the `get()` method.
17-
*
18-
* @param listener A listener that is called when the
19-
* host store changes
20-
*/
21-
subscribe: (listener: () => void) => void;
22-
23-
/**
24-
* Get a property value from the host state.
25-
*
26-
* @param property The property name.
27-
* @returns The property value.
28-
*/
29-
get: (property: string) => unknown;
30-
31-
/**
32-
* **UNSTABLE API**
33-
*
34-
* Set a property value in the host state.
35-
*
36-
* @param property The property name.
37-
* @param value The new property value.
38-
*/
39-
set?: (property: string, value: unknown) => void;
40-
}
41-
42-
/**
43-
* A mutable host store implements the `set()` method.
44-
*/
45-
export interface MutableHostStore extends HostStore {
46-
/**
47-
* **UNSTABLE API**
48-
*
49-
* Set a property value in the host state.
50-
*
51-
* @param property The property name.
52-
* @param value The new property value.
53-
*/
54-
set: (property: string, value: unknown) => void;
55-
}
56-
57-
export function isHostStore(value: unknown): value is HostStore {
58-
return (
59-
isObject(value) && isFunction(value.get) && isFunction(value.subscribe)
60-
);
61-
}
62-
63-
export function isMutableHostStore(value: unknown): value is MutableHostStore {
64-
return isHostStore(value) && isFunction(value.set);
65-
}
66-
67-
/**
68-
* A framework plugin.
69-
* Plugins are no-arg functions that are called
70-
* after the framework's initialisation.
71-
* Most typically, a plugin wants to return new components
72-
* in the `components` array:
73-
* `{ components: [["MyComponent", MyComponent]] }`.
74-
*/
75-
export interface Plugin {
76-
components?: ComponentRegistration[];
77-
}
78-
79-
export type ComponentRegistration = [string, ComponentType<ComponentProps>];
80-
81-
export type PluginLike = Plugin | (() => Plugin) | Promise<PluginLike>;
825

836
/**
847
* Chartlets options to be provided
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import type { ComponentType } from "react";
2+
import type { ComponentProps } from "@/components/Component";
3+
4+
/**
5+
* A component registration - a pair comprising the component type name
6+
* and the React component.
7+
*/
8+
export type ComponentRegistration = [string, ComponentType<ComponentProps>];
9+
10+
/**
11+
* A framework plugin.
12+
* Plugins are no-arg functions that are called
13+
* after the framework's initialisation.
14+
* Most typically, a plugin wants to return new components
15+
* in the `components` array:
16+
* `{ components: [["MyComponent", MyComponent]] }`.
17+
*/
18+
export interface Plugin {
19+
components?: ComponentRegistration[];
20+
}
21+
22+
/**
23+
* An object of type `Plugin`, or a function that returns a
24+
* value that can be resolved to a `Plugin`,
25+
* or a promise that resolves to a value that can be
26+
* resolved to a `Plugin`.
27+
*/
28+
export type PluginLike = Plugin | (() => PluginLike) | Promise<PluginLike>;

0 commit comments

Comments
 (0)