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(bulk_actions): let the completion tests' saves land in their own test
Elian Doran811be2715c10
diff --git a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsxindex d957535e3a..0844fdcac3 100644--- a/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx+++ b/apps/client/src/widgets/dialogs/bulk_actions.spec.tsx@@ -13,7 +13,7 @@ import BulkActionsDialog from "./bulk_actions";const server = vi.hoisted(() => ({get: vi.fn(async (_url: string): Promise<string[]> => []),post: vi.fn(),- put: vi.fn(async () => {}),+ put: vi.fn(async (_url: string, _body: { value: string }) => {}),remove: vi.fn(async () => {}),getMaxKnownEntityChangeId: () => 0}));@@ -81,7 +81,7 @@ describe("BulkActionsDialog", () => {return [ ...container.querySelectorAll<HTMLInputElement>(".bulk-existing-action-list input.bulk-action-name") ];}- /** Types `value` into `input`, lets the debounced lookup run and returns the suggestion rows. */+ /** Types `value` into `input`, lets the debounced lookup run and returns the suggestion rows. Needs fake timers. */async function typeAndSuggest(input: HTMLInputElement | undefined, value: string) {if (!input) throw new Error("No such field.");await act(async () => {@@ -89,11 +89,20 @@ describe("BulkActionsDialog", () => {input.dispatchEvent(new Event("input", { bubbles: true }));});await act(async () => {- await new Promise((resolve) => setTimeout(resolve, 200));+ await vi.advanceTimersByTimeAsync(200);});return [ ...document.querySelectorAll<HTMLElement>(".form-autocomplete-dropdown li") ];}+ /** Runs the row's debounced save, so it lands in this test, and returns the saved action. */+ async function savedAction() {+ await act(async () => {+ await vi.advanceTimersByTimeAsync(2000);+ });+ const value = server.put.mock.calls.at(-1)?.[1]?.value;+ return value === undefined ? undefined : JSON.parse(value);+ }+it("shows the first selected notes as chips and the rest on demand", async () => {const noteIds = [ "n1", "n2", "n3", "n4", "n5", "n6", "n7" ];await open(noteIds);@@ -300,31 +309,43 @@ describe("BulkActionsDialog", () => {});it("completes a relation name against the names in use", async () => {- server.get.mockImplementation(async (url) =>- (url.startsWith("attribute-names/?type=relation&query=au") ? [ "author" ] : []));- buildNote({ id: "_bulkAction", title: "Bulk action", "#action": JSON.stringify({ name: "deleteRelation" }) });- await open([ "n1" ]);+ vi.useFakeTimers();+ try {+ server.get.mockImplementation(async (url) =>+ (url.startsWith("attribute-names/?type=relation&query=au") ? [ "author" ] : []));+ buildNote({ id: "_bulkAction", title: "Bulk action", "#action": JSON.stringify({ name: "deleteRelation" }) });+ await open([ "n1" ]);- const suggestions = await typeAndSuggest(labelNameInputs()[0], "au");- expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "author" ]);+ const suggestions = await typeAndSuggest(labelNameInputs()[0], "au");+ expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "author" ]);- await act(async () => suggestions[0].click());- expect(labelNameInputs()[0].value).toBe("author");+ await act(async () => suggestions[0].click());+ expect(labelNameInputs()[0].value).toBe("author");+ expect(await savedAction()).toStrictEqual({ name: "deleteRelation", relationName: "author" });+ } finally {+ vi.useRealTimers();+ }});it("completes a label value against the values the label already has", async () => {- server.get.mockImplementation(async (url) =>- (url === "attribute-values/status" ? [ "done", "doing", "todo" ] : []));- buildNote({- id: "_bulkAction", title: "Bulk action",- "#action": JSON.stringify({ name: "updateLabelValue", labelName: "status" })- });- await open([ "n1" ]);+ vi.useFakeTimers();+ try {+ server.get.mockImplementation(async (url) =>+ (url === "attribute-values/status" ? [ "done", "doing", "todo" ] : []));+ buildNote({+ id: "_bulkAction", title: "Bulk action",+ "#action": JSON.stringify({ name: "updateLabelValue", labelName: "status" })+ });+ await open([ "n1" ]);- const valueInput = container.querySelector<HTMLInputElement>(- ".bulk-existing-action-list .bulk-action-fields input:not(.bulk-action-name)");- const suggestions = await typeAndSuggest(valueInput ?? undefined, "doi");- expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "doing" ]);+ const valueInput = container.querySelector<HTMLInputElement>(+ ".bulk-existing-action-list .bulk-action-fields input:not(.bulk-action-name)");+ const suggestions = await typeAndSuggest(valueInput ?? undefined, "doi");+ expect(suggestions.map((li) => li.textContent)).toStrictEqual([ "doing" ]);+ expect(await savedAction()).toStrictEqual({ name: "updateLabelValue", labelName: "status", labelValue: "doi" });+ } finally {+ vi.useRealTimers();+ }});it("saves an action only when its fields change", async () => {