|
1 | 1 | import Pattern from "./tiptap"; |
| 2 | +import events from "@patternslib/patternslib/src/core/events"; |
2 | 3 | import utils from "@patternslib/patternslib/src/core/utils"; |
3 | 4 | import tiptap_utils from "./utils"; |
4 | 5 |
|
@@ -1069,7 +1070,6 @@ describe("pat-tiptap", () => { |
1069 | 1070 | await utils.timeout(1); // Wait a tick for the tooltip to open. |
1070 | 1071 | await utils.timeout(1); // Wait a tick for the tooltip to open. |
1071 | 1072 | await utils.timeout(1); // Wait a tick for the tooltip to open. |
1072 | | - console.log(document.body.innerHTML); |
1073 | 1073 |
|
1074 | 1074 | expect(document.querySelector(".tiptap-items")).toBeFalsy(); |
1075 | 1075 | const mention = editable.firstChild.firstChild; |
@@ -1097,4 +1097,98 @@ describe("pat-tiptap", () => { |
1097 | 1097 | global.fetch.mockClear(); |
1098 | 1098 | delete global.fetch; |
1099 | 1099 | }); |
| 1100 | + |
| 1101 | + it("8.8 - Close suggestion dialog when clicking outside", async () => { |
| 1102 | + global.fetch = jest.fn().mockImplementation(mockFetch(SUGGESTION_RESPONSE)); |
| 1103 | + |
| 1104 | + document.body.innerHTML = ` |
| 1105 | + <textarea |
| 1106 | + class="pat-tiptap" |
| 1107 | + data-pat-tiptap=" |
| 1108 | + tags-menu: https://demo.at/tags.html; |
| 1109 | + "> |
| 1110 | + </textarea> |
| 1111 | + `; |
| 1112 | + |
| 1113 | + new Pattern(document.querySelector(".pat-tiptap")); |
| 1114 | + await utils.timeout(1); |
| 1115 | + |
| 1116 | + const editable = document.querySelector(".tiptap-container [contenteditable]"); |
| 1117 | + const range = document.createRange(); |
| 1118 | + const sel = window.getSelection(); |
| 1119 | + |
| 1120 | + // Add the triggering character into the content editable |
| 1121 | + editable.innerHTML = "<p>#</p>"; |
| 1122 | + // Set the cursor right after the #-sign. |
| 1123 | + range.setStart(editable.childNodes[0].childNodes[0], 1); |
| 1124 | + range.collapse(true); |
| 1125 | + sel.removeAllRanges(); |
| 1126 | + sel.addRange(range); |
| 1127 | + |
| 1128 | + await utils.timeout(1); // Wait a tick for the tooltip to open. |
| 1129 | + await utils.timeout(1); // Wait a tick for the suggestion-pattern to be initialized. |
| 1130 | + |
| 1131 | + // Check for class ``tiptap-tags`` set on tooltip container. |
| 1132 | + expect(document.querySelector(".tooltip-container.tiptap-tags")).toBeTruthy(); |
| 1133 | + expect(document.querySelector(".tiptap-items")).toBeTruthy(); |
| 1134 | + |
| 1135 | + // No close when clicking within the suggestion menu |
| 1136 | + document.querySelector(".tiptap-items").dispatchEvent(events.mousedown_event()); |
| 1137 | + expect(document.querySelector(".tooltip-container.tiptap-tags")).toBeTruthy(); |
| 1138 | + |
| 1139 | + // Close when clicking outside the suggestion menu |
| 1140 | + document.body.dispatchEvent(events.mousedown_event()); |
| 1141 | + expect(document.querySelector(".tooltip-container.tiptap-tags")).toBeFalsy(); |
| 1142 | + |
| 1143 | + global.fetch.mockClear(); |
| 1144 | + delete global.fetch; |
| 1145 | + }); |
| 1146 | + |
| 1147 | + it("8.9 - Close suggestion with Escape", async () => { |
| 1148 | + global.fetch = jest.fn().mockImplementation(mockFetch(SUGGESTION_RESPONSE)); |
| 1149 | + |
| 1150 | + document.body.innerHTML = ` |
| 1151 | + <textarea |
| 1152 | + class="pat-tiptap" |
| 1153 | + data-pat-tiptap=" |
| 1154 | + tags-menu: https://demo.at/tags.html; |
| 1155 | + "> |
| 1156 | + </textarea> |
| 1157 | + `; |
| 1158 | + |
| 1159 | + new Pattern(document.querySelector(".pat-tiptap")); |
| 1160 | + await utils.timeout(1); |
| 1161 | + |
| 1162 | + const editable = document.querySelector(".tiptap-container [contenteditable]"); |
| 1163 | + const range = document.createRange(); |
| 1164 | + const sel = window.getSelection(); |
| 1165 | + |
| 1166 | + // Add the triggering character into the content editable |
| 1167 | + editable.innerHTML = "<p>#</p>"; |
| 1168 | + // Set the cursor right after the #-sign. |
| 1169 | + range.setStart(editable.childNodes[0].childNodes[0], 1); |
| 1170 | + range.collapse(true); |
| 1171 | + sel.removeAllRanges(); |
| 1172 | + sel.addRange(range); |
| 1173 | + |
| 1174 | + await utils.timeout(1); // Wait a tick for the tooltip to open. |
| 1175 | + await utils.timeout(1); // Wait a tick for the suggestion-pattern to be initialized. |
| 1176 | + |
| 1177 | + // Check for class ``tiptap-tags`` set on tooltip container. |
| 1178 | + expect(document.querySelector(".tooltip-container.tiptap-tags")).toBeTruthy(); |
| 1179 | + expect(document.querySelector(".tiptap-items")).toBeTruthy(); |
| 1180 | + |
| 1181 | + // Close when pressing "Escape" |
| 1182 | + editable.dispatchEvent( |
| 1183 | + new KeyboardEvent("keydown", { |
| 1184 | + key: "Escape", |
| 1185 | + bubbles: true, |
| 1186 | + cancelable: true, |
| 1187 | + }) |
| 1188 | + ); |
| 1189 | + expect(document.querySelector(".tooltip-container.tiptap-tags")).toBeFalsy(); |
| 1190 | + |
| 1191 | + global.fetch.mockClear(); |
| 1192 | + delete global.fetch; |
| 1193 | + }); |
1100 | 1194 | }); |
0 commit comments