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