Skip to content
Source
Sign inSign up
Open History to see the last commit

composerMentionDrag.test.ts

78 lines2.5 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import {
4 COMPOSER_MENTION_DRAG_TYPE,
5 type ComposerMentionDropHost,
6 makeComposerMentionDragHandlers,
7} from "./composerMentionDrag.ts";
8​
9const makeDragEvent = (options?: {
10 mention?: string;
11 types?: ReadonlyArray<string>;
12 format?: string;
13}) => {
14 const mention = options?.mention ?? "[index.md](docs/index.md)";
15 const format = options?.format ?? COMPOSER_MENTION_DRAG_TYPE;
16 const calls: Array<string> = [];
17 const event = {
18 dataTransfer: {
19 types: options?.types ?? [format, "text/plain"],
20 getData: (requested: string) => (requested === format ? mention : ""),
21 dropEffect: "none",
22 },
23 nativeEvent: {
24 stopPropagation: () => void calls.push("nativeStopPropagation"),
25 },
26 preventDefault: () => void calls.push("preventDefault"),
27 stopPropagation: () => void calls.push("stopPropagation"),
28 };
29 return { event, calls };
30};
31​
32const makeHost = (insertResult = true) => {
33 const log: Array<string> = [];
34 const host: ComposerMentionDropHost = {
35 insertMentionAtEnd: (text) => {
36 log.push(`insert:${text}`);
37 return insertResult;
38 },
39 insertPullRequestAtEnd: (pullRequest) => {
40 log.push(`pr:${pullRequest.number}`);
41 return insertResult;
42 },
43 setDragActive: (active) => void log.push(`active:${active}`),
44 onInsertRejected: () => void log.push("rejected"),
45 };
46 return { host, log };
47};
48​
49describe("makeComposerMentionDragHandlers", () => {
50 it("leaves drags without the mention payload alone", () => {
51 const { host, log } = makeHost();
52 const handlers = makeComposerMentionDragHandlers(host);
53 const { event, calls } = makeDragEvent({ types: ["Files"] });
54 handlers.onDragEnter(event);
55 handlers.onDragOver(event);
56 handlers.onDrop(event);
57 expect(calls).toEqual([]);
58 expect(log).toEqual([]);
59 });
60​
61 it("stops the native event too, not just the synthetic one", () => {
62 const { host } = makeHost();
63 const handlers = makeComposerMentionDragHandlers(host);
64 const { event, calls } = makeDragEvent();
65 handlers.onDrop(event);
66 expect(calls).toContain("preventDefault");
67 expect(calls).toContain("stopPropagation");
68 expect(calls).toContain("nativeStopPropagation");
69 });
70​
71 it('answers dragover with the "move" effect the tree allows', () => {
72 const { host } = makeHost();
73 const handlers = makeComposerMentionDragHandlers(host);
74 const { event } = makeDragEvent();
75 handlers.onDragOver(event);
76 expect(event.dataTransfer.dropEffect).toBe("move");
77 });
78});