|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { gridWidgetContainersMap } from './grid-stack-render-provider'; |
| 3 | + |
| 4 | +// Mock GridStack type |
| 5 | +class MockGridStack { |
| 6 | + el: HTMLElement; |
| 7 | + constructor() { |
| 8 | + this.el = document.createElement('div'); |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +describe('GridStackRenderProvider', () => { |
| 13 | + beforeEach(() => { |
| 14 | + // Clear the WeakMap before each test |
| 15 | + gridWidgetContainersMap.constructor.prototype.clear?.call(gridWidgetContainersMap); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should store widget containers in WeakMap for each grid instance', () => { |
| 19 | + // Mock grid instances |
| 20 | + const grid1 = new MockGridStack() as any; |
| 21 | + const grid2 = new MockGridStack() as any; |
| 22 | + const widget1 = { id: '1', grid: grid1 }; |
| 23 | + const widget2 = { id: '2', grid: grid2 }; |
| 24 | + const element1 = document.createElement('div'); |
| 25 | + const element2 = document.createElement('div'); |
| 26 | + |
| 27 | + // Simulate renderCB |
| 28 | + const renderCB = (element, widget) => { |
| 29 | + if (widget.id && widget.grid) { |
| 30 | + // Get or create the widget container map for this grid instance |
| 31 | + let containers = gridWidgetContainersMap.get(widget.grid); |
| 32 | + if (!containers) { |
| 33 | + containers = new Map(); |
| 34 | + gridWidgetContainersMap.set(widget.grid, containers); |
| 35 | + } |
| 36 | + containers.set(widget.id, element); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + renderCB(element1, widget1); |
| 41 | + renderCB(element2, widget2); |
| 42 | + |
| 43 | + const containers1 = gridWidgetContainersMap.get(grid1); |
| 44 | + const containers2 = gridWidgetContainersMap.get(grid2); |
| 45 | + |
| 46 | + expect(containers1?.get('1')).toBe(element1); |
| 47 | + expect(containers2?.get('2')).toBe(element2); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should not have containers for different grid instances mixed up', () => { |
| 51 | + const grid1 = new MockGridStack() as any; |
| 52 | + const grid2 = new MockGridStack() as any; |
| 53 | + const widget1 = { id: '1', grid: grid1 }; |
| 54 | + const widget2 = { id: '2', grid: grid1 }; |
| 55 | + const widget3 = { id: '3', grid: grid2 }; |
| 56 | + const element1 = document.createElement('div'); |
| 57 | + const element2 = document.createElement('div'); |
| 58 | + const element3 = document.createElement('div'); |
| 59 | + |
| 60 | + // Simulate renderCB |
| 61 | + const renderCB = (element: HTMLElement, widget: any) => { |
| 62 | + if (widget.id && widget.grid) { |
| 63 | + let containers = gridWidgetContainersMap.get(widget.grid); |
| 64 | + if (!containers) { |
| 65 | + containers = new Map(); |
| 66 | + gridWidgetContainersMap.set(widget.grid, containers); |
| 67 | + } |
| 68 | + containers.set(widget.id, element); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
| 72 | + renderCB(element1, widget1); |
| 73 | + renderCB(element2, widget2); |
| 74 | + renderCB(element3, widget3); |
| 75 | + |
| 76 | + const containers1 = gridWidgetContainersMap.get(grid1); |
| 77 | + const containers2 = gridWidgetContainersMap.get(grid2); |
| 78 | + |
| 79 | + // Grid1 should have widgets 1 and 2 |
| 80 | + expect(containers1?.size).toBe(2); |
| 81 | + expect(containers1?.get('1')).toBe(element1); |
| 82 | + expect(containers1?.get('2')).toBe(element2); |
| 83 | + expect(containers1?.get('3')).toBeUndefined(); |
| 84 | + |
| 85 | + // Grid2 should only have widget 3 |
| 86 | + expect(containers2?.size).toBe(1); |
| 87 | + expect(containers2?.get('3')).toBe(element3); |
| 88 | + expect(containers2?.get('1')).toBeUndefined(); |
| 89 | + expect(containers2?.get('2')).toBeUndefined(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should clean up when grid instance is deleted from WeakMap', () => { |
| 93 | + const grid = new MockGridStack() as any; |
| 94 | + const widget = { id: '1', grid }; |
| 95 | + const element = document.createElement('div'); |
| 96 | + |
| 97 | + // Add to WeakMap |
| 98 | + const containers = new Map<string, HTMLElement>(); |
| 99 | + containers.set(widget.id, element); |
| 100 | + gridWidgetContainersMap.set(grid, containers); |
| 101 | + |
| 102 | + // Verify it exists |
| 103 | + expect(gridWidgetContainersMap.has(grid)).toBe(true); |
| 104 | + |
| 105 | + // Delete from WeakMap |
| 106 | + gridWidgetContainersMap.delete(grid); |
| 107 | + |
| 108 | + // Verify it's gone |
| 109 | + expect(gridWidgetContainersMap.has(grid)).toBe(false); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should handle multiple widgets in the same grid', () => { |
| 113 | + const grid = new MockGridStack() as any; |
| 114 | + const widgets = [ |
| 115 | + { id: '1', grid }, |
| 116 | + { id: '2', grid }, |
| 117 | + { id: '3', grid }, |
| 118 | + ]; |
| 119 | + const elements = widgets.map(() => document.createElement('div')); |
| 120 | + |
| 121 | + // Simulate renderCB for all widgets |
| 122 | + widgets.forEach((widget, index) => { |
| 123 | + const element = elements[index]; |
| 124 | + if (widget.id && widget.grid) { |
| 125 | + let containers = gridWidgetContainersMap.get(widget.grid); |
| 126 | + if (!containers) { |
| 127 | + containers = new Map(); |
| 128 | + gridWidgetContainersMap.set(widget.grid, containers); |
| 129 | + } |
| 130 | + containers.set(widget.id, element); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + const containers = gridWidgetContainersMap.get(grid); |
| 135 | + expect(containers?.size).toBe(3); |
| 136 | + expect(containers?.get('1')).toBe(elements[0]); |
| 137 | + expect(containers?.get('2')).toBe(elements[1]); |
| 138 | + expect(containers?.get('3')).toBe(elements[2]); |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
0 commit comments