diff --git a/__mocks__/expo-sqlite.js b/__mocks__/expo-sqlite.js deleted file mode 100644 index 6a7c58e..0000000 --- a/__mocks__/expo-sqlite.js +++ /dev/null @@ -1,44 +0,0 @@ -// __mocks__/expo-sqlite.js -const sqlite3 = require('sqlite3').verbose(); - -class SQLiteDatabase { - constructor(name) { - this.db = new sqlite3.Database(':memory:'); - } - - runAsync(sql, params = []) { - return new Promise((resolve, reject) => { - this.db.run(sql, params, function (err) { - if (err) { - reject(err); - } else { - resolve({ changes: this.changes }); - } - }); - }); - } - - execAsync(sql) { - return new Promise((resolve, reject) => { - this.db.exec(sql, err => { - if (err) { - reject(err); - } else { - resolve(); - } - }); - }); - } - - closeAsync() { - return new Promise(resolve => { - this.db.close(() => resolve()); - }); - } -} - -const SQLite = { - openDatabaseAsync: jest.fn(name => new SQLiteDatabase(name)), -}; - -module.exports = SQLite; \ No newline at end of file diff --git a/app/lib/__tests__/settings.spec.tsx b/app/lib/__tests__/settings.spec.tsx index 5a61a53..c71d349 100644 --- a/app/lib/__tests__/settings.spec.tsx +++ b/app/lib/__tests__/settings.spec.tsx @@ -7,12 +7,14 @@ describe('Settings', () => { beforeEach(async () => { // Initialize your Settings class here with a fresh database instance - settings = new Settings(await getDb()); + const db = await getDb(); + if (!db) throw new Error("Could not get db"); + settings = new Settings(db); }); afterEach(async () => { // Clean up the database after each test - await settings.db.executeSql('DELETE FROM settings'); + settings && await settings.db.executeSql('DELETE FROM settings'); }); describe('setHostLanguage', () => { @@ -57,7 +59,7 @@ describe('Settings', () => { it('should return the LibreTranslate base URL from the database', async () => { const value = 'https://another-example.com'; await settings.db.executeSql( - `INSERT INTO settings (libetransalte_base_url) VALUES (?)`, + `INSERT INTO settings (libretranslate_base_url) VALUES (?)`, [value] ); diff --git a/app/lib/db.ts b/app/lib/db.ts index 2557178..85915c9 100644 --- a/app/lib/db.ts +++ b/app/lib/db.ts @@ -2,7 +2,7 @@ import * as SQLite from 'expo-sqlite'; export const MIGRATE_UP = { 1: [ - `CREATE TABLE IF NOT EXIST settings ( + `CREATE TABLE IF NOT EXISTS settings ( host_language TEXT, libretranslate_base_url TEXT, ui_direction INTEGER diff --git a/app/lib/settings.ts b/app/lib/settings.ts index 02b35c7..85f33fc 100644 --- a/app/lib/settings.ts +++ b/app/lib/settings.ts @@ -19,7 +19,7 @@ export class Settings { } const query = ` -SELECT ? +SELECT ${key} FROM settings LIMIT 1` const result = await this.db.getFirstAsync( @@ -33,10 +33,10 @@ LIMIT 1` if (!Settings.KEYS.includes(key)) { throw new Error(`Invalid setting: '${key}'`) } - const statement = `INSERT INTO settings (${key}) -SELECT '?' -ON CONFLICT DO UPDATE SET ${key} = ?` - await this.db.runAsync(statement, [value, value]); + const statement = `REPLACE INTO settings ('${key}') +VALUES (?)` + const args = [value] + await this.db.runAsync(statement, args); } async setHostLanguage(value: string) { diff --git a/jestSetup.ts b/jestSetup.ts index f41848d..02216a4 100644 --- a/jestSetup.ts +++ b/jestSetup.ts @@ -1,5 +1,6 @@ // jestSetup.ts +/** jest.mock('expo-sqlite', () => { return { openDatabaseAsync: async (name: string) => { @@ -11,14 +12,20 @@ jest.mock('expo-sqlite', () => { executeSql: jest.fn((sql: string) => db.exec(sql)), runAsync: jest.fn(async (sql: string, params = []) => { const stmt = db.prepare(sql); - stmt.run(params) + // console.log("Running %s with %s", sql, params); + try { + stmt.run(params); + } catch (e) { + throw new Error(`running ${sql} with params ${JSON.stringify(params)}: ${e}`); + } }), getFirstAsync: jest.fn(async (sql : string, params = []) => { const stmt = db.prepare(sql) - const result = stmt.run(params); - return stmt.all(params)[0] + // const result = stmt.run(...params); + return stmt.get(params) }) }; }, }; -}); \ No newline at end of file +}); +*/ \ No newline at end of file diff --git a/package.json b/package.json index db9f084..3fd7956 100644 --- a/package.json +++ b/package.json @@ -66,9 +66,7 @@ "!**/.expo/**" ], "automock": false, - "setupFilesAfterEnv": [ - "/jestSetup.ts" - ], + "setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"], "testTimeout": 10000 }, "devDependencies": {