mirror of
https://github.com/mattermost/mattermost-mobile.git
synced 2026-11-17 17:08:16 +00:00
* 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
249 lines
9.0 KiB
TypeScript
249 lines
9.0 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
import NetInfo from '@react-native-community/netinfo';
|
|
import {AppState} from 'react-native';
|
|
import {BackgroundTimer} from 'react-native-nitro-bg-timer-plus';
|
|
|
|
import {fetchStatusByIds} from '@actions/remote/user';
|
|
import {handleFirstConnect, handleReconnect} from '@actions/websocket';
|
|
import WebSocketClient from '@client/websocket';
|
|
import DatabaseManager from '@database/manager';
|
|
import {getCurrentUserId} from '@queries/servers/system';
|
|
import {queryAllUsers} from '@queries/servers/user';
|
|
import TestHelper from '@test/test_helper';
|
|
import {logError} from '@utils/log';
|
|
|
|
import WebsocketManager from './websocket_manager';
|
|
|
|
import type {ServerDatabase} from '@typings/database/database';
|
|
|
|
jest.mock('@react-native-community/netinfo');
|
|
jest.mock('react-native/Libraries/AppState/AppState');
|
|
jest.mock('@actions/local/user');
|
|
jest.mock('@actions/remote/user');
|
|
jest.mock('@actions/websocket');
|
|
jest.mock('@actions/websocket/event');
|
|
jest.mock('@client/websocket');
|
|
jest.mock('@database/manager');
|
|
jest.mock('@queries/servers/system');
|
|
jest.mock('@queries/servers/user');
|
|
jest.mock('@utils/log');
|
|
|
|
describe('WebsocketManager', () => {
|
|
let manager: typeof WebsocketManager;
|
|
let mockWebSocketClient: any;
|
|
let mockCallbacks: {[key: string]: (...args: any[]) => void};
|
|
const mockServerUrl = 'https://example.com';
|
|
const mockToken = 'mock-token';
|
|
const mockCredentials = [{serverUrl: mockServerUrl, token: mockToken} as ServerCredential];
|
|
|
|
beforeEach(async () => {
|
|
mockCallbacks = {};
|
|
await DatabaseManager.init([mockServerUrl]);
|
|
|
|
// Reset NetInfo mock
|
|
(NetInfo.fetch as jest.Mock).mockResolvedValue({isConnected: true, type: 'wifi'});
|
|
(NetInfo.addEventListener as jest.Mock).mockReturnValue(jest.fn());
|
|
|
|
// Reset AppState mock
|
|
AppState.currentState = 'active';
|
|
|
|
// Reset WebSocketClient mock
|
|
mockWebSocketClient = {
|
|
setFirstConnectCallback: jest.fn((cb) => {
|
|
mockCallbacks.firstConnect = cb;
|
|
}),
|
|
|
|
setEventCallback: jest.fn(),
|
|
setReconnectCallback: jest.fn((cb) => {
|
|
mockCallbacks.reconnect = cb;
|
|
}),
|
|
setReliableReconnectCallback: jest.fn(),
|
|
setCloseCallback: jest.fn((cb) => {
|
|
mockCallbacks.close = cb;
|
|
}),
|
|
initialize: jest.fn(),
|
|
isConnected: jest.fn().mockReturnValue(true),
|
|
close: jest.fn(),
|
|
invalidate: jest.fn(),
|
|
waitForClose: jest.fn().mockResolvedValue(undefined),
|
|
};
|
|
(WebSocketClient as jest.Mock).mockImplementation(() => mockWebSocketClient);
|
|
|
|
// Reset DatabaseManager mock
|
|
jest.spyOn(DatabaseManager, 'getServerDatabaseAndOperator').mockImplementation(() => ({
|
|
database: {},
|
|
operator: {},
|
|
} as ServerDatabase));
|
|
|
|
manager = WebsocketManager;
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await DatabaseManager.destroyServerDatabase(mockServerUrl);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
describe('init', () => {
|
|
it('should initialize correctly', async () => {
|
|
await manager.init(mockCredentials);
|
|
|
|
expect(NetInfo.fetch).toHaveBeenCalled();
|
|
expect(WebSocketClient).toHaveBeenCalledWith(mockServerUrl, mockToken, undefined);
|
|
expect(NetInfo.addEventListener).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should handle initialization error gracefully', async () => {
|
|
(DatabaseManager.getServerDatabaseAndOperator as jest.Mock).mockImplementationOnce(() => {
|
|
throw new Error('Database error');
|
|
});
|
|
|
|
await manager.init(mockCredentials);
|
|
|
|
expect(jest.mocked(logError)).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('client management', () => {
|
|
beforeEach(async () => {
|
|
await manager.init(mockCredentials);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should create and invalidate clients correctly', async () => {
|
|
const client = await manager.createClient(mockServerUrl, mockToken);
|
|
expect(client).toBeDefined();
|
|
|
|
await manager.invalidateClient(mockServerUrl);
|
|
expect(manager.getClient(mockServerUrl)).toBeUndefined();
|
|
});
|
|
|
|
it('should handle websocket state observations', () => {
|
|
const observable = manager.observeWebsocketState(mockServerUrl);
|
|
expect(observable).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('closeAll', () => {
|
|
beforeEach(async () => {
|
|
await manager.init(mockCredentials);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should close clients without calling invalidate', () => {
|
|
manager.closeAll();
|
|
|
|
expect(mockWebSocketClient.close).toHaveBeenCalledWith(true);
|
|
expect(mockWebSocketClient.invalidate).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('invalidateClient', () => {
|
|
beforeEach(async () => {
|
|
await manager.init(mockCredentials);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it('should call close, waitForClose, then invalidate in order', async () => {
|
|
const callOrder: string[] = [];
|
|
mockWebSocketClient.close.mockImplementation(() => callOrder.push('close'));
|
|
mockWebSocketClient.waitForClose.mockImplementation(() => {
|
|
callOrder.push('waitForClose');
|
|
return Promise.resolve();
|
|
});
|
|
mockWebSocketClient.invalidate.mockImplementation(() => callOrder.push('invalidate'));
|
|
|
|
await manager.invalidateClient(mockServerUrl);
|
|
|
|
expect(callOrder).toEqual(['close', 'waitForClose', 'invalidate']);
|
|
});
|
|
|
|
it('should handle missing client gracefully', async () => {
|
|
await manager.invalidateClient('https://nonexistent.com');
|
|
expect(mockWebSocketClient.close).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('connection handling', () => {
|
|
beforeEach(async () => {
|
|
await manager.init(mockCredentials);
|
|
});
|
|
|
|
it('should handle first connect correctly', async () => {
|
|
mockWebSocketClient.isConnected.mockReturnValueOnce(false);
|
|
|
|
await manager.initializeClient(mockServerUrl);
|
|
|
|
expect(mockWebSocketClient.initialize).toHaveBeenCalledWith({}, true);
|
|
expect(handleFirstConnect).toHaveBeenCalledWith(mockServerUrl, 'WebSocket Reconnect');
|
|
|
|
if (mockCallbacks.firstConnect) {
|
|
mockCallbacks.firstConnect();
|
|
}
|
|
});
|
|
|
|
it('should handle reconnect correctly', async () => {
|
|
const client = manager.getClient(mockServerUrl);
|
|
expect(client).toBeDefined();
|
|
|
|
mockCallbacks.reconnect();
|
|
expect(handleReconnect).toHaveBeenCalledWith(mockServerUrl);
|
|
});
|
|
});
|
|
|
|
describe('state changes', () => {
|
|
beforeEach(async () => {
|
|
await manager.init(mockCredentials);
|
|
});
|
|
|
|
it('should handle app state changes', () => {
|
|
const mockTimerId = 123;
|
|
jest.spyOn(BackgroundTimer, 'setTimeout').mockReturnValue(mockTimerId);
|
|
|
|
// Get the app state callback and simulate background state
|
|
const mockAppStateChange = (AppState.addEventListener as jest.Mock).mock.calls[0][1];
|
|
mockAppStateChange('active');
|
|
mockAppStateChange('background');
|
|
|
|
expect(BackgroundTimer.setTimeout).toHaveBeenCalled();
|
|
expect(BackgroundTimer.setTimeout).toHaveBeenCalledWith(expect.any(Function), 15000);
|
|
});
|
|
|
|
it('should handle network state changes', () => {
|
|
const mockNetInfoCallback = (NetInfo.addEventListener as jest.Mock).mock.calls[0][0];
|
|
mockNetInfoCallback({isConnected: false, type: 'none'});
|
|
|
|
// Verify that clients are closed when network is disconnected
|
|
expect(manager.getClient(mockServerUrl)?.close).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('periodic updates', () => {
|
|
beforeEach(async () => {
|
|
jest.mocked(getCurrentUserId).mockResolvedValue('user1');
|
|
jest.mocked(queryAllUsers).mockImplementation(() => TestHelper.fakeQuery([
|
|
TestHelper.fakeUserModel({id: 'user1'}),
|
|
TestHelper.fakeUserModel({id: 'user2'}),
|
|
]));
|
|
await manager.init(mockCredentials);
|
|
});
|
|
|
|
it('should handle periodic status updates', async () => {
|
|
const client = manager.getClient(mockServerUrl);
|
|
expect(client).toBeDefined();
|
|
|
|
// Trigger first connect callback which starts periodic updates
|
|
mockCallbacks.firstConnect();
|
|
|
|
// Wait for all promises to resolve
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
expect(fetchStatusByIds).toHaveBeenCalledWith(mockServerUrl, ['user2']);
|
|
|
|
// Close the websocket to stop the periodic updates
|
|
mockCallbacks.close(0);
|
|
});
|
|
});
|
|
});
|