Skip to content

Commit

Permalink
more types/state tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forman committed Dec 10, 2024
1 parent d287830 commit 92ce571
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
22 changes: 22 additions & 0 deletions chartlets.js/packages/lib/src/types/state/component.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect } from "vitest";
import { isComponentState, isContainerState } from "./component";

describe("isComponentState", () => {
it("should work", () => {
expect(isComponentState({ type: "Button" })).toBe(true);
expect(isComponentState({ type: "Button", children: [] })).toBe(true);
expect(isComponentState({})).toBe(false);
expect(isComponentState("Button")).toBe(false);
expect(isComponentState({ type: 2 })).toBe(false);
});
});

describe("isContainerState", () => {
it("should work", () => {
expect(isContainerState({ type: "Button" })).toBe(false);
expect(isContainerState({ type: "Button", children: [] })).toBe(true);
expect(isContainerState({})).toBe(false);
expect(isContainerState("Button")).toBe(false);
expect(isContainerState({ type: 2 })).toBe(false);
});
});
28 changes: 28 additions & 0 deletions chartlets.js/packages/lib/src/types/state/options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from "vitest";
import { isMutableHostStore } from "./options";

const hostStore = {
get: (name: string) => name,
subscribe: (_cb: () => void) => {},
};

const mutableHostStore = {
...hostStore,
set: (_name: string, _value: unknown) => {},
};

describe("isHostStore", () => {
it("should work", () => {
expect(isMutableHostStore({})).toBe(false);
expect(isMutableHostStore(hostStore)).toBe(true);

Check failure on line 17 in chartlets.js/packages/lib/src/types/state/options.test.ts

View workflow job for this annotation

GitHub Actions / frontend

src/types/state/options.test.ts > isHostStore > should work

AssertionError: expected false to be true // Object.is equality - Expected + Received - true + false ❯ src/types/state/options.test.ts:17:43
expect(isMutableHostStore(mutableHostStore)).toBe(true);
});
});

describe("isMutableHostStore", () => {
it("should work", () => {
expect(isMutableHostStore({})).toBe(false);
expect(isMutableHostStore(hostStore)).toBe(false);
expect(isMutableHostStore(mutableHostStore)).toBe(true);
});
});
14 changes: 10 additions & 4 deletions chartlets.js/packages/lib/src/types/state/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { ComponentType } from "react";
import type { ApiOptions } from "@/types/api";
import type { LoggingOptions } from "@/actions/helpers/configureLogging";
import type { ComponentProps } from "@/component/Component";
import { isObject } from "@/utils/isObject";
import { isFunction } from "@/utils/isFunction";

/**
* The host store represents an interface to the state of
Expand Down Expand Up @@ -52,10 +54,14 @@ export interface MutableHostStore extends HostStore {
set: (property: string, value: unknown) => void;
}

export function isMutableHostStore(
hostStore: HostStore | undefined,
): hostStore is MutableHostStore {
return !!hostStore && typeof hostStore.set === "function";
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);
}

/**
Expand Down

0 comments on commit 92ce571

Please sign in to comment.