MCPcopy Create free account
hub / github.com/TanStack/db / createTestOfflineEnvironment

Function createTestOfflineEnvironment

packages/offline-transactions/tests/harness.ts:132–265  ·  view source on GitHub ↗
(
  options: TestOfflineEnvironmentOptions = {},
)

Source from the content-addressed store, hash-verified

130}
131
132export function createTestOfflineEnvironment(
133 options: TestOfflineEnvironmentOptions = {},
134): {
135 executor: ReturnType<typeof startOfflineExecutor>
136 storage: FakeStorageAdapter
137 collection: Collection<TestItem, string, {}>
138 mutationFnName: string
139 mutationCalls: Array<OfflineMutationFnParams & { attempt: number }>
140 waitForLeader: () => Promise<void>
141 leader: FakeLeaderElection
142 serverState: Map<string, TestItem>
143 applyMutations: (mutations: Array<PendingMutation<TestItem>>) => void
144} {
145 const mutationFnName = options.mutationFnName ?? `syncData`
146 const storage = options.storage ?? new FakeStorageAdapter()
147 const mutationCalls: Array<OfflineMutationFnParams & { attempt: number }> = []
148 const attemptCounter = new Map<string, number>()
149
150 const { collection, controller } = createDefaultCollection()
151 const serverState = new Map<string, TestItem>()
152
153 const applyMutations = (mutations: Array<PendingMutation<TestItem>>) => {
154 controller.begin()
155
156 for (const mutation of mutations) {
157 switch (mutation.type) {
158 case `insert`: {
159 const value = mutation.modified
160 serverState.set(value.id, value)
161 controller.write({ type: `insert`, value })
162 break
163 }
164 case `update`: {
165 const value = mutation.modified
166 serverState.set(value.id, value)
167 controller.write({ type: `update`, value })
168 break
169 }
170 case `delete`: {
171 const original = mutation.original as TestItem | undefined
172 const fallbackIdFromKey =
173 (typeof mutation.key === `string` ? mutation.key : undefined) ??
174 mutation.globalKey.split(`:`).pop()
175 const id = original?.id ?? fallbackIdFromKey
176
177 if (!id) {
178 throw new Error(
179 `Unable to determine id for delete mutation ${mutation.globalKey}`,
180 )
181 }
182
183 const previousValue = serverState.get(id)
184 serverState.delete(id)
185
186 const emittedValue: TestItem = previousValue ?? {
187 id,
188 value: original?.value ?? ``,
189 completed: original?.completed ?? false,

Callers 2

Calls 2

createDefaultCollectionFunction · 0.85
startOfflineExecutorFunction · 0.50

Tested by

no test coverage detected