|
1 | 1 | import { createComputed, createRoot } from "solid-js"; |
2 | | -import { createStaticStore } from "../src"; |
| 2 | +import { createStaticStore, handleDiffArray } from "../src"; |
3 | 3 | import { suite } from "uvu"; |
4 | 4 | import * as assert from "uvu/assert"; |
5 | 5 |
|
@@ -63,3 +63,85 @@ tss("individual keys only update when changed", () => |
63 | 63 | // ); |
64 | 64 |
|
65 | 65 | tss.run(); |
| 66 | + |
| 67 | +const da = suite("handleDiffArray"); |
| 68 | + |
| 69 | +da("handleAdded called for new array", () => { |
| 70 | + const a: string[] = []; |
| 71 | + const b = ["foo", "bar", "baz", "hello", "world"]; |
| 72 | + const captured: any[] = []; |
| 73 | + handleDiffArray( |
| 74 | + b, |
| 75 | + a, |
| 76 | + item => { |
| 77 | + captured.push(item); |
| 78 | + }, |
| 79 | + () => { |
| 80 | + throw "Should never run"; |
| 81 | + } |
| 82 | + ); |
| 83 | + assert.is(captured.length, 5); |
| 84 | + assert.ok(captured.includes("foo")); |
| 85 | + assert.ok(captured.includes("bar")); |
| 86 | + assert.ok(captured.includes("baz")); |
| 87 | + assert.ok(captured.includes("hello")); |
| 88 | + assert.ok(captured.includes("world")); |
| 89 | +}); |
| 90 | + |
| 91 | +da("handleRemoved for cleared array", () => { |
| 92 | + const a = ["foo", "bar", "baz", "hello", "world"]; |
| 93 | + const b: string[] = []; |
| 94 | + const captured: any[] = []; |
| 95 | + handleDiffArray( |
| 96 | + b, |
| 97 | + a, |
| 98 | + () => { |
| 99 | + throw "Should never run"; |
| 100 | + }, |
| 101 | + item => { |
| 102 | + captured.push(item); |
| 103 | + } |
| 104 | + ); |
| 105 | + assert.is(captured.length, 5); |
| 106 | + assert.ok(captured.includes("foo")); |
| 107 | + assert.ok(captured.includes("bar")); |
| 108 | + assert.ok(captured.includes("baz")); |
| 109 | + assert.ok(captured.includes("hello")); |
| 110 | + assert.ok(captured.includes("world")); |
| 111 | +}); |
| 112 | + |
| 113 | +da("callbacks shouldn't run for same array", () => { |
| 114 | + const a = ["foo", "bar", "baz", "hello", "world"]; |
| 115 | + const b = ["foo", "bar", "baz", "hello", "world"]; |
| 116 | + handleDiffArray( |
| 117 | + b, |
| 118 | + a, |
| 119 | + () => { |
| 120 | + throw "Should never run"; |
| 121 | + }, |
| 122 | + () => { |
| 123 | + throw "Should never run"; |
| 124 | + } |
| 125 | + ); |
| 126 | +}); |
| 127 | + |
| 128 | +da("calls callbacks for added and removed items", () => { |
| 129 | + const a = ["foo", "baz", "hello"]; |
| 130 | + const b = ["foo", "bar", "hello", "world"]; |
| 131 | + const capturedAdded: any[] = []; |
| 132 | + const capturedRemoved: any[] = []; |
| 133 | + handleDiffArray( |
| 134 | + b, |
| 135 | + a, |
| 136 | + item => capturedAdded.push(item), |
| 137 | + item => capturedRemoved.push(item) |
| 138 | + ); |
| 139 | + assert.is(capturedAdded.length, 2); |
| 140 | + assert.ok(capturedAdded.includes("bar")); |
| 141 | + assert.ok(capturedAdded.includes("world")); |
| 142 | + |
| 143 | + assert.is(capturedRemoved.length, 1); |
| 144 | + assert.ok(capturedRemoved.includes("baz")); |
| 145 | +}); |
| 146 | + |
| 147 | +da.run(); |
0 commit comments