|
| 1 | +/* Copyright 2024 Marimo. All rights reserved. */ |
| 2 | +import { describe, it, expect, afterEach } from "vitest"; |
| 3 | +import { reorderColumnSizes, storageFn } from "../storage"; |
| 4 | + |
| 5 | +describe("setColumnWidth", () => { |
| 6 | + const { clearStorage, getColumnWidth, setColumnWidth } = storageFn; |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + clearStorage(); |
| 10 | + }); |
| 11 | + |
| 12 | + it("should set width for an existing index", () => { |
| 13 | + // Setup initial state |
| 14 | + setColumnWidth(0, 100); |
| 15 | + setColumnWidth(1, 200); |
| 16 | + |
| 17 | + // Test |
| 18 | + setColumnWidth(0, 150); |
| 19 | + |
| 20 | + // Verify |
| 21 | + expect(getColumnWidth(0)).toBe(150); |
| 22 | + expect(getColumnWidth(1)).toBe(200); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should set width for a new index", () => { |
| 26 | + // Setup initial state |
| 27 | + setColumnWidth(0, 100); |
| 28 | + |
| 29 | + // Test |
| 30 | + setColumnWidth(1, 200); |
| 31 | + |
| 32 | + // Verify |
| 33 | + expect(getColumnWidth(0)).toBe(100); |
| 34 | + expect(getColumnWidth(1)).toBe(200); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should pad with contentWidth when setting width for out of bounds index", () => { |
| 38 | + // Setup initial state |
| 39 | + setColumnWidth(0, 100); |
| 40 | + |
| 41 | + // Test |
| 42 | + setColumnWidth(3, 300); |
| 43 | + |
| 44 | + // Verify |
| 45 | + expect(getColumnWidth(0)).toBe(100); |
| 46 | + expect(getColumnWidth(1)).toBe("contentWidth"); |
| 47 | + expect(getColumnWidth(2)).toBe("contentWidth"); |
| 48 | + expect(getColumnWidth(3)).toBe(300); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should handle empty initial state", () => { |
| 52 | + // Test |
| 53 | + setColumnWidth(2, 200); |
| 54 | + |
| 55 | + // Verify |
| 56 | + expect(getColumnWidth(0)).toBe("contentWidth"); |
| 57 | + expect(getColumnWidth(1)).toBe("contentWidth"); |
| 58 | + expect(getColumnWidth(2)).toBe(200); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should update multiple columns", () => { |
| 62 | + // Setup initial state |
| 63 | + setColumnWidth(0, 100); |
| 64 | + setColumnWidth(1, 200); |
| 65 | + setColumnWidth(2, 300); |
| 66 | + |
| 67 | + // Test |
| 68 | + setColumnWidth(0, 150); |
| 69 | + setColumnWidth(2, 350); |
| 70 | + |
| 71 | + // Verify |
| 72 | + expect(getColumnWidth(0)).toBe(150); |
| 73 | + expect(getColumnWidth(1)).toBe(200); |
| 74 | + expect(getColumnWidth(2)).toBe(350); |
| 75 | + }); |
| 76 | + |
| 77 | + it("should set contentWidth directly", () => { |
| 78 | + // Setup initial state |
| 79 | + setColumnWidth(0, 100); |
| 80 | + setColumnWidth(1, 200); |
| 81 | + |
| 82 | + // Test |
| 83 | + setColumnWidth(0, "contentWidth"); |
| 84 | + |
| 85 | + // Verify |
| 86 | + expect(getColumnWidth(0)).toBe("contentWidth"); |
| 87 | + expect(getColumnWidth(1)).toBe(200); |
| 88 | + }); |
| 89 | + |
| 90 | + it("should maintain correct widths after reordering", () => { |
| 91 | + // Setup initial state with 3 columns |
| 92 | + setColumnWidth(0, 100); |
| 93 | + setColumnWidth(1, 200); |
| 94 | + setColumnWidth(2, 300); |
| 95 | + |
| 96 | + // Reorder column 0 to position 2 |
| 97 | + reorderColumnSizes(0, 2); |
| 98 | + |
| 99 | + // Verify the widths are in the correct order after reordering |
| 100 | + expect(getColumnWidth(0)).toBe(200); // Original column 1 |
| 101 | + expect(getColumnWidth(1)).toBe(300); // Original column 2 |
| 102 | + expect(getColumnWidth(2)).toBe(100); // Original column 0 |
| 103 | + }); |
| 104 | +}); |
0 commit comments