|
| 1 | +import { CommonmarkEditor } from "../../../src/commonmark/editor"; |
| 2 | +import { RichTextEditor } from "../../../src/rich-text/editor"; |
| 3 | +import { placeholderPlugin } from "../../../src/shared/prosemirror-plugins/placeholder"; |
| 4 | + |
| 5 | +const PLACEHOLDER_TEXT = "This is a placeholder"; |
| 6 | + |
| 7 | +function commonmarkView( |
| 8 | + markdown: string, |
| 9 | + placeholderText: string |
| 10 | +): CommonmarkEditor { |
| 11 | + return new CommonmarkEditor(document.createElement("div"), markdown, { |
| 12 | + placeholderText, |
| 13 | + }); |
| 14 | +} |
| 15 | + |
| 16 | +function richView(markdownInput: string, placeholderText: string) { |
| 17 | + return new RichTextEditor(document.createElement("div"), markdownInput, { |
| 18 | + placeholderText, |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +describe("placeholder plugin", () => { |
| 23 | + describe("commonmark", () => { |
| 24 | + it("should add placeholder when the editor is empty", () => { |
| 25 | + const view = commonmarkView("", PLACEHOLDER_TEXT); |
| 26 | + const elAttr = |
| 27 | + view.editorView.dom.firstElementChild.getAttribute( |
| 28 | + "data-placeholder" |
| 29 | + ); |
| 30 | + const ariaAttr = |
| 31 | + view.editorView.dom.getAttribute("aria-placeholder"); |
| 32 | + |
| 33 | + expect(elAttr).toBe(PLACEHOLDER_TEXT); |
| 34 | + expect(ariaAttr).toBe(PLACEHOLDER_TEXT); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should not add placeholder when the editor is populated", () => { |
| 38 | + const view = commonmarkView("Hello world", PLACEHOLDER_TEXT); |
| 39 | + const elAttr = |
| 40 | + view.editorView.dom.firstElementChild.getAttribute( |
| 41 | + "data-placeholder" |
| 42 | + ); |
| 43 | + const ariaAttr = |
| 44 | + view.editorView.dom.getAttribute("aria-placeholder"); |
| 45 | + |
| 46 | + expect(elAttr).toBeNull(); |
| 47 | + expect(ariaAttr).toBe(PLACEHOLDER_TEXT); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should remove placeholder when text is added", () => { |
| 51 | + const view = commonmarkView("", PLACEHOLDER_TEXT); |
| 52 | + let elAttr = |
| 53 | + view.editorView.dom.firstElementChild.getAttribute( |
| 54 | + "data-placeholder" |
| 55 | + ); |
| 56 | + |
| 57 | + expect(elAttr).toBe(PLACEHOLDER_TEXT); |
| 58 | + |
| 59 | + view.editorView.dispatch( |
| 60 | + view.editorView.state.tr.insertText("Hello world") |
| 61 | + ); |
| 62 | + |
| 63 | + elAttr = |
| 64 | + view.editorView.dom.firstElementChild.getAttribute( |
| 65 | + "data-placeholder" |
| 66 | + ); |
| 67 | + expect(elAttr).toBeNull(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("rich-text", () => { |
| 72 | + it("should add placeholder when the editor is empty", () => { |
| 73 | + const view = richView("", PLACEHOLDER_TEXT); |
| 74 | + const elAttr = |
| 75 | + view.editorView.dom.firstElementChild.getAttribute( |
| 76 | + "data-placeholder" |
| 77 | + ); |
| 78 | + const ariaAttr = |
| 79 | + view.editorView.dom.getAttribute("aria-placeholder"); |
| 80 | + |
| 81 | + expect(elAttr).toBe(PLACEHOLDER_TEXT); |
| 82 | + expect(ariaAttr).toBe(PLACEHOLDER_TEXT); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should not add placeholder when the editor is populated", () => { |
| 86 | + const view = richView("# Hello", PLACEHOLDER_TEXT); |
| 87 | + const elAttr = |
| 88 | + view.editorView.dom.firstElementChild.getAttribute( |
| 89 | + "data-placeholder" |
| 90 | + ); |
| 91 | + const ariaAttr = |
| 92 | + view.editorView.dom.getAttribute("aria-placeholder"); |
| 93 | + |
| 94 | + expect(elAttr).toBeNull(); |
| 95 | + expect(ariaAttr).toBe(PLACEHOLDER_TEXT); |
| 96 | + }); |
| 97 | + |
| 98 | + it("should remove placeholder when text is added", () => { |
| 99 | + const view = richView("", PLACEHOLDER_TEXT); |
| 100 | + let elAttr = |
| 101 | + view.editorView.dom.firstElementChild.getAttribute( |
| 102 | + "data-placeholder" |
| 103 | + ); |
| 104 | + |
| 105 | + expect(elAttr).toBe(PLACEHOLDER_TEXT); |
| 106 | + |
| 107 | + view.editorView.dispatch( |
| 108 | + view.editorView.state.tr.insertText("Hello world") |
| 109 | + ); |
| 110 | + |
| 111 | + elAttr = |
| 112 | + view.editorView.dom.firstElementChild.getAttribute( |
| 113 | + "data-placeholder" |
| 114 | + ); |
| 115 | + expect(elAttr).toBeNull(); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe("plugin", () => { |
| 120 | + it("should not activate when the placeholder text is invalid", () => { |
| 121 | + // start with a valid option to set the base case |
| 122 | + let plugin = placeholderPlugin(PLACEHOLDER_TEXT); |
| 123 | + expect(plugin.spec.state).toBeDefined(); |
| 124 | + |
| 125 | + // now try with an invalid option |
| 126 | + plugin = placeholderPlugin(""); |
| 127 | + expect(plugin.spec.state).toBeUndefined(); |
| 128 | + }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments