mattermost-mobile-upstream/app/database/manager/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

126 lines
4.9 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Database, Q} from '@nozbe/watermelondb';
import {MM_TABLES} from '@constants/database';
import DatabaseManager from '@database/manager';
import ServerDataOperator from '@database/operator/server_data_operator';
import type ServersModel from '@typings/database/models/app/servers';
const {SERVERS} = MM_TABLES.APP;
// NOTE : On the mock Database Manager, we cannot test for :
// 1. Android/iOS file path
// 2. Deletion of the 'databases' folder on those two platforms
describe('*** Database Manager tests ***', () => {
const serverUrls = ['https://appv1.mattermost.com', 'https://appv2.mattermost.com'];
beforeAll(async () => {
await DatabaseManager.init(serverUrls);
});
afterAll(async () => {
const servers = Object.keys(DatabaseManager.serverDatabases);
for await (const server of servers) {
await DatabaseManager.destroyServerDatabase(server);
}
});
it('=> should return a default database', async () => {
expect.assertions(2);
const appDatabase = DatabaseManager.appDatabase?.database;
expect(appDatabase).toBeInstanceOf(Database);
expect(Object.keys(DatabaseManager.serverDatabases).length).toBe(2);
});
it('=> should create a new server database', async () => {
expect.assertions(3);
const spyOnAddServerToDefaultDatabase = jest.spyOn(DatabaseManager as any, 'addServerToAppDatabase');
const connection1 = await DatabaseManager!.createServerDatabase({
config: {
dbName: 'appv3 mattermost',
serverUrl: 'https://appv3.mattermost.com',
identifier: 'appv3',
},
});
expect(connection1?.database).toBeInstanceOf(Database);
expect(connection1?.operator).toBeInstanceOf(ServerDataOperator);
expect(spyOnAddServerToDefaultDatabase).toHaveBeenCalledTimes(1);
});
it('=> should switch between active servers', async () => {
expect.assertions(4);
await DatabaseManager.updateServerIdentifier(serverUrls[0], 'appv1');
await DatabaseManager.updateServerIdentifier(serverUrls[1], 'appv2');
let activeServerUrl = await DatabaseManager.getActiveServerUrl();
const serverA = await DatabaseManager.getActiveServerDatabase();
// as we haven't set an active server yet, so the first registered server should be the active one
expect(activeServerUrl).toBe(serverUrls[0]);
expect(serverA).toEqual(DatabaseManager.serverDatabases[serverUrls[0]]!.database);
await DatabaseManager.setActiveServerDatabase('https://appv2.mattermost.com');
// new active server should change and we have a Database and is active
activeServerUrl = await DatabaseManager.getActiveServerUrl();
const serverB = await DatabaseManager.getActiveServerDatabase();
expect(activeServerUrl).toBe(serverUrls[1]);
expect(serverB).toEqual(DatabaseManager.serverDatabases[serverUrls[1]]!.database);
});
it('=> should delete appv1 server from the servers table of App database', async () => {
expect.assertions(2);
await DatabaseManager.setActiveServerDatabase('https://appv1.mattermost.com');
await DatabaseManager.destroyServerDatabase('https://appv1.mattermost.com');
const fetchServerRecords = async (serverUrl: string) => {
const servers = await DatabaseManager.appDatabase?.database!.collections.get<ServersModel>(SERVERS).query(Q.where('url', serverUrl)).fetch();
return servers?.length || 0;
};
const destroyed = await fetchServerRecords(serverUrls[0]);
expect(destroyed).toBe(0);
// Removing database for appv1 connection
const activeServerUrl = await DatabaseManager.getActiveServerUrl();
expect(activeServerUrl).toEqual(serverUrls[1]);
});
it('=> should return appv3 server url from the servers table of App database', async () => {
expect.assertions(1);
const serverUrl = await DatabaseManager.getServerUrlFromIdentifier('appv3');
expect(serverUrl).toBe('https://appv3.mattermost.com');
});
it('=> updatePersistenceFlag persists the flag on the matching server record', async () => {
expect.assertions(2);
const targetUrl = 'https://appv3.mattermost.com';
const fetchServer = async () => {
const servers = await DatabaseManager.appDatabase?.database.collections.
get<ServersModel>(SERVERS).query(Q.where('url', targetUrl)).fetch();
return servers?.[0];
};
const before = await fetchServer();
expect(before?.persistenceFlag).toBe('');
await DatabaseManager.updatePersistenceFlag(targetUrl, 'wiped');
const after = await fetchServer();
expect(after?.persistenceFlag).toBe('wiped');
});
});