context_drop 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -317,3 +317,502 @@ describe("ContextDrop Frontend DOM Sanitizer & Snapshot Capture", () => {
317
317
  assert.equal(selectedRef, "#submit-order");
318
318
  });
319
319
  });
320
+
321
+ interface MockListenerEntry {
322
+ listener: (ev: MockUiEvent) => void;
323
+ capture: boolean;
324
+ }
325
+
326
+ class MockUiEvent {
327
+ public readonly type: string;
328
+ public readonly button: number;
329
+ public readonly clientX: number;
330
+ public readonly clientY: number;
331
+ public target: unknown = null;
332
+ public currentTarget: unknown = null;
333
+ public defaultPrevented: boolean = false;
334
+ public propagationStopped: boolean = false;
335
+
336
+ constructor(
337
+ type: string,
338
+ init: { button?: number; clientX?: number; clientY?: number } = {}
339
+ ) {
340
+ this.type = type;
341
+ this.button = init.button ?? 0;
342
+ this.clientX = init.clientX ?? 0;
343
+ this.clientY = init.clientY ?? 0;
344
+ }
345
+
346
+ public preventDefault(): void {
347
+ this.defaultPrevented = true;
348
+ }
349
+
350
+ public stopPropagation(): void {
351
+ this.propagationStopped = true;
352
+ }
353
+ }
354
+
355
+ class MockClassList {
356
+ private classes: Set<string> = new Set();
357
+
358
+ public syncFromString(raw: string): void {
359
+ this.classes.clear();
360
+ for (const token of raw.split(/\s+/)) {
361
+ if (token) this.classes.add(token);
362
+ }
363
+ }
364
+
365
+ public toString(): string {
366
+ return Array.from(this.classes).join(" ");
367
+ }
368
+
369
+ public add(...tokens: string[]): void {
370
+ for (const token of tokens) {
371
+ if (token) this.classes.add(token);
372
+ }
373
+ }
374
+
375
+ public remove(...tokens: string[]): void {
376
+ for (const token of tokens) {
377
+ this.classes.delete(token);
378
+ }
379
+ }
380
+
381
+ public toggle(token: string, force?: boolean): boolean {
382
+ const shouldHave = force !== undefined ? force : !this.classes.has(token);
383
+ if (shouldHave) {
384
+ this.classes.add(token);
385
+ } else {
386
+ this.classes.delete(token);
387
+ }
388
+ return shouldHave;
389
+ }
390
+
391
+ public contains(token: string): boolean {
392
+ return this.classes.has(token);
393
+ }
394
+ }
395
+
396
+ class MockDomElement {
397
+ public readonly nodeType: number = 1;
398
+ public readonly tagName: string;
399
+ public readonly classList: MockClassList = new MockClassList();
400
+ public readonly style: Record<string, string> = { cssText: "", left: "", top: "", cursor: "" };
401
+ public parentElement: MockDomElement | null = null;
402
+ public parentNode: MockDomElement | null = null;
403
+ public childNodes: MockDomElement[] = [];
404
+ public hidden: boolean = false;
405
+ public type: string = "";
406
+ public title: string = "";
407
+ public textContent: string = "";
408
+ public placeholder: string = "";
409
+ public value: string = "";
410
+ public disabled: boolean = false;
411
+ public multiple: boolean = false;
412
+ public accept: string = "";
413
+ public scrollTop: number = 0;
414
+ public scrollHeight: number = 0;
415
+ private rawHtml: string = "";
416
+ private attrs: Map<string, string> = new Map();
417
+ private listeners: Map<string, MockListenerEntry[]> = new Map();
418
+ private rect: { left: number; top: number; width: number; height: number } = {
419
+ left: 20,
420
+ top: 20,
421
+ width: 220,
422
+ height: 40
423
+ };
424
+
425
+ constructor(tagName: string) {
426
+ this.tagName = tagName.toUpperCase();
427
+ }
428
+
429
+ public get className(): string {
430
+ return this.classList.toString();
431
+ }
432
+
433
+ public set className(val: string) {
434
+ this.classList.syncFromString(val);
435
+ this.attrs.set("class", val);
436
+ }
437
+
438
+ public get innerHTML(): string {
439
+ return this.rawHtml;
440
+ }
441
+
442
+ public set innerHTML(html: string) {
443
+ this.rawHtml = html;
444
+ this.childNodes = [];
445
+ if (html.includes("<svg")) {
446
+ this.appendChild(new MockDomElement("svg"));
447
+ }
448
+ const spanMatches = html.matchAll(/<span(?:\s+class="([^"]*)")?>([^<]*)<\/span>/g);
449
+ for (const match of spanMatches) {
450
+ const span = new MockDomElement("span");
451
+ if (match[1]) span.className = match[1];
452
+ span.textContent = match[2] || "";
453
+ this.appendChild(span);
454
+ }
455
+ }
456
+
457
+ public setAttribute(name: string, value: string): void {
458
+ this.attrs.set(name, value);
459
+ if (name === "class") {
460
+ this.classList.syncFromString(value);
461
+ }
462
+ }
463
+
464
+ public getAttribute(name: string): string | null {
465
+ return this.attrs.get(name) ?? null;
466
+ }
467
+
468
+ public hasAttribute(name: string): boolean {
469
+ return this.attrs.has(name);
470
+ }
471
+
472
+ public appendChild(child: MockDomElement): MockDomElement {
473
+ child.parentElement = this;
474
+ child.parentNode = this;
475
+ this.childNodes.push(child);
476
+ return child;
477
+ }
478
+
479
+ public removeChild(child: MockDomElement): MockDomElement {
480
+ const idx = this.childNodes.indexOf(child);
481
+ if (idx !== -1) {
482
+ this.childNodes.splice(idx, 1);
483
+ child.parentElement = null;
484
+ child.parentNode = null;
485
+ }
486
+ return child;
487
+ }
488
+
489
+ public contains(other: unknown): boolean {
490
+ let cur = other as MockDomElement | null;
491
+ while (cur) {
492
+ if (cur === this) return true;
493
+ cur = cur.parentNode;
494
+ }
495
+ return false;
496
+ }
497
+
498
+ public matches(selector: string): boolean {
499
+ if (selector.startsWith(".")) {
500
+ return this.classList.contains(selector.slice(1));
501
+ }
502
+ if (selector.startsWith("#")) {
503
+ return this.getAttribute("id") === selector.slice(1);
504
+ }
505
+ const attrMatch = selector.match(/^\[([a-zA-Z0-9_-]+)(?:=['"]([^'"]*)['"])?\]$/);
506
+ if (attrMatch) {
507
+ if (!this.hasAttribute(attrMatch[1])) return false;
508
+ return attrMatch[2] === undefined || this.getAttribute(attrMatch[1]) === attrMatch[2];
509
+ }
510
+ return this.tagName.toLowerCase() === selector.toLowerCase();
511
+ }
512
+
513
+ public closest(selector: string): MockDomElement | null {
514
+ let cur: MockDomElement | null = this;
515
+ while (cur) {
516
+ if (cur.matches(selector)) return cur;
517
+ cur = cur.parentElement;
518
+ }
519
+ return null;
520
+ }
521
+
522
+ public querySelector(selector: string): MockDomElement | null {
523
+ for (const child of this.childNodes) {
524
+ if (child.matches(selector)) return child;
525
+ const nested = child.querySelector(selector);
526
+ if (nested) return nested;
527
+ }
528
+ return null;
529
+ }
530
+
531
+ public getBoundingClientRect(): {
532
+ x: number;
533
+ y: number;
534
+ left: number;
535
+ top: number;
536
+ width: number;
537
+ height: number;
538
+ } {
539
+ const parsedLeft = this.style.left ? Number.parseFloat(this.style.left) : this.rect.left;
540
+ const parsedTop = this.style.top ? Number.parseFloat(this.style.top) : this.rect.top;
541
+ return {
542
+ x: parsedLeft,
543
+ y: parsedTop,
544
+ left: parsedLeft,
545
+ top: parsedTop,
546
+ width: this.rect.width,
547
+ height: this.rect.height
548
+ };
549
+ }
550
+
551
+ public focus(): void {}
552
+
553
+ public addEventListener(
554
+ type: string,
555
+ listener: (ev: MockUiEvent) => void,
556
+ options?: boolean | { capture?: boolean }
557
+ ): void {
558
+ const capture = typeof options === "boolean" ? options : Boolean(options?.capture);
559
+ const list = this.listeners.get(type) ?? [];
560
+ list.push({ listener, capture });
561
+ this.listeners.set(type, list);
562
+ }
563
+
564
+ public removeEventListener(
565
+ type: string,
566
+ listener: (ev: MockUiEvent) => void,
567
+ options?: boolean | { capture?: boolean }
568
+ ): void {
569
+ const capture = typeof options === "boolean" ? options : Boolean(options?.capture);
570
+ const list = this.listeners.get(type);
571
+ if (!list) return;
572
+ this.listeners.set(
573
+ type,
574
+ list.filter((entry) => !(entry.listener === listener && entry.capture === capture))
575
+ );
576
+ }
577
+
578
+ public dispatchEvent(ev: MockUiEvent): boolean {
579
+ if (!ev.target) {
580
+ ev.target = this;
581
+ }
582
+ const path: MockDomElement[] = [];
583
+ let cur: MockDomElement | null = this;
584
+ while (cur) {
585
+ path.push(cur);
586
+ cur = cur.parentElement;
587
+ }
588
+
589
+ for (let i = path.length - 1; i >= 0 && !ev.propagationStopped; i -= 1) {
590
+ const node = path[i];
591
+ ev.currentTarget = node;
592
+ for (const entry of [...(node.listeners.get(ev.type) ?? [])]) {
593
+ if (entry.capture) {
594
+ entry.listener(ev);
595
+ }
596
+ }
597
+ }
598
+
599
+ for (let i = 0; i < path.length && !ev.propagationStopped; i += 1) {
600
+ const node = path[i];
601
+ ev.currentTarget = node;
602
+ for (const entry of [...(node.listeners.get(ev.type) ?? [])]) {
603
+ if (!entry.capture) {
604
+ entry.listener(ev);
605
+ }
606
+ }
607
+ }
608
+
609
+ return !ev.defaultPrevented;
610
+ }
611
+ }
612
+
613
+ function withMountedWidget(
614
+ fn: (ctx: {
615
+ widget: ContextDropWidgetElement;
616
+ shadowRoot: MockDomElement;
617
+ mockWindow: MockDomElement & { innerWidth: number; innerHeight: number; location: { href: string } };
618
+ }) => void | Promise<void>
619
+ ): Promise<void> {
620
+ const g = globalThis as Record<string, unknown>;
621
+ const prevWindow = g.window;
622
+ const prevDocument = g.document;
623
+
624
+ const mockWindow = Object.assign(new MockDomElement("window"), {
625
+ innerWidth: 1280,
626
+ innerHeight: 800,
627
+ location: { href: "http://localhost:3000/orders" }
628
+ });
629
+ const mockBody = new MockDomElement("body");
630
+ const mockDocument = {
631
+ body: mockBody,
632
+ createElement: (tag: string) => new MockDomElement(tag)
633
+ };
634
+
635
+ g.window = mockWindow;
636
+ g.document = mockDocument;
637
+
638
+ const shadowRoot = new MockDomElement("shadow-root");
639
+ const widget = new ContextDropWidgetElement();
640
+ const hostShim = widget as unknown as {
641
+ attachShadow: () => ShadowRoot;
642
+ getAttribute: (name: string) => string | null;
643
+ };
644
+ hostShim.attachShadow = () => shadowRoot as unknown as ShadowRoot;
645
+ hostShim.getAttribute = () => null;
646
+
647
+ widget.connectedCallback();
648
+
649
+ return Promise.resolve()
650
+ .then(() => fn({ widget, shadowRoot, mockWindow }))
651
+ .finally(() => {
652
+ widget.disconnectedCallback();
653
+ if (prevWindow === undefined) {
654
+ delete g.window;
655
+ } else {
656
+ g.window = prevWindow;
657
+ }
658
+ if (prevDocument === undefined) {
659
+ delete g.document;
660
+ } else {
661
+ g.document = prevDocument;
662
+ }
663
+ });
664
+ }
665
+
666
+ describe("ContextDrop Floating Dock & Drag Handle Interactions", () => {
667
+ it("activates Pin and ContextDrop floating dock buttons on pointer click (including nested label spans)", async () => {
668
+ await withMountedWidget(({ shadowRoot, mockWindow }) => {
669
+ const pinDock = shadowRoot.querySelector(".cd-pin-dock");
670
+ const floatingPinBtn = shadowRoot.querySelector(".cd-floating-pin-btn");
671
+ const triggerBtn = shadowRoot.querySelector(".cd-trigger");
672
+ const composer = shadowRoot.querySelector(".cd-composer");
673
+ const compactBar = shadowRoot.querySelector(".cd-compact-bar");
674
+
675
+ assert.ok(pinDock && floatingPinBtn && triggerBtn && composer && compactBar);
676
+ assert.equal(composer.hidden, true);
677
+ assert.equal(compactBar.hidden, true);
678
+ assert.equal(pinDock.hidden, false);
679
+
680
+ // Click nested <span>ContextDrop</span> inside .cd-trigger
681
+ const triggerSpan = triggerBtn.querySelector("span") ?? triggerBtn;
682
+ triggerSpan.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 40, clientY: 30 }));
683
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { button: 0, clientX: 40, clientY: 30 }));
684
+ triggerSpan.dispatchEvent(new MockUiEvent("click", { button: 0, clientX: 40, clientY: 30 }));
685
+
686
+ assert.equal(composer.hidden, false);
687
+ assert.equal(pinDock.hidden, true);
688
+ assert.equal(triggerBtn.getAttribute("aria-expanded"), "true");
689
+
690
+ // Close composer to return to floating dock
691
+ const closeBtn = shadowRoot.querySelector("[aria-label='Close feedback composer']");
692
+ assert.ok(closeBtn);
693
+ closeBtn.dispatchEvent(new MockUiEvent("click"));
694
+ assert.equal(composer.hidden, true);
695
+ assert.equal(pinDock.hidden, false);
696
+
697
+ // Click nested <span>Pin</span> inside .cd-floating-pin-btn
698
+ const pinSpan = floatingPinBtn.querySelector("span") ?? floatingPinBtn;
699
+ pinSpan.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 30, clientY: 30 }));
700
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { button: 0, clientX: 30, clientY: 30 }));
701
+ pinSpan.dispatchEvent(new MockUiEvent("click", { button: 0, clientX: 30, clientY: 30 }));
702
+
703
+ assert.equal(compactBar.hidden, false);
704
+ assert.equal(floatingPinBtn.getAttribute("aria-pressed"), "true");
705
+ });
706
+ });
707
+
708
+ it("repositions the dock when dragging >5px from Pin or ContextDrop buttons and suppresses synthetic click", async () => {
709
+ await withMountedWidget(({ shadowRoot, mockWindow }) => {
710
+ const root = shadowRoot.querySelector(".cd-root");
711
+ const pinDock = shadowRoot.querySelector(".cd-pin-dock");
712
+ const floatingPinBtn = shadowRoot.querySelector(".cd-floating-pin-btn");
713
+ const triggerBtn = shadowRoot.querySelector(".cd-trigger");
714
+ const composer = shadowRoot.querySelector(".cd-composer");
715
+ const compactBar = shadowRoot.querySelector(".cd-compact-bar");
716
+
717
+ assert.ok(root && pinDock && floatingPinBtn && triggerBtn && composer && compactBar);
718
+
719
+ // Drag >5px starting from .cd-floating-pin-btn
720
+ floatingPinBtn.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 50, clientY: 50 }));
721
+ mockWindow.dispatchEvent(new MockUiEvent("pointermove", { clientX: 150, clientY: 120 }));
722
+ assert.equal(pinDock.classList.contains("is-dragging"), true);
723
+ assert.equal(root.classList.contains("is-free-positioned"), true);
724
+ assert.equal(root.style.left, "120px");
725
+ assert.equal(root.style.top, "90px");
726
+
727
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { clientX: 150, clientY: 120 }));
728
+ assert.equal(pinDock.classList.contains("is-dragging"), false);
729
+
730
+ // Browser dispatches synthetic click after pointerup over the dragged button
731
+ const syntheticClick = new MockUiEvent("click", { clientX: 150, clientY: 120 });
732
+ floatingPinBtn.dispatchEvent(syntheticClick);
733
+ assert.equal(syntheticClick.defaultPrevented, true);
734
+ assert.equal(compactBar.hidden, true);
735
+ assert.equal(composer.hidden, true);
736
+
737
+ // Drag >5px starting from .cd-trigger
738
+ triggerBtn.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 150, clientY: 120 }));
739
+ mockWindow.dispatchEvent(new MockUiEvent("pointermove", { clientX: 210, clientY: 180 }));
740
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { clientX: 210, clientY: 180 }));
741
+ const triggerSyntheticClick = new MockUiEvent("click", { clientX: 210, clientY: 180 });
742
+ triggerBtn.dispatchEvent(triggerSyntheticClick);
743
+ assert.equal(triggerSyntheticClick.defaultPrevented, true);
744
+ assert.equal(composer.hidden, true);
745
+ assert.equal(root.style.left, "180px");
746
+ assert.equal(root.style.top, "150px");
747
+
748
+ // Subsequent normal click on .cd-trigger opens composer as expected
749
+ triggerBtn.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 210, clientY: 180 }));
750
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { button: 0, clientX: 210, clientY: 180 }));
751
+ triggerBtn.dispatchEvent(new MockUiEvent("click", { button: 0, clientX: 210, clientY: 180 }));
752
+ assert.equal(composer.hidden, false);
753
+ });
754
+ });
755
+
756
+ it("supports keyboard activation (click without pointerdown/pointerup) on Pin and ContextDrop buttons", async () => {
757
+ await withMountedWidget(async ({ shadowRoot, mockWindow }) => {
758
+ const floatingPinBtn = shadowRoot.querySelector(".cd-floating-pin-btn");
759
+ const triggerBtn = shadowRoot.querySelector(".cd-trigger");
760
+ const composer = shadowRoot.querySelector(".cd-composer");
761
+ const compactBar = shadowRoot.querySelector(".cd-compact-bar");
762
+
763
+ assert.ok(floatingPinBtn && triggerBtn && composer && compactBar);
764
+
765
+ // Keyboard Enter/Space dispatches click directly with no pointer events
766
+ triggerBtn.dispatchEvent(new MockUiEvent("click"));
767
+ assert.equal(composer.hidden, false);
768
+
769
+ // Close via close button
770
+ const closeBtn = shadowRoot.querySelector("[aria-label='Close feedback composer']");
771
+ assert.ok(closeBtn);
772
+ closeBtn.dispatchEvent(new MockUiEvent("click"));
773
+ assert.equal(composer.hidden, true);
774
+
775
+ // Simulate a drag where pointerup occurred outside the button (no synthetic click dispatched)
776
+ floatingPinBtn.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 30, clientY: 30 }));
777
+ mockWindow.dispatchEvent(new MockUiEvent("pointermove", { clientX: 90, clientY: 90 }));
778
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { clientX: 90, clientY: 90 }));
779
+
780
+ await new Promise((resolve) => setTimeout(resolve, 5));
781
+
782
+ // Subsequent keyboard activation on floatingPinBtn still works
783
+ floatingPinBtn.dispatchEvent(new MockUiEvent("click"));
784
+ assert.equal(compactBar.hidden, false);
785
+ });
786
+ });
787
+
788
+ it("ignores nested buttons inside container drag bars (.cd-composer-header, .cd-compact-bar)", async () => {
789
+ await withMountedWidget(({ widget, shadowRoot, mockWindow }) => {
790
+ widget.openComposer();
791
+
792
+ const root = shadowRoot.querySelector(".cd-root");
793
+ const header = shadowRoot.querySelector(".cd-composer-header");
794
+ const minBtn = shadowRoot.querySelector("[aria-label='Minimize chatbox to compact bar']");
795
+ const compactBar = shadowRoot.querySelector(".cd-compact-bar");
796
+
797
+ assert.ok(root && header && minBtn && compactBar);
798
+
799
+ // Pointerdown + move on nested minimize button must NOT drag .cd-root
800
+ minBtn.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 100, clientY: 40 }));
801
+ mockWindow.dispatchEvent(new MockUiEvent("pointermove", { clientX: 250, clientY: 160 }));
802
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { clientX: 250, clientY: 160 }));
803
+ assert.equal(root.classList.contains("is-free-positioned"), false);
804
+
805
+ // Clicking nested minimize button minimizes to compact bar
806
+ minBtn.dispatchEvent(new MockUiEvent("click"));
807
+ assert.equal(compactBar.hidden, false);
808
+
809
+ // Dragging non-button area of .cd-compact-bar DOES reposition .cd-root
810
+ compactBar.dispatchEvent(new MockUiEvent("pointerdown", { button: 0, clientX: 60, clientY: 40 }));
811
+ mockWindow.dispatchEvent(new MockUiEvent("pointermove", { clientX: 140, clientY: 110 }));
812
+ mockWindow.dispatchEvent(new MockUiEvent("pointerup", { clientX: 140, clientY: 110 }));
813
+ assert.equal(root.classList.contains("is-free-positioned"), true);
814
+ assert.equal(root.style.left, "100px");
815
+ assert.equal(root.style.top, "90px");
816
+ });
817
+ });
818
+ });