improve tests, especially for navigation.

This commit is contained in:
Jordan
2025-02-27 08:23:27 -08:00
parent 6f941c56d1
commit 87446784ae
24 changed files with 748 additions and 448 deletions

View File

@ -1,9 +1,9 @@
import React, { Dispatch } from "react";
import { render, screen, fireEvent, act } from "@testing-library/react-native";
import SettingsComponent from "@/components/Settings";
import { Settings } from "@/app/lib/settings";
import { getDb } from "@/app/lib/db";
import { language_matrix } from "@/app/i18n/api";
import { Settings } from "@/app/lib/settings";
import { getDb, migrateDb } from "@/app/lib/db";
const RENDER_TIME = 1000;
@ -49,45 +49,10 @@ jest.mock("@/app/i18n/api", () => {
}
})
jest.mock("@/app/lib/db", () => {
return {
getDb: jest.fn(() => {
return {
runAsync: jest.fn((statement : string, value : string) => {}),
getFirstAsync: jest.fn((statement : string, value : string) => {
return []
}),
}
})
}
})
jest.mock("@/app/lib/settings", () => {
const originalModule = jest.requireActual('@/app/lib/settings');
class MockSettings {
public constructor(public db = {}) {}
public setHostLanguage = jest.fn((val : string) => {
})
public setLibretranslateBaseUrl(val : string) {
}
getHostLanguage = jest.fn(() => {
return "en"
})
getLibretranslateBaseUrl = jest.fn(() => {
return "http://localhost:5004"
});
}
return {
...originalModule,
Settings: MockSettings
}
})
describe("SettingsComponent", () => {
beforeEach(async() => {
const settings = new Settings(await getDb());
await migrateDb();
const settings = await Settings.getDefault();
await settings.setHostLanguage("en");
await settings.setLibretranslateBaseUrl("https://example.com");
})
@ -116,13 +81,12 @@ describe("SettingsComponent", () => {
test("updates host language setting when input changes", async () => {
render(<SettingsComponent />);
// Wait for the component to fetch and display the initial settings
await screen.findByText(/Host Language:/i);
await screen.findByText(/LibreTranslate Base URL:/i);
// Change the host language input value
const picker = screen.getByAccessibilityHint("language");
const picker = screen.getByAccessibilityHint("hostLanguage");
fireEvent(picker, "onvalueChange", "es");
expect(picker.props.selectedIndex).toStrictEqual(0);
});