|
| 1 | +import { BlockNoteEditor, editorHasBlockWithType } from "@blocknote/core"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import * as z from "zod/v4"; |
| 4 | +import { createTestEditor } from "../createTestEditor.js"; |
| 5 | +import { testSchema } from "../testSchema.js"; |
| 6 | + |
| 7 | +// Tests for verifying that type guards which check if an editor's schema |
| 8 | +// contains a block (and its props) are working correctly. |
| 9 | +describe("Editor block schema type guard tests", () => { |
| 10 | + const getEditor = createTestEditor(testSchema) as () => BlockNoteEditor< |
| 11 | + any, |
| 12 | + any, |
| 13 | + any |
| 14 | + >; |
| 15 | + |
| 16 | + it("blockType", () => { |
| 17 | + expect(editorHasBlockWithType(getEditor(), "heading")).toBeTruthy(); |
| 18 | + }); |
| 19 | + |
| 20 | + it("blockTypeInvalid", () => { |
| 21 | + expect(editorHasBlockWithType(getEditor(), "embed")).toBeFalsy(); |
| 22 | + }); |
| 23 | + |
| 24 | + it("blockWithPropTypes", () => { |
| 25 | + expect( |
| 26 | + editorHasBlockWithType( |
| 27 | + getEditor(), |
| 28 | + "heading", |
| 29 | + z.object({ |
| 30 | + level: z.number(), |
| 31 | + textColor: z.string(), |
| 32 | + }), |
| 33 | + ), |
| 34 | + ).toBeTruthy(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("blockWithPropTypesInvalidType", () => { |
| 38 | + expect( |
| 39 | + editorHasBlockWithType( |
| 40 | + getEditor(), |
| 41 | + "heading", |
| 42 | + z.object({ |
| 43 | + level: z.number(), |
| 44 | + textColor: z.number(), |
| 45 | + }), |
| 46 | + ), |
| 47 | + ).toBeFalsy(); |
| 48 | + }); |
| 49 | + |
| 50 | + it("blockWithPropSchema", () => { |
| 51 | + expect( |
| 52 | + editorHasBlockWithType( |
| 53 | + getEditor(), |
| 54 | + "heading", |
| 55 | + z.object({ |
| 56 | + level: z.union([z.literal(1), z.literal(2), z.literal(3)]).default(1), |
| 57 | + textColor: z.string().default("default"), |
| 58 | + }), |
| 59 | + ), |
| 60 | + ).toBeTruthy(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("blockWithPropSchemaInvalidType", () => { |
| 64 | + expect( |
| 65 | + editorHasBlockWithType( |
| 66 | + getEditor(), |
| 67 | + "heading", |
| 68 | + z.object({ |
| 69 | + level: z.union([z.literal(1), z.literal(2), z.literal(3)]).default(1), |
| 70 | + textColor: z.number().default(1), |
| 71 | + }), |
| 72 | + ), |
| 73 | + ).toBeFalsy(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("blockWithPropSchemaInvalidValues", () => { |
| 77 | + expect( |
| 78 | + editorHasBlockWithType( |
| 79 | + getEditor(), |
| 80 | + "heading", |
| 81 | + z.object({ |
| 82 | + level: z.union([z.literal(1), z.literal(2), z.literal(3)]).default(1), |
| 83 | + textColor: z.string().default("default"), |
| 84 | + }), |
| 85 | + ), |
| 86 | + ).toBeFalsy(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("blockWithPropTypesUndefinedDefault", () => { |
| 90 | + expect( |
| 91 | + editorHasBlockWithType( |
| 92 | + getEditor(), |
| 93 | + "numberedListItem", |
| 94 | + z.object({ |
| 95 | + start: z.number(), |
| 96 | + textColor: z.string(), |
| 97 | + }), |
| 98 | + ), |
| 99 | + ).toBeTruthy(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("blockWithPropSchemaUndefinedDefaultInvalidType", () => { |
| 103 | + expect( |
| 104 | + editorHasBlockWithType( |
| 105 | + getEditor(), |
| 106 | + "numberedListItem", |
| 107 | + z.object({ |
| 108 | + start: z.string(), |
| 109 | + textColor: z.string(), |
| 110 | + }), |
| 111 | + ), |
| 112 | + ).toBeFalsy(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("customBlockType", () => { |
| 116 | + expect(editorHasBlockWithType(getEditor(), "simpleImage")).toBeTruthy(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("customBlockWithPropTypes", () => { |
| 120 | + expect( |
| 121 | + editorHasBlockWithType( |
| 122 | + getEditor(), |
| 123 | + "simpleImage", |
| 124 | + z.object({ |
| 125 | + name: z.string(), |
| 126 | + url: z.string(), |
| 127 | + }), |
| 128 | + ), |
| 129 | + ).toBeTruthy(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("customBlockWithPropTypesInvalidType", () => { |
| 133 | + expect( |
| 134 | + editorHasBlockWithType( |
| 135 | + getEditor(), |
| 136 | + "simpleImage", |
| 137 | + z.object({ |
| 138 | + name: z.string(), |
| 139 | + url: z.number(), |
| 140 | + }), |
| 141 | + ), |
| 142 | + ).toBeFalsy(); |
| 143 | + }); |
| 144 | + |
| 145 | + it("customBlockWithPropSchema", () => { |
| 146 | + expect( |
| 147 | + editorHasBlockWithType( |
| 148 | + getEditor(), |
| 149 | + "simpleImage", |
| 150 | + z.object({ |
| 151 | + name: z.string().default(""), |
| 152 | + url: z.string().default(""), |
| 153 | + }), |
| 154 | + ), |
| 155 | + ).toBeTruthy(); |
| 156 | + }); |
| 157 | + |
| 158 | + it("customBlockWithPropSchemaInvalidType", () => { |
| 159 | + expect( |
| 160 | + editorHasBlockWithType( |
| 161 | + getEditor(), |
| 162 | + "simpleImage", |
| 163 | + z.object({ |
| 164 | + name: z.boolean().default(false), |
| 165 | + url: z.string().default(""), |
| 166 | + }), |
| 167 | + ), |
| 168 | + ).toBeFalsy(); |
| 169 | + }); |
| 170 | + |
| 171 | + it("customBlockWithPropSchemaInvalidValues", () => { |
| 172 | + expect( |
| 173 | + editorHasBlockWithType( |
| 174 | + getEditor(), |
| 175 | + "simpleImage", |
| 176 | + z.object({ |
| 177 | + name: z |
| 178 | + .union([z.literal("image"), z.literal("photo")]) |
| 179 | + .default("photo"), |
| 180 | + url: z.string().default(""), |
| 181 | + }), |
| 182 | + ), |
| 183 | + ).toBeFalsy(); |
| 184 | + }); |
| 185 | +}); |
0 commit comments