Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

preloadCollection.test.ts

140 lines3.6 KB

1import { beforeEach, describe, expect, it, vi } from "vitest"
2import { preloadCollection } from "../src/useCollection"
3import type { CollectionConfig } from "@tanstack/optimistic"
4import "fake-indexeddb/auto"
5​
6describe(`preloadCollection`, () => {
7 beforeEach(() => {
8 vi.resetAllMocks()
9 })
10​
11 it(`should resolve once the first commit is done`, async () => {
12 // Create a mock sync function that will call begin, write, and commit
13 let commitFn: () => void
14​
15 const config: { id: string } & CollectionConfig = {
16 id: `test-collection`,
17 sync: {
18 sync: ({ begin, write, commit }) => {
19 // Store the commit function to call it later
20 commitFn = commit
21​
22 // Start the transaction
23 begin()
24​
25 // Write some data
26 write({
27 key: `test`,
28 value: { name: `Test Item` },
29 type: `insert`,
30 })
31​
32 // We'll call commit later in the test
33 },
34 },
35 mutationFn: {
36 persist: async () => {
37 // No-op function for testing
38 },
39 },
40 }
41​
42 // Create a preload promise
43 const preloadPromise = preloadCollection(config)
44​
45 // The promise should not resolve yet
46 let resolved = false
47 preloadPromise.then(() => {
48 resolved = true
49 })
50​
51 // Wait a tick to allow any synchronous resolution
52 await new Promise((resolve) => setTimeout(resolve, 0))
53 expect(resolved).toBe(false)
54​
55 // Now commit the transaction
56 commitFn!()
57​
58 // Wait for the promise to resolve
59 await preloadPromise
60 expect(resolved).toBe(true)
61 })
62​
63 it(`should resolve immediately if already loaded`, async () => {
64 // Create a mock sync function
65 let commitCalled = 0
66 const config: { id: string } & CollectionConfig = {
67 id: `test-collection-2`,
68 sync: {
69 sync: ({ begin, write, commit }) => {
70 begin()
71 write({
72 key: `test`,
73 value: { name: `Test Item` },
74 type: `insert`,
75 })
76 Promise.resolve().then(() => {
77 commit()
78 commitCalled++
79 })
80 },
81 },
82 mutationFn: {
83 persist: async () => {
84 // No-op function for testing
85 },
86 },
87 }
88​
89 // First call should create the collection and wait for commit
90 await preloadCollection(config)
91 expect(commitCalled).toBe(1)
92​
93 // Second call should resolve immediately because collection already exists
94 // and has committed
95 await preloadCollection(config)
96 expect(commitCalled).toBe(1) // Sync should not be called again
97 expect(commitCalled).toBe(1) // Sync should not be called again
98 })
99​
100 it(`should return the same promise for concurrent preload calls`, async () => {
101 let commitFn: () => void
102 let beginCalled = 0
103​
104 const config: { id: string } & CollectionConfig = {
105 id: `test-collection-3`,
106 sync: {
107 sync: ({ begin, write, commit }) => {
108 beginCalled++
109 commitFn = commit
110 begin()
111 write({
112 key: `test`,
113 value: { name: `Test Item` },
114 type: `insert`,
115 })
116 // Don't commit yet
117 },
118 },
119 mutationFn: {
120 persist: async () => {
121 // No-op function for testing
122 },
123 },
124 }
125​
126 // Create two preload promises
127 const promise1 = preloadCollection(config)
128 const promise2 = preloadCollection(config)
129​
130 // They should be the same promise
131 expect(promise1).toBe(promise2)
132 expect(beginCalled).toBe(1) // Sync should only be called once
133​
134 // Now commit
135 commitFn!()
136​
137 // Both promises should resolve
138 await Promise.all([promise1, promise2])
139 })
140})