mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
@@ -9,9 +9,12 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
const flush = vi.fn(() => Promise.resolve());
|
||||
const syncAll = vi.fn(() => Promise.resolve());
|
||||
const hydrate = vi.fn(() => Promise.resolve());
|
||||
const loadSettings = vi.fn(() => Promise.resolve());
|
||||
|
||||
let refetchCb: ((tripId: string) => void) | null = null;
|
||||
let preReconnect: (() => Promise<void>) | null = null;
|
||||
let settingsLoaded = false;
|
||||
let authenticated = true;
|
||||
|
||||
vi.mock('../../../src/sync/mutationQueue', () => ({
|
||||
mutationQueue: { flush: () => flush() },
|
||||
@@ -27,6 +30,12 @@ vi.mock('../../../src/api/websocket', () => ({
|
||||
vi.mock('../../../src/store/tripStore', () => ({
|
||||
useTripStore: { getState: () => ({ hydrateActiveTrip: hydrate }) },
|
||||
}));
|
||||
vi.mock('../../../src/store/settingsStore', () => ({
|
||||
useSettingsStore: { getState: () => ({ isLoaded: settingsLoaded, loadSettings }) },
|
||||
}));
|
||||
vi.mock('../../../src/store/authStore', () => ({
|
||||
useAuthStore: { getState: () => ({ isAuthenticated: authenticated }) },
|
||||
}));
|
||||
|
||||
import { registerSyncTriggers, unregisterSyncTriggers } from '../../../src/sync/syncTriggers';
|
||||
|
||||
@@ -35,8 +44,9 @@ const flushMicrotasks = async () => {
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear();
|
||||
flush.mockClear(); syncAll.mockClear(); hydrate.mockClear(); loadSettings.mockClear();
|
||||
refetchCb = null; preReconnect = null;
|
||||
settingsLoaded = false; authenticated = true;
|
||||
Object.defineProperty(navigator, 'onLine', { value: true, writable: true, configurable: true });
|
||||
});
|
||||
|
||||
@@ -72,5 +82,35 @@ describe('syncTriggers', () => {
|
||||
expect(flush).toHaveBeenCalled();
|
||||
expect(syncAll).toHaveBeenCalled();
|
||||
expect(hydrate).toHaveBeenCalledWith('7');
|
||||
// The order is the point: re-seeding Dexie before the queue drains would
|
||||
// overwrite edits that have not been sent yet.
|
||||
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(syncAll.mock.invocationCallOrder[0]);
|
||||
expect(flush.mock.invocationCallOrder[0]).toBeLessThan(hydrate.mock.invocationCallOrder[0]);
|
||||
});
|
||||
|
||||
it('online event retries the settings load when it has not yet succeeded (#1618)', async () => {
|
||||
registerSyncTriggers();
|
||||
window.dispatchEvent(new Event('online'));
|
||||
await flushMicrotasks();
|
||||
|
||||
expect(loadSettings).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not retry the settings load once it has loaded', async () => {
|
||||
settingsLoaded = true;
|
||||
registerSyncTriggers();
|
||||
window.dispatchEvent(new Event('online'));
|
||||
await flushMicrotasks();
|
||||
|
||||
expect(loadSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not retry the settings load while unauthenticated', async () => {
|
||||
authenticated = false;
|
||||
registerSyncTriggers();
|
||||
window.dispatchEvent(new Event('online'));
|
||||
await flushMicrotasks();
|
||||
|
||||
expect(loadSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user