131 lines
3.9 KiB
TypeScript
131 lines
3.9 KiB
TypeScript
// jestSetup.ts
|
|
|
|
// include this line for mocking react-native-gesture-handler
|
|
import 'react-native-gesture-handler/jestSetup';
|
|
|
|
jest.mock("expo-sqlite", () => {
|
|
const { DatabaseSync } = require("node:sqlite");
|
|
const db = new DatabaseSync(":memory:");
|
|
|
|
const { MIGRATE_UP } = jest.requireActual("./app/lib/migrations");
|
|
|
|
const genericRun = (sql: string, ... params : string []) => {
|
|
// console.log("Running %s with %s", sql, params);
|
|
try {
|
|
const stmt = db.prepare(sql);
|
|
stmt.run(...params);
|
|
} catch (e) {
|
|
throw new Error(
|
|
`running ${sql} with params ${JSON.stringify(params)}: ${e}`
|
|
);
|
|
}
|
|
}
|
|
|
|
const genericGetFirst = (sql: string, params = []) => {
|
|
const stmt = db.prepare(sql);
|
|
// const result = stmt.run(...params);
|
|
return stmt.get(params);
|
|
};
|
|
|
|
const openDatabaseAsync = async (name: string) => {
|
|
return {
|
|
closeAsync: jest.fn(() => db.close()),
|
|
executeSql: jest.fn((sql: string) => db.exec(sql)),
|
|
runAsync: jest.fn(genericRun),
|
|
runSync: jest.fn(genericRun),
|
|
getFirstAsync: jest.fn(genericGetFirst),
|
|
getFirstSync: jest.fn(genericGetFirst),
|
|
};
|
|
};
|
|
return {
|
|
migrateDb: async (direction: "up" | "down" = "up") => {
|
|
const db = await openDatabaseAsync("translation_terrace_development");
|
|
for (let m of Object.values(MIGRATE_UP)) {
|
|
for (let stmt of m) {
|
|
await db.executeSql(stmt);
|
|
}
|
|
}
|
|
},
|
|
openDatabaseAsync,
|
|
};
|
|
});
|
|
|
|
// include this section and the NativeAnimatedHelper section for mocking react-native-reanimated
|
|
jest.mock('react-native-reanimated', () => {
|
|
const Reanimated = require('react-native-reanimated/mock');
|
|
|
|
// The mock for `call` immediately calls the callback which is incorrect
|
|
// So we override it with a no-op
|
|
Reanimated.default.call = () => {};
|
|
|
|
return Reanimated;
|
|
});
|
|
|
|
// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
|
|
// jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');
|
|
|
|
// Mock implementation of the Settings class for Jest
|
|
|
|
// Mock implementation of the Settings class
|
|
jest.mock('@/app/lib/settings', () => {
|
|
const originalModule = jest.requireActual('@/app/lib/settings');
|
|
|
|
class MockSettings extends originalModule.Settings {
|
|
constructor(db: any) {
|
|
super(db);
|
|
}
|
|
|
|
private getValue(key: string) {
|
|
// Return mock values for testing
|
|
switch (key) {
|
|
case "host_language":
|
|
return "en";
|
|
case "libretranslate_base_url":
|
|
return "http://mock.libretranslate.com";
|
|
case 'ui_direction':
|
|
return "ltr";
|
|
case "whisper_model":
|
|
return "base";
|
|
default:
|
|
throw new Error(`Invalid setting: '${key}'`);
|
|
}
|
|
}
|
|
|
|
private setValue(key: string, value: any) {
|
|
// Mock setting values
|
|
console.log(`Mock set ${key} to ${value}`);
|
|
}
|
|
|
|
static getDefault() {
|
|
const mockDb = { // Mock database object
|
|
select: jest.fn().mockReturnThis(),
|
|
limit: jest.fn().mockReturnThis(),
|
|
first: jest.fn().mockResolvedValue({ host_language: "en" }),
|
|
update: jest.fn().mockResolvedValue(1),
|
|
insert: jest.fn().mockResolvedValue([1])
|
|
};
|
|
return new MockSettings(mockDb);
|
|
}
|
|
}
|
|
|
|
return {
|
|
__esModule: true,
|
|
...originalModule,
|
|
default: MockSettings
|
|
};
|
|
});
|
|
|
|
jest.mock('expo-file-system', () => ({
|
|
// ... other methods ...
|
|
createDownloadResumable: jest.fn(() => ({
|
|
downloadAsync: jest.fn(() => Promise.resolve({ uri: 'mocked-uri' })),
|
|
pauseAsync: jest.fn(() => Promise.resolve()),
|
|
resumeAsync: jest.fn(() => Promise.resolve()),
|
|
cancelAsync: jest.fn(() => Promise.resolve()),
|
|
})),
|
|
getInfoAsync: jest.fn(() => ({
|
|
exists: () => false,
|
|
}))
|
|
// ... other methods ...
|
|
}));
|