|
1 | 1 | import { beforeEach, describe, expect, it } from "vitest"
|
| 2 | +import { Temporal } from "temporal-polyfill" |
2 | 3 | import { createCollection } from "../../src/collection.js"
|
3 | 4 | import { createLiveQueryCollection, eq } from "../../src/query/index.js"
|
4 | 5 | import { Query } from "../../src/query/builder/index.js"
|
@@ -313,4 +314,74 @@ describe(`createLiveQueryCollection`, () => {
|
313 | 314 | // Resubscribe should not throw (would throw "Graph already finalized" without the fix)
|
314 | 315 | expect(() => liveQuery.subscribeChanges(() => {})).not.toThrow()
|
315 | 316 | })
|
| 317 | + |
| 318 | + it(`should handle temporal values correctly in live queries`, async () => { |
| 319 | + // Define a type with temporal values |
| 320 | + type Task = { |
| 321 | + id: number |
| 322 | + name: string |
| 323 | + duration: Temporal.Duration |
| 324 | + } |
| 325 | + |
| 326 | + // Initial data with temporal duration |
| 327 | + const initialTask: Task = { |
| 328 | + id: 1, |
| 329 | + name: `Test Task`, |
| 330 | + duration: Temporal.Duration.from({ hours: 1 }), |
| 331 | + } |
| 332 | + |
| 333 | + // Create a collection with temporal values |
| 334 | + const taskCollection = createCollection( |
| 335 | + mockSyncCollectionOptions<Task>({ |
| 336 | + id: `test-tasks`, |
| 337 | + getKey: (task) => task.id, |
| 338 | + initialData: [initialTask], |
| 339 | + }) |
| 340 | + ) |
| 341 | + |
| 342 | + // Create a live query collection that includes the temporal value |
| 343 | + const liveQuery = createLiveQueryCollection((q) => |
| 344 | + q.from({ task: taskCollection }) |
| 345 | + ) |
| 346 | + |
| 347 | + await liveQuery.preload() |
| 348 | + |
| 349 | + // After initial sync, the live query should see the row with the temporal value |
| 350 | + expect(liveQuery.size).toBe(1) |
| 351 | + const initialResult = liveQuery.get(1) |
| 352 | + expect(initialResult).toBeDefined() |
| 353 | + expect(initialResult!.duration).toBeInstanceOf(Temporal.Duration) |
| 354 | + expect(initialResult!.duration.hours).toBe(1) |
| 355 | + |
| 356 | + // Simulate backend change: update the temporal value to 10 hours |
| 357 | + const updatedTask: Task = { |
| 358 | + id: 1, |
| 359 | + name: `Test Task`, |
| 360 | + duration: Temporal.Duration.from({ hours: 10 }), |
| 361 | + } |
| 362 | + |
| 363 | + // Update the task in the collection (simulating backend sync) |
| 364 | + /* |
| 365 | + taskCollection.update(1, (draft) => { |
| 366 | + draft.duration = updatedTask.duration |
| 367 | + }) |
| 368 | + */ |
| 369 | + |
| 370 | + taskCollection.utils.begin() |
| 371 | + taskCollection.utils.write({ |
| 372 | + type: `update`, |
| 373 | + value: updatedTask, |
| 374 | + }) |
| 375 | + taskCollection.utils.commit() |
| 376 | + |
| 377 | + // Wait for the update to propagate |
| 378 | + // await new Promise((resolve) => setTimeout(resolve, 10)) |
| 379 | + |
| 380 | + // The live query should now contain the new temporal value |
| 381 | + const updatedResult = liveQuery.get(1) |
| 382 | + expect(updatedResult).toBeDefined() |
| 383 | + expect(updatedResult!.duration).toBeInstanceOf(Temporal.Duration) |
| 384 | + expect(updatedResult!.duration.hours).toBe(10) |
| 385 | + expect(updatedResult!.duration.total({ unit: `hours` })).toBe(10) |
| 386 | + }) |
316 | 387 | })
|
0 commit comments