Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions packages/core/src/blocks/defaultBlockTypeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,11 @@ export function editorHasBlockWithType<
.values === "object" &&
typeof propSpec.values === "object"
) {
if (
editor.schema.blockSpecs[blockType].config.propSchema[propName].values
.length !== propSpec.values.length
) {
return false;
}

for (
let i = 0;
i <
editor.schema.blockSpecs[blockType].config.propSchema[propName].values
.length;
i++
) {
for (const value of propSpec.values) {
if (
editor.schema.blockSpecs[blockType].config.propSchema[propName]
.values[i] !== propSpec.values[i]
!editor.schema.blockSpecs[blockType].config.propSchema[
propName
].values.includes(value)
) {
return false;
}
Expand Down
86 changes: 86 additions & 0 deletions tests/src/unit/core/typeGuards/runTests.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { BlockNoteEditor, editorHasBlockWithType } from "@blocknote/core";
import { describe, expect, it } from "vitest";

import { createTestEditor } from "../createTestEditor.js";
import { testSchema } from "../testSchema.js";

// Tests for verifying that type guards which check if an editor's schema
// contains a block (and its props) are working correctly.
describe("Editor block schema type guard tests", () => {
const getEditor = createTestEditor(testSchema) as () => BlockNoteEditor<
any,
any,
any
>;

it("blockType", () => {
expect(editorHasBlockWithType(getEditor(), "heading")).toBeTruthy();
});

it("blockTypeInvalid", () => {
expect(editorHasBlockWithType(getEditor(), "embed")).toBeFalsy();
});

it("blockWithPropTypes", () => {
expect(
editorHasBlockWithType(getEditor(), "heading", {
level: "number",
textColor: "string",
}),
).toBeTruthy();
});

it("blockWithPropTypesInvalidType", () => {
expect(
editorHasBlockWithType(getEditor(), "heading", {
level: "number",
textColor: "number",
}),
).toBeFalsy();
});

it("blockWithPropSchema", () => {
expect(
editorHasBlockWithType(getEditor(), "heading", {
level: { default: 1, values: [1, 2, 3] },
textColor: { default: "default" },
}),
).toBeTruthy();
});

it("blockWithPropSchemaInvalidType", () => {
expect(
editorHasBlockWithType(getEditor(), "heading", {
level: { default: 1, values: [1, 2, 3] },
textColor: { default: 1 },
}),
).toBeFalsy();
});

it("blockWithPropSchemaInvalidValues", () => {
expect(
editorHasBlockWithType(getEditor(), "heading", {
level: { default: 1, values: [1, 2, 3, 4, 5, 6, 7] },
textColor: { default: "default" },
}),
).toBeFalsy();
});

it("blockWithPropTypesUndefinedDefault", () => {
expect(
editorHasBlockWithType(getEditor(), "numberedListItem", {
start: "number",
textColor: "string",
}),
).toBeTruthy();
});

it("blockWithPropSchemaUndefinedDefaultInvalidType", () => {
expect(
editorHasBlockWithType(getEditor(), "numberedListItem", {
start: "string",
textColor: "string",
}),
).toBeFalsy();
});
});
Loading