mattermost-mobile-upstream/app/hooks/is_initial_sync.test.ts
Carlos Garcia a5ff8a0d95
Mm 68767 zero persistence (#9803)
* implements auto cache cleanup when zero persistence mode is enabled (0 value)

* uses memory flag with sqlite instead of loki when running in zero persistence mode

* renames ephemeral mode manager and improves readability

* zero persistence refactors and restore values during transitions

* fixes ephemeral mode tests

* use initializeClient instead of just initialize to wait for doReconnect to finish

that way teams loading is awaited while WS reconnects during ZPM transitions

* adds inital sync as a mount hook to prevent undesired load error states

during app cold start in ZPM mode, we need a way to load team and channels
without showing loading error during the transition. The nre is initial sync
hook allows for it

* reset started sync subject on wipe

* removes extra log lines

* resets tracked server during ephemeral mode transitions

* improvements to apply persistence mode changes when something fails

* address coderabbit PR suggestions

* add batching time logging

* throws ephemeral mode change error as is

* make deleteServerDatabaseFiles private

* inline derive persistence flag function

* return current user id when refetching current user

* support zero persistence mode server for push notifications

* adds support for zero persistence mode when sharing to mattermost

* adds clarifying comment

* catch and log error deleting files

* reorder translations to correct position

* adds some new tests to improve coverage

* ios push notification native fix

* logout when fetchMe fails with 401 status code during reconnection

* properly surface errors

* return proper false value when checking for zero persistence flag

* change comments and properly await on some operations

* prevent not set persistence mode to report as an error

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* include servers with no persistence flag set in share list

* disable button for accessibility

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* improves when to store and remove signing key from global area

* review push notification reception running in zero persistence mode

* add server to ephemeral mode manager right after login

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-07-16 13:55:45 +02:00

88 lines
3.0 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {renderHook, act} from '@testing-library/react-native';
import {BehaviorSubject, type Observable, type Subscription} from 'rxjs';
import {getHasEverStartedSyncSubject, getLoadingTeamChannelsSubject} from '@store/team_load_store';
import {useIsInitialSync} from './is_initial_sync';
jest.mock('@store/team_load_store', () => ({
getLoadingTeamChannelsSubject: jest.fn(),
getHasEverStartedSyncSubject: jest.fn(),
}));
describe('useIsInitialSync', () => {
const teamLoadingSubject = new BehaviorSubject(0);
const syncStartedSubject = new BehaviorSubject(false);
const serverUrl = 'https://example.com';
beforeEach(() => {
(getLoadingTeamChannelsSubject as jest.Mock).mockReturnValue(teamLoadingSubject);
(getHasEverStartedSyncSubject as jest.Mock).mockReturnValue(syncStartedSubject);
});
afterEach(() => {
jest.restoreAllMocks();
teamLoadingSubject.next(0);
syncStartedSubject.next(false);
});
it('should return true when sync has never started and not loading', () => {
const {result} = renderHook(() => useIsInitialSync(serverUrl));
expect(result.current).toBe(true);
});
it('should return false once sync has started and no loading is in flight', () => {
syncStartedSubject.next(true);
const {result} = renderHook(() => useIsInitialSync(serverUrl));
expect(result.current).toBe(false);
});
it('should return true while loading even if sync has previously started', () => {
syncStartedSubject.next(true);
teamLoadingSubject.next(1);
const {result} = renderHook(() => useIsInitialSync(serverUrl));
expect(result.current).toBe(true);
});
it('should update when either subject emits', () => {
const {result} = renderHook(() => useIsInitialSync(serverUrl));
expect(result.current).toBe(true);
act(() => {
syncStartedSubject.next(true);
});
expect(result.current).toBe(false);
act(() => {
teamLoadingSubject.next(1);
});
expect(result.current).toBe(true);
act(() => {
teamLoadingSubject.next(0);
});
expect(result.current).toBe(false);
});
it('should unsubscribe from both subjects on unmount', () => {
const loadingUnsub = jest.fn();
const syncStartedUnsub = jest.fn();
const fakeLoadingObservable = {
subscribe: () => ({unsubscribe: loadingUnsub} as unknown as Subscription),
} as unknown as Observable<boolean>;
jest.spyOn(teamLoadingSubject, 'pipe').mockReturnValue(fakeLoadingObservable);
jest.spyOn(syncStartedSubject, 'subscribe').mockReturnValue({unsubscribe: syncStartedUnsub} as unknown as Subscription);
const {unmount} = renderHook(() => useIsInitialSync(serverUrl));
unmount();
expect(loadingUnsub).toHaveBeenCalled();
expect(syncStartedUnsub).toHaveBeenCalled();
});
});