-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from bcdev/forman-33-actions_tests
Actions test #1
- Loading branch information
Showing
11 changed files
with
307 additions
and
103 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
chartlets.js/packages/lib/src/actions/configureFramework.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { ComponentType, FC } from "react"; | ||
import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
import { configureFramework, resolvePlugin } from "./configureFramework"; | ||
import { store } from "@/store"; | ||
import { registry } from "@/components/registry"; | ||
import type { HostStore } from "@/types/state/host"; | ||
import type { Plugin } from "@/types/state/plugin"; | ||
import type { ComponentProps } from "@/components/Component"; | ||
|
||
function getComponents(): [string, ComponentType<ComponentProps>][] { | ||
interface DivProps extends ComponentProps { | ||
text: string; | ||
} | ||
const Div: FC<DivProps> = ({ text }) => <div>{text}</div>; | ||
return [ | ||
["A", Div as FC<ComponentProps>], | ||
["B", Div as FC<ComponentProps>], | ||
]; | ||
} | ||
|
||
describe("configureFramework", () => { | ||
it("should accept no arg", () => { | ||
configureFramework(); | ||
expect(store.getState().configuration).toEqual({}); | ||
}); | ||
|
||
it("should accept empty arg", () => { | ||
configureFramework({}); | ||
expect(store.getState().configuration).toEqual({}); | ||
}); | ||
|
||
it("should enable logging", () => { | ||
configureFramework({ | ||
logging: { | ||
enabled: true, | ||
}, | ||
}); | ||
expect(store.getState().configuration).toEqual({ | ||
logging: { enabled: true }, | ||
}); | ||
}); | ||
|
||
it("should subscribe to host store", () => { | ||
const listeners = []; | ||
const hostStore: HostStore = { | ||
get: (_key: string) => null, | ||
subscribe: (l: () => void) => { | ||
listeners.push(l); | ||
}, | ||
}; | ||
configureFramework({ | ||
hostStore, | ||
}); | ||
expect(listeners.length).toBe(1); | ||
}); | ||
|
||
it("should install plugins", () => { | ||
expect(registry.types.length).toBe(0); | ||
configureFramework({ | ||
plugins: [{ components: getComponents() }], | ||
}); | ||
expect(registry.types.length).toBe(2); | ||
}); | ||
}); | ||
|
||
describe("resolvePlugin", () => { | ||
beforeEach(() => { | ||
registry.clear(); | ||
}); | ||
|
||
afterEach(() => { | ||
registry.clear(); | ||
}); | ||
|
||
it("should resolve a object", async () => { | ||
const pluginObj: Plugin = { components: getComponents() }; | ||
expect(registry.types.length).toBe(0); | ||
const result = await resolvePlugin(pluginObj); | ||
expect(result).toBe(pluginObj); | ||
expect(registry.types.length).toBe(2); | ||
}); | ||
|
||
it("should resolve a function", async () => { | ||
const pluginObj = { components: getComponents() }; | ||
const pluginFunction = () => pluginObj; | ||
expect(registry.types.length).toBe(0); | ||
const result = await resolvePlugin(pluginFunction); | ||
expect(result).toBe(pluginObj); | ||
expect(registry.types.length).toBe(2); | ||
}); | ||
|
||
it("should resolve a promise", async () => { | ||
const pluginObj = { components: getComponents() }; | ||
const pluginPromise = Promise.resolve(pluginObj); | ||
expect(registry.types.length).toBe(0); | ||
const result = await resolvePlugin(pluginPromise); | ||
expect(result).toBe(pluginObj); | ||
expect(registry.types.length).toBe(2); | ||
}); | ||
|
||
it("should resolve undefined", async () => { | ||
expect(registry.types.length).toBe(0); | ||
const result = await resolvePlugin(undefined as unknown as Plugin); | ||
expect(result).toBe(undefined); | ||
expect(registry.types.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
chartlets.js/packages/lib/src/actions/handleHostStoreChanges.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { describe, it, expect, beforeEach } from "vitest"; | ||
import { store } from "@/store"; | ||
import { handleHostStoreChange } from "./handleHostStoreChange"; | ||
|
||
describe("handleHostStoreChange", () => { | ||
let listeners: (() => void)[] = []; | ||
let hostState: Record<string, unknown> = {}; | ||
const hostStore = { | ||
get: (key: string) => hostState[key], | ||
set: (key: string, value: unknown) => { | ||
hostState = { ...hostState, [key]: value }; | ||
listeners.forEach((l) => void l()); | ||
}, | ||
subscribe: (_l: () => void) => { | ||
listeners.push(_l); | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
listeners = []; | ||
hostState = {}; | ||
}); | ||
|
||
it("should do nothing without host store", () => { | ||
store.setState({ configuration: {} }); | ||
const oldState = store.getState(); | ||
handleHostStoreChange(); | ||
const newState = store.getState(); | ||
expect(newState).toBe(oldState); | ||
expect(newState).toEqual(oldState); | ||
}); | ||
|
||
it("should synchronize theme mode", () => { | ||
store.setState({ configuration: { hostStore } }); | ||
expect(store.getState().themeMode).toBeUndefined(); | ||
hostStore.set("themeMode", "light"); | ||
handleHostStoreChange(); | ||
expect(store.getState().themeMode).toEqual("light"); | ||
}); | ||
|
||
it("should generate callback requests", () => { | ||
const extensions = [{ name: "e0", version: "0", contributes: ["panels"] }]; | ||
store.setState({ | ||
configuration: { hostStore }, | ||
extensions, | ||
contributionsResult: { | ||
status: "ok", | ||
data: { | ||
extensions, | ||
contributions: { | ||
panels: [ | ||
{ | ||
name: "p0", | ||
extension: "e0", | ||
layout: { | ||
function: { | ||
name: "layout", | ||
parameters: [], | ||
return: {}, | ||
}, | ||
inputs: [], | ||
outputs: [], | ||
}, | ||
callbacks: [ | ||
{ | ||
function: { | ||
name: "callback", | ||
parameters: [], | ||
return: {}, | ||
}, | ||
inputs: [{ id: "@app", property: "variableName" }], | ||
outputs: [{ id: "select", property: "value" }], | ||
}, | ||
], | ||
initialState: {}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
}); | ||
hostStore.set("variableName", "CHL"); | ||
handleHostStoreChange(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { isObject } from "@/utils/isObject"; | ||
import { isFunction } from "@/utils/isFunction"; | ||
|
||
/** | ||
* The host store represents an interface to the state of | ||
* the application that is using Chartlets. | ||
*/ | ||
export interface HostStore { | ||
/** | ||
* Let Chartlets listen to changes in the host store that may | ||
* cause different values to be returned from the `get()` method. | ||
* | ||
* @param listener A listener that is called when the | ||
* host store changes | ||
*/ | ||
subscribe: (listener: () => void) => void; | ||
|
||
/** | ||
* Get a property value from the host state. | ||
* | ||
* @param property The property name. | ||
* @returns The property value. | ||
*/ | ||
get: (property: string) => unknown; | ||
|
||
/** | ||
* **UNSTABLE API** | ||
* | ||
* Set a property value in the host state. | ||
* | ||
* @param property The property name. | ||
* @param value The new property value. | ||
*/ | ||
set?: (property: string, value: unknown) => void; | ||
} | ||
|
||
/** | ||
* A mutable host store implements the `set()` method. | ||
*/ | ||
export interface MutableHostStore extends HostStore { | ||
/** | ||
* **UNSTABLE API** | ||
* | ||
* Set a property value in the host state. | ||
* | ||
* @param property The property name. | ||
* @param value The new property value. | ||
*/ | ||
set: (property: string, value: unknown) => void; | ||
} | ||
|
||
export function isHostStore(value: unknown): value is HostStore { | ||
return ( | ||
isObject(value) && isFunction(value.get) && isFunction(value.subscribe) | ||
); | ||
} | ||
|
||
export function isMutableHostStore(value: unknown): value is MutableHostStore { | ||
return isHostStore(value) && isFunction(value.set); | ||
} |
Oops, something went wrong.