|
| 1 | +/** |
| 2 | + * ObjectUI — useOffline auto-syncs mutations queued while ONLINE (objectui#6818) |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * The auto-sync effect was keyed `[isOnline, enabled]` with |
| 6 | + * `react-hooks/exhaustive-deps` suppressed, so its `queue.length === 0` guard |
| 7 | + * was evaluated against the queue as it stood when `isOnline` or `enabled` last |
| 8 | + * changed. `queueMutation` has never been conditional on being offline, so a |
| 9 | + * mutation queued while ALREADY online found the effect asleep: nothing |
| 10 | + * re-ran it, and only an explicit `sync()` could drain the queue. |
| 11 | + * |
| 12 | + * Nothing in this repo reaches that queue — `AppHeader.tsx` is the one in-repo |
| 13 | + * caller and it destructures `isOnline` only — so a green suite proved nothing |
| 14 | + * about this path before these pins existed. Each one below drives a real |
| 15 | + * mutation through the queue. |
| 16 | + * |
| 17 | + * Timers are faked because two of the pins are about WHEN the 100ms |
| 18 | + * stabilization timer fires, not merely whether it does. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 22 | +import { renderHook, act } from '@testing-library/react'; |
| 23 | +import { useOffline, type OfflineResult } from '../useOffline'; |
| 24 | + |
| 25 | +function queueOne(result: { current: OfflineResult }, resource: string) { |
| 26 | + act(() => { |
| 27 | + result.current.queueMutation({ operation: 'create', resource, data: { resource } }); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +/** Advance fake time and let React flush whatever the timers scheduled. */ |
| 32 | +async function advance(ms: number) { |
| 33 | + await act(async () => { |
| 34 | + await vi.advanceTimersByTimeAsync(ms); |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function setOnline(value: boolean) { |
| 39 | + Object.defineProperty(window.navigator, 'onLine', { configurable: true, value }); |
| 40 | + act(() => { |
| 41 | + window.dispatchEvent(new Event(value ? 'online' : 'offline')); |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + localStorage.clear(); |
| 47 | + setOnline(true); |
| 48 | + vi.useFakeTimers(); |
| 49 | +}); |
| 50 | + |
| 51 | +afterEach(() => { |
| 52 | + vi.useRealTimers(); |
| 53 | + setOnline(true); |
| 54 | +}); |
| 55 | + |
| 56 | +describe('useOffline — auto-sync reaches mutations queued while online (#6818)', () => { |
| 57 | + // ---- pin 1: the card's finding, stated as behaviour ---------------------- |
| 58 | + // This is the assertion the whole card is about, and it fails on the base: |
| 59 | + // the effect had already returned early against an empty queue and nothing |
| 60 | + // re-ran it when `queueMutation` made the queue non-empty. |
| 61 | + it('drains a mutation queued while already online', async () => { |
| 62 | + const { result } = renderHook(() => useOffline()); |
| 63 | + expect(result.current.isOnline).toBe(true); |
| 64 | + |
| 65 | + queueOne(result, 'account'); |
| 66 | + expect(result.current.pendingCount).toBe(1); |
| 67 | + |
| 68 | + await advance(150); |
| 69 | + |
| 70 | + expect(result.current.pendingCount).toBe(0); |
| 71 | + expect(result.current.syncState).toBe('idle'); |
| 72 | + }); |
| 73 | + |
| 74 | + // ---- pin 2: DISCRIMINATING — the timer must NOT restart per mutation ----- |
| 75 | + // The suppressed dep list existed to stop the 100ms timer restarting on every |
| 76 | + // queued mutation, and that reason is kept: the dep is the BOOLEAN |
| 77 | + // `queue.length > 0`. This pin is what tells the two shapes apart. The timer |
| 78 | + // is armed at t=0 by the first mutation; a second mutation lands at t=60. If |
| 79 | + // the effect were keyed on the queue (or on `queue.length`), it would re-run |
| 80 | + // there and re-arm for t=160, so at t=105 nothing would have drained yet. |
| 81 | + // Keyed on the boolean, the ORIGINAL timer fires at t=100 and — because |
| 82 | + // `sync` now reads the queue through the same commit-phase ref it already |
| 83 | + // read `batchSize` through — it flushes BOTH entries, not just the first. |
| 84 | + it('keeps the original timer when a second mutation is queued before it fires', async () => { |
| 85 | + const { result } = renderHook(() => useOffline()); |
| 86 | + |
| 87 | + queueOne(result, 'account'); |
| 88 | + await advance(60); |
| 89 | + expect(result.current.pendingCount).toBe(1); |
| 90 | + |
| 91 | + queueOne(result, 'contact'); |
| 92 | + expect(result.current.pendingCount).toBe(2); |
| 93 | + |
| 94 | + await advance(45); // t = 105: past the ORIGINAL 100ms deadline, short of a restarted one |
| 95 | + |
| 96 | + expect(result.current.pendingCount).toBe(0); |
| 97 | + }); |
| 98 | + |
| 99 | + // ---- pin 3: the original feature is not traded away ---------------------- |
| 100 | + // Green on the base too, deliberately: it is the regression guard for the |
| 101 | + // behaviour the narrow dep list did deliver ("sync when you come back |
| 102 | + // online"), so a future edit cannot close #6818 by breaking the reconnect. |
| 103 | + it('still auto-syncs on the offline to online transition', async () => { |
| 104 | + const { result } = renderHook(() => useOffline()); |
| 105 | + |
| 106 | + setOnline(false); |
| 107 | + expect(result.current.isOnline).toBe(false); |
| 108 | + |
| 109 | + queueOne(result, 'account'); |
| 110 | + await advance(150); |
| 111 | + // Offline: the guard is right to hold the queue. |
| 112 | + expect(result.current.pendingCount).toBe(1); |
| 113 | + |
| 114 | + setOnline(true); |
| 115 | + await advance(150); |
| 116 | + |
| 117 | + expect(result.current.pendingCount).toBe(0); |
| 118 | + }); |
| 119 | + |
| 120 | + // ---- pin 4: point 2 of the card — one call, one notion of "current" ------ |
| 121 | + // `sync` read `batchSize` through a ref (newest) and `queue` from its own |
| 122 | + // closure (a snapshot), so the two halves of a RETAINED call disagreed about |
| 123 | + // how current they were — and the auto-sync effect retains one by design. |
| 124 | + // Mirrors #6797's pin 1 ("a retained closure reads the newest batchSize") |
| 125 | + // with its missing half: a retained closure batches the newest QUEUE. |
| 126 | + it('lets a retained sync closure batch the newest queue, not its own snapshot', async () => { |
| 127 | + const { result } = renderHook(() => useOffline()); |
| 128 | + |
| 129 | + queueOne(result, 'account'); |
| 130 | + const retained = result.current.sync; |
| 131 | + |
| 132 | + queueOne(result, 'contact'); |
| 133 | + expect(result.current.pendingCount).toBe(2); |
| 134 | + |
| 135 | + await act(async () => { |
| 136 | + const settled = retained(); |
| 137 | + await vi.advanceTimersByTimeAsync(1); // the simulated round-trip, not the 100ms timer |
| 138 | + await settled; |
| 139 | + }); |
| 140 | + |
| 141 | + // On the base this is 1: `retained` closed over the one-entry queue and |
| 142 | + // flushed only that, leaving the mutation queued after it behind. |
| 143 | + expect(result.current.pendingCount).toBe(0); |
| 144 | + }); |
| 145 | +}); |
0 commit comments