|
| 1 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 2 | +import { open } from '@op-engineering/op-sqlite'; |
| 3 | +import { beforeEach, describe, expect, test } from 'react-native-harness'; |
| 4 | +import { Storage } from 'mendix-native'; |
| 5 | + |
| 6 | +const DB_NAME = 'storage-harness.sqlite'; |
| 7 | +const TABLE_NAME = 'storage_harness_records'; |
| 8 | + |
| 9 | +async function seedDatabase(): Promise<void> { |
| 10 | + const db = open({ name: DB_NAME }); |
| 11 | + |
| 12 | + await db.execute( |
| 13 | + `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY NOT NULL, value TEXT NOT NULL)` |
| 14 | + ); |
| 15 | + await db.execute(`DELETE FROM ${TABLE_NAME}`); |
| 16 | + await db.execute(`INSERT INTO ${TABLE_NAME} (value) VALUES (?)`, ['fixture']); |
| 17 | + |
| 18 | + db.close(); |
| 19 | +} |
| 20 | + |
| 21 | +async function getRowCount(): Promise<number> { |
| 22 | + const db = open({ name: DB_NAME }); |
| 23 | + const result = await db.execute( |
| 24 | + `SELECT COUNT(*) AS count FROM ${TABLE_NAME}` |
| 25 | + ); |
| 26 | + const rowCount = Number(result.rows?.[0]?.count ?? 0); |
| 27 | + |
| 28 | + db.close(); |
| 29 | + |
| 30 | + return rowCount; |
| 31 | +} |
| 32 | + |
| 33 | +async function tableExists(): Promise<boolean> { |
| 34 | + const db = open({ name: DB_NAME }); |
| 35 | + const result = await db.execute( |
| 36 | + "SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?", |
| 37 | + [TABLE_NAME] |
| 38 | + ); |
| 39 | + const exists = result.rows.length > 0; |
| 40 | + |
| 41 | + db.close(); |
| 42 | + |
| 43 | + return exists; |
| 44 | +} |
| 45 | + |
| 46 | +describe('Storage', () => { |
| 47 | + beforeEach(async () => { |
| 48 | + await AsyncStorage.clear(); |
| 49 | + |
| 50 | + try { |
| 51 | + await Storage.closeDatabaseConnections(); |
| 52 | + } catch { |
| 53 | + // Ignore cleanup failures so the real test can surface the error. |
| 54 | + } |
| 55 | + |
| 56 | + try { |
| 57 | + await Storage.clearDatabases(); |
| 58 | + } catch { |
| 59 | + // Ignore cleanup failures so the real test can surface the error. |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + describe('clearAsyncStorage', () => { |
| 64 | + test('removes previously persisted AsyncStorage keys', async () => { |
| 65 | + await AsyncStorage.setItem('storage-harness:key-1', 'value-1'); |
| 66 | + await AsyncStorage.setItem('storage-harness:key-2', 'value-2'); |
| 67 | + |
| 68 | + await Storage.clearAsyncStorage(); |
| 69 | + |
| 70 | + const values = await AsyncStorage.multiGet([ |
| 71 | + 'storage-harness:key-1', |
| 72 | + 'storage-harness:key-2', |
| 73 | + ]); |
| 74 | + |
| 75 | + expect(values).toEqual([ |
| 76 | + ['storage-harness:key-1', null], |
| 77 | + ['storage-harness:key-2', null], |
| 78 | + ]); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('closeDatabaseConnections', () => { |
| 83 | + test('closes active sqlite connections without deleting persisted data', async () => { |
| 84 | + const db = open({ name: DB_NAME }); |
| 85 | + |
| 86 | + await db.execute( |
| 87 | + `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY NOT NULL, value TEXT NOT NULL)` |
| 88 | + ); |
| 89 | + await db.execute(`DELETE FROM ${TABLE_NAME}`); |
| 90 | + await db.execute(`INSERT INTO ${TABLE_NAME} (value) VALUES (?)`, [ |
| 91 | + 'fixture', |
| 92 | + ]); |
| 93 | + |
| 94 | + await Storage.closeDatabaseConnections(); |
| 95 | + |
| 96 | + expect(await getRowCount()).toBe(1); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('clearDatabases', () => { |
| 101 | + test('removes sqlite schema created through op-sqlite', async () => { |
| 102 | + await seedDatabase(); |
| 103 | + expect(await tableExists()).toBe(true); |
| 104 | + |
| 105 | + await Storage.clearDatabases(); |
| 106 | + |
| 107 | + expect(await tableExists()).toBe(false); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('clearAll', () => { |
| 112 | + test('clears AsyncStorage and sqlite state in one call', async () => { |
| 113 | + await AsyncStorage.setItem('storage-harness:combined', 'present'); |
| 114 | + await seedDatabase(); |
| 115 | + |
| 116 | + await Storage.clearAll(); |
| 117 | + |
| 118 | + expect(await AsyncStorage.getItem('storage-harness:combined')).toBe(null); |
| 119 | + expect(await tableExists()).toBe(false); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments