|
| 1 | +/* global jest, global */ |
| 2 | + |
| 3 | +import { createBasicEditor, updateContent, sleep } from "src/decidim/editor/test/helpers"; |
| 4 | + |
| 5 | +import Dialog from "src/decidim/editor/extensions/dialog"; |
| 6 | +import Image from "src/decidim/editor/extensions/image"; |
| 7 | +import uploadTemplates from "src/decidim/editor/test/fixtures/upload_templates"; |
| 8 | + |
| 9 | +class DummyDialog { |
| 10 | + constructor(element) { this.element = element; } |
| 11 | + |
| 12 | + open() { this.element.dataset.dialogOpen = true; } |
| 13 | + |
| 14 | + close() { this.element.dataset.dialogOpen = null; } |
| 15 | +} |
| 16 | + |
| 17 | +// Not implemented in Jest |
| 18 | +global.Touch = class Touch { |
| 19 | + constructor(options) { |
| 20 | + this.pageX = options.pageX; |
| 21 | + this.pageY = options.pageY; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe("Image (links)", () => { |
| 26 | + let editor = null; |
| 27 | + let editorElement = null; |
| 28 | + let uploadDialogElement = null; |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + document.body.innerHTML = ""; |
| 32 | + |
| 33 | + const dialogWrapper = document.createElement("div"); |
| 34 | + dialogWrapper.innerHTML = uploadTemplates.redesign; |
| 35 | + uploadDialogElement = dialogWrapper.firstElementChild; |
| 36 | + uploadDialogElement.dataset.dialog = "testDialog"; |
| 37 | + uploadDialogElement.dialog = new DummyDialog(uploadDialogElement); |
| 38 | + window.Decidim.currentDialogs = { testDialog: uploadDialogElement.dialog }; |
| 39 | + document.body.append(uploadDialogElement); |
| 40 | + |
| 41 | + editor = createBasicEditor({ |
| 42 | + extensions: [Dialog, Image.configure({ uploadDialogSelector: "#upload_dialog", uploadImagesPath: "/editor_images", contentTypes: ["image/png"] })] |
| 43 | + }); |
| 44 | + editorElement = editor.view.dom; |
| 45 | + |
| 46 | + const csrf = document.createElement("meta"); |
| 47 | + csrf.setAttribute("name", "csrf-token"); |
| 48 | + csrf.setAttribute("content", "abcdef0123456789"); |
| 49 | + document.head.append(csrf); |
| 50 | + |
| 51 | + global.fetch = jest.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ url: "/path/to/image.jpg" }) })); |
| 52 | + }); |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + jest.restoreAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("allows setting an image with href and target attributes", async () => { |
| 59 | + editorElement.focus(); |
| 60 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" }); |
| 61 | + await sleep(0); |
| 62 | + |
| 63 | + expect(editor.getHTML()).toMatchHtml(` |
| 64 | + <a href="https://example.com" target="_blank" rel="noopener noreferrer"> |
| 65 | + <div class="editor-content-image" data-image=""> |
| 66 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 67 | + </div> |
| 68 | + </a> |
| 69 | + `); |
| 70 | + }); |
| 71 | + |
| 72 | + it("allows setting an image with href but no target", async () => { |
| 73 | + editorElement.focus(); |
| 74 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com" }); |
| 75 | + await sleep(0); |
| 76 | + |
| 77 | + expect(editor.getHTML()).toMatchHtml(` |
| 78 | + <a href="https://example.com"> |
| 79 | + <div class="editor-content-image" data-image=""> |
| 80 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 81 | + </div> |
| 82 | + </a> |
| 83 | + `); |
| 84 | + }); |
| 85 | + |
| 86 | + it("parses existing HTML with linked images", async () => { |
| 87 | + editorElement.focus(); |
| 88 | + await updateContent(editorElement, |
| 89 | + '<a href="https://example.com" target="_blank"><div class="editor-content-image" data-image=""><img src="/path/to/image.jpg" alt="Test image"></div></a>', |
| 90 | + editor |
| 91 | + ); |
| 92 | + |
| 93 | + expect(editor.getHTML()).toMatchHtml(` |
| 94 | + <a href="https://example.com" target="_blank" rel="noopener noreferrer"> |
| 95 | + <div class="editor-content-image" data-image=""> |
| 96 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 97 | + </div> |
| 98 | + </a> |
| 99 | + `); |
| 100 | + }); |
| 101 | + |
| 102 | + it("parses existing HTML with linked images without target", async () => { |
| 103 | + editorElement.focus(); |
| 104 | + await updateContent(editorElement, |
| 105 | + '<a href="https://example.com"><div class="editor-content-image" data-image=""><img src="/path/to/image.jpg" alt="Test image"></div></a>', |
| 106 | + editor |
| 107 | + ); |
| 108 | + |
| 109 | + expect(editor.getHTML()).toMatchHtml(` |
| 110 | + <a href="https://example.com"> |
| 111 | + <div class="editor-content-image" data-image=""> |
| 112 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 113 | + </div> |
| 114 | + </a> |
| 115 | + `); |
| 116 | + }); |
| 117 | + |
| 118 | + it("allows updating image link attributes", async () => { |
| 119 | + editorElement.focus(); |
| 120 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" }); |
| 121 | + await sleep(0); |
| 122 | + |
| 123 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://docs.example.com" }); |
| 124 | + await sleep(0); |
| 125 | + |
| 126 | + expect(editor.getHTML()).toMatchHtml(` |
| 127 | + <a href="https://docs.example.com"> |
| 128 | + <div class="editor-content-image" data-image=""> |
| 129 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 130 | + </div> |
| 131 | + </a> |
| 132 | + `); |
| 133 | + }); |
| 134 | + |
| 135 | + it("allows removing link from image", async () => { |
| 136 | + editorElement.focus(); |
| 137 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" }); |
| 138 | + await sleep(0); |
| 139 | + |
| 140 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: null, target: null }); |
| 141 | + await sleep(0); |
| 142 | + |
| 143 | + expect(editor.getHTML()).toMatchHtml(` |
| 144 | + <div class="editor-content-image" data-image=""> |
| 145 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 146 | + </div> |
| 147 | + `); |
| 148 | + }); |
| 149 | + |
| 150 | + it("renders images without links correctly", async () => { |
| 151 | + editorElement.focus(); |
| 152 | + editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image" }); |
| 153 | + await sleep(0); |
| 154 | + |
| 155 | + expect(editor.getHTML()).toMatchHtml(` |
| 156 | + <div class="editor-content-image" data-image=""> |
| 157 | + <img src="/path/to/image.jpg" alt="Test image"> |
| 158 | + </div> |
| 159 | + `); |
| 160 | + }); |
| 161 | +}); |
0 commit comments