mattermost-mobile-upstream/app/actions/websocket/index.test.ts
Elias Nahum b6fc85385e
Migrate from RNN to Expo router (#9402)
* Expo-Router partial migration

* Full migration to ExpoRouter

* i18n

* add tests

* Merge branch 'main' into expo-router

* align package-lock.json

* i18n

* feedback review

* feedback review

* android 16kb patch

* Refactored emoji picker keyboard using a state machine

* set android launch_screen in drawables instead of layout

* no conditional hooks

* remove duplicate call in test

* address feedback

* fix tests and address more feedback

* small corrections

* small corrections

* small corrections

* fix draft test

* fix draft test 2

* remove navigateToScreen in draff test

* fix nit in draft test

* remove doc file

* Feedback review

* fix snapshots

* improve test coverage

* fix i18n

* Upgrade t o RN 0.83, other dependencies and new architecture (#9737)

* library hardware-keyboard android new arch support fix

* library rnshare android new arch support fix

* library rnutils android new arch support fix

* library secure-pdf-viewer add new arch support

* Enable new arch on iOS and Android

* update emm and network client with new-arch

* update intune to use new-arch

* performance improvements

* fix tests

* fixes post input on new-arch

* fix android emoji picker

* remove doc

* run tests in parallel

* upgrade to React Native 0.83.9, Expo 55, React 19, and New Architecture

Bumps core dependencies to their latest versions:
- React Native 0.77.3 → 0.83.9 (New Architecture enabled)
- React 18.3.1 → 19.2.5
- Expo SDK 52 → 55
- TypeScript 5.8.3 → 6.0.3
- Node.js 22.16.0 → 24.15.0
- react-native-reanimated 3.17.3 → 4.3.0
- Various other dependency upgrades

Migrates iOS AppDelegate from Objective-C to Swift, updates Android
build tooling and Gradle wrapper, refreshes all patches and font
assets, and adapts app code, tests, and CI workflows throughout for
API changes introduced by these upgrades.

* remove unecessary dismissKeyboard in notifications

* feedback review

* update intune to use new-arch

* update @mattermost deps

* missing i18n string

* additional fixes

* update fastlane
2026-05-13 17:44:55 +08:00

223 lines
9.3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {DeviceEventEmitter} from 'react-native';
import {markChannelAsViewed} from '@actions/local/channel';
import {dataRetentionCleanup, expiredBoRPostCleanup} from '@actions/local/systems';
import {markChannelAsRead} from '@actions/remote/channel';
import {entry, handleEntryAfterLoadNavigation} from '@actions/remote/entry/common';
import {deferredAppEntryActions} from '@actions/remote/entry/deferred';
import {fetchPostsForChannel, fetchPostThread} from '@actions/remote/post';
import {openAllUnreadChannels} from '@actions/remote/preference';
import {loadConfigAndCalls} from '@calls/actions/calls';
import {isSupportedServerCalls} from '@calls/utils';
import DatabaseManager from '@database/manager';
import AppsManager from '@managers/apps_manager';
import {handlePlaybookReconnect} from '@playbooks/actions/websocket/reconnect';
import {getActiveServerUrl} from '@queries/app/servers';
import {getLastPostInThread} from '@queries/servers/post';
import {getConfig, getCurrentChannelId, getCurrentTeamId, setLastFullSync} from '@queries/servers/system';
import {getIsCRTEnabled} from '@queries/servers/thread';
import {getCurrentUser} from '@queries/servers/user';
import EphemeralStore from '@store/ephemeral_store';
import {NavigationStore} from '@store/navigation_store';
import TestHelper from '@test/test_helper';
import {handleFirstConnect, handleReconnect} from './index';
jest.mock('@actions/local/channel');
jest.mock('@actions/local/systems');
jest.mock('@actions/remote/channel');
jest.mock('@actions/remote/entry/common');
jest.mock('@actions/remote/entry/deferred');
jest.mock('@actions/remote/post');
jest.mock('@actions/remote/scheduled_post');
jest.mock('@actions/remote/preference');
jest.mock('@actions/remote/user');
jest.mock('@calls/actions/calls');
jest.mock('@calls/utils');
jest.mock('@database/manager');
jest.mock('@managers/apps_manager');
jest.mock('@queries/app/servers');
jest.mock('@queries/servers/post');
jest.mock('@queries/servers/system');
jest.mock('@queries/servers/thread');
jest.mock('@queries/servers/user');
jest.mock('@store/ephemeral_store');
jest.mock('@store/navigation_store');
jest.mock('@store/team_load_store');
jest.mock('@utils/helpers', () => ({
isTablet: jest.fn().mockReturnValue(false),
}));
jest.mock('@playbooks/actions/websocket/reconnect');
describe('WebSocket Index Actions', () => {
const serverUrl = 'baseHandler.test.com';
const currentUserId = 'current-user-id';
const currentTeamId = 'current-team-id';
const currentChannelId = 'current-channel-id';
const groupLabel = 'DeepLink';
beforeEach(async () => {
jest.clearAllMocks();
jest.spyOn(DeviceEventEmitter, 'emit');
await DatabaseManager.init([serverUrl]);
DatabaseManager.serverDatabases[serverUrl] = {
operator: {
database: {},
batchRecords: jest.fn(),
},
} as any;
DatabaseManager.getServerDatabaseAndOperator = jest.fn().mockReturnValue({
database: {},
operator: {
batchRecords: jest.fn(),
},
});
});
describe('handleFirstConnect', () => {
it('should handle first connection successfully', async () => {
const mockEntryData = {
models: [],
initialTeamId: currentTeamId,
initialChannelId: currentChannelId,
prefData: {preferences: []},
teamData: {memberships: [], teams: []},
chData: {memberships: [], channels: []},
gmConverted: false,
};
jest.mocked(entry).mockResolvedValue(mockEntryData);
jest.mocked(getCurrentUser).mockResolvedValue(TestHelper.fakeUserModel({
id: currentUserId,
locale: 'en',
}));
jest.mocked(getConfig).mockResolvedValue({Version: '9.0.0'} as ClientConfig);
jest.mocked(isSupportedServerCalls).mockReturnValue(true);
const error = await handleFirstConnect(serverUrl, groupLabel);
expect(error).toBeUndefined();
expect(entry).toHaveBeenCalled();
expect(handleEntryAfterLoadNavigation).toHaveBeenCalled();
expect(setLastFullSync).toHaveBeenCalled();
expect(loadConfigAndCalls).toHaveBeenCalled();
expect(deferredAppEntryActions).toHaveBeenCalled();
expect(handlePlaybookReconnect).toHaveBeenCalledWith(serverUrl);
});
it('should handle error when server database not found', async () => {
DatabaseManager.serverDatabases = {};
const error = await handleFirstConnect(serverUrl);
expect(error).toBeInstanceOf(Error);
expect((error as Error).message).toBe('cannot find server database');
});
});
describe('handleReconnect', () => {
beforeEach(() => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue([]);
jest.mocked(getActiveServerUrl).mockResolvedValue(serverUrl);
jest.mocked(getCurrentChannelId).mockResolvedValue(currentChannelId);
jest.mocked(getCurrentTeamId).mockResolvedValue(currentTeamId);
jest.mocked(getIsCRTEnabled).mockResolvedValue(false);
jest.mocked(EphemeralStore.getCurrentThreadId).mockReturnValue('');
jest.mocked(EphemeralStore.wasNotificationTapped).mockReturnValue(false);
});
it('should handle reconnection successfully', async () => {
const mockEntryData = {
models: [],
initialTeamId: currentTeamId,
initialChannelId: currentChannelId,
prefData: {preferences: []},
teamData: {memberships: [], teams: []},
chData: {memberships: [], channels: []},
gmConverted: false,
};
jest.mocked(entry).mockResolvedValue(mockEntryData);
jest.mocked(getCurrentUser).mockResolvedValue(TestHelper.fakeUserModel({
id: currentUserId,
locale: 'en',
}));
jest.mocked(getConfig).mockResolvedValue({Version: '9.0.0'} as ClientConfig);
jest.mocked(isSupportedServerCalls).mockReturnValue(true);
jest.mocked(getActiveServerUrl).mockResolvedValue(serverUrl);
const error = await handleReconnect(serverUrl);
expect(error).toBeUndefined();
expect(entry).toHaveBeenCalled();
expect(handleEntryAfterLoadNavigation).toHaveBeenCalled();
expect(setLastFullSync).toHaveBeenCalled();
expect(loadConfigAndCalls).toHaveBeenCalled();
expect(deferredAppEntryActions).toHaveBeenCalled();
expect(openAllUnreadChannels).toHaveBeenCalled();
expect(dataRetentionCleanup).toHaveBeenCalled();
expect(expiredBoRPostCleanup).toHaveBeenCalled();
expect(AppsManager.refreshAppBindings).toHaveBeenCalled();
expect(handlePlaybookReconnect).toHaveBeenCalledWith(serverUrl);
});
it('should fetch posts for channel screen', async () => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['channel']);
await handleReconnect(serverUrl);
expect(fetchPostsForChannel).toHaveBeenCalledWith(serverUrl, currentChannelId, false, false, 'WebSocket Reconnect');
expect(markChannelAsRead).toHaveBeenCalledWith(serverUrl, currentChannelId, false, 'WebSocket Reconnect');
expect(markChannelAsViewed).toHaveBeenCalledWith(serverUrl, currentChannelId, true);
});
it('should fetch thread posts when CRT enabled', async () => {
const threadId = 'thread-id';
const lastPost = TestHelper.fakePostModel({id: 'post-id', createAt: 123});
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['thread']);
jest.mocked(getIsCRTEnabled).mockResolvedValue(true);
jest.mocked(EphemeralStore.getCurrentThreadId).mockReturnValue(threadId);
jest.mocked(getLastPostInThread).mockResolvedValue(lastPost);
await handleReconnect(serverUrl);
expect(fetchPostThread).toHaveBeenCalledWith(
serverUrl,
threadId,
{
fromCreateAt: lastPost.createAt,
fromPost: lastPost.id,
direction: 'down',
},
false,
'WebSocket Reconnect',
);
});
it('should handle notification tapped state', async () => {
jest.mocked(NavigationStore.getScreensInStack).mockReturnValue(['channel']);
jest.mocked(EphemeralStore.wasNotificationTapped).mockReturnValue(true);
await handleReconnect(serverUrl);
expect(markChannelAsViewed).not.toHaveBeenCalled();
expect(EphemeralStore.setNotificationTapped).toHaveBeenCalledWith(false);
});
it('should handle error in entry data', async () => {
jest.mocked(entry).mockResolvedValue({error: new Error('entry error')});
const result = await handleReconnect(serverUrl);
expect(result).toBeInstanceOf(Error);
expect((result as Error).message).toBe('entry error');
});
});
});