|
| 1 | +import {vi} from "vitest" |
1 | 2 | import { |
| 3 | + Immer, |
2 | 4 | setAutoFreeze, |
3 | 5 | current, |
4 | 6 | immerable, |
@@ -244,3 +246,119 @@ function runTests(name) { |
244 | 246 | }) |
245 | 247 | }) |
246 | 248 | } |
| 249 | + |
| 250 | +// Bound Set traversal so a regression cannot hang the test process. |
| 251 | +function takeSetSnapshot(draft) { |
| 252 | + const forEach = Set.prototype.forEach |
| 253 | + const guard = vi |
| 254 | + .spyOn(Set.prototype, "forEach") |
| 255 | + .mockImplementation(function (callback, thisArg) { |
| 256 | + const size = this.size |
| 257 | + let visited = 0 |
| 258 | + return forEach.call(this, (value, key, set) => { |
| 259 | + if (++visited > size) |
| 260 | + throw new Error("Set snapshot traversal grew beyond its initial size") |
| 261 | + callback.call(thisArg, value, key, set) |
| 262 | + }) |
| 263 | + }) |
| 264 | + try { |
| 265 | + return current(draft) |
| 266 | + } finally { |
| 267 | + guard.mockRestore() |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +describe.each([true, false])( |
| 272 | + "current Set snapshots - autoFreeze=%s", |
| 273 | + autoFreeze => { |
| 274 | + const {produce} = new Immer({autoFreeze}) |
| 275 | + |
| 276 | + it("snapshots modified objects in insertion order without leaking drafts", () => { |
| 277 | + const changed = {count: 0} |
| 278 | + const untouched = {count: 10} |
| 279 | + const base = new Set([changed, "middle", untouched]) |
| 280 | + let snapshot |
| 281 | + const next = produce(base, draft => { |
| 282 | + const item = draft.values().next().value |
| 283 | + item.count = 1 |
| 284 | + snapshot = takeSetSnapshot(draft) |
| 285 | + const values = [...snapshot] |
| 286 | + expect(values).toEqual([{count: 1}, "middle", untouched]) |
| 287 | + expect(values[2]).toBe(untouched) |
| 288 | + expect(values.every(value => !isDraft(value))).toBe(true) |
| 289 | + expect(Object.isFrozen(snapshot)).toBe(false) |
| 290 | + expect(Object.isFrozen(values[0])).toBe(false) |
| 291 | + item.count = 2 |
| 292 | + expect(values[0].count).toBe(1) |
| 293 | + }) |
| 294 | + expect([...snapshot]).toEqual([{count: 1}, "middle", untouched]) |
| 295 | + expect([...next]).toEqual([{count: 2}, "middle", untouched]) |
| 296 | + expect(changed.count).toBe(0) |
| 297 | + }) |
| 298 | + |
| 299 | + it("snapshots object Sets nested in a parent", () => { |
| 300 | + const base = {items: new Set([{count: 0}])} |
| 301 | + let snapshot |
| 302 | + const next = produce(base, draft => { |
| 303 | + const item = draft.items.values().next().value |
| 304 | + item.count = 1 |
| 305 | + snapshot = takeSetSnapshot(draft) |
| 306 | + item.count = 2 |
| 307 | + }) |
| 308 | + expect([...snapshot.items]).toEqual([{count: 1}]) |
| 309 | + expect([...next.items]).toEqual([{count: 2}]) |
| 310 | + expect([...base.items]).toEqual([{count: 0}]) |
| 311 | + }) |
| 312 | + |
| 313 | + it("keeps membership snapshots independent of later additions and deletions", () => { |
| 314 | + const removed = {id: "removed"} |
| 315 | + const kept = {id: "kept"} |
| 316 | + const base = new Set([removed, kept]) |
| 317 | + let snapshot |
| 318 | + const next = produce(base, draft => { |
| 319 | + draft.delete(removed) |
| 320 | + draft.add({id: "added"}) |
| 321 | + snapshot = takeSetSnapshot(draft) |
| 322 | + expect([...snapshot]).toEqual([kept, {id: "added"}]) |
| 323 | + expect([...snapshot][0]).toBe(kept) |
| 324 | + draft.clear() |
| 325 | + }) |
| 326 | + expect([...snapshot]).toEqual([kept, {id: "added"}]) |
| 327 | + expect(next.size).toBe(0) |
| 328 | + expect([...base]).toEqual([removed, kept]) |
| 329 | + }) |
| 330 | + |
| 331 | + it("snapshots arrays stored as Set values", () => { |
| 332 | + const base = new Set([[1]]) |
| 333 | + let snapshot |
| 334 | + produce(base, draft => { |
| 335 | + const item = draft.values().next().value |
| 336 | + item.push(2) |
| 337 | + snapshot = takeSetSnapshot(draft) |
| 338 | + item.push(3) |
| 339 | + }) |
| 340 | + expect([...snapshot]).toEqual([[1, 2]]) |
| 341 | + expect([...base]).toEqual([[1]]) |
| 342 | + }) |
| 343 | + |
| 344 | + it("keeps unchanged frozen objects exactly once when adding a primitive", () => { |
| 345 | + const item = freeze({count: 0}) |
| 346 | + const base = new Set([item]) |
| 347 | + let snapshot |
| 348 | + produce(base, draft => { |
| 349 | + draft.add(1) |
| 350 | + snapshot = takeSetSnapshot(draft) |
| 351 | + }) |
| 352 | + expect([...snapshot]).toEqual([item, 1]) |
| 353 | + expect([...snapshot][0]).toBe(item) |
| 354 | + }) |
| 355 | + |
| 356 | + it("returns the original Set when its objects are only read", () => { |
| 357 | + const base = new Set([{count: 0}]) |
| 358 | + produce(base, draft => { |
| 359 | + expect(draft.values().next().value.count).toBe(0) |
| 360 | + expect(takeSetSnapshot(draft)).toBe(base) |
| 361 | + }) |
| 362 | + }) |
| 363 | + } |
| 364 | +) |
0 commit comments