Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(client): fire selectionchange after the selection moves, as browsers do
Elian Doran9add68c15bfe
diff --git a/apps/client/src/test/setup.ts b/apps/client/src/test/setup.tsindex 5d5704d065..33baa76c2c 100644--- a/apps/client/src/test/setup.ts+++ b/apps/client/src/test/setup.ts@@ -13,6 +13,7 @@ vi.mock("../services/server.js", mockServer);injectGlobals();survivePendingModalCallbacks();+deferSelectionChange();drainBootstrapTransitions();function injectGlobals() {@@ -49,6 +50,29 @@ function survivePendingModalCallbacks() {};}+/**+ * Fires `selectionchange` after the code that moved the selection, as browsers do.+ *+ * happy-dom dispatches it from inside `Selection.collapse()`. CodeMirror moves the DOM selection+ * during `EditorView.update()`, and its listener updates the view again, which throws+ * "Calls to EditorView.update are not allowed while an update is in progress".+ */+function deferSelectionChange() {+ const dispatch = document.dispatchEvent.bind(document);+ // happy-dom's `dispatchEvent()` calls itself again for each event phase.+ const deferred = new WeakSet<Event>();++ document.dispatchEvent = (event: Event) => {+ if (event.type !== "selectionchange" || deferred.has(event)) {+ return dispatch(event);+ }++ deferred.add(event);+ queueMicrotask(() => dispatch(event));+ return true;+ };+}+function mockWebsocket() {function subscribeToMessages(_callback: (message: unknown) => void) {// Do nothing.