-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] rework storage engine tests to use CommQueryExecutor
Summary: Updates test to new module Depends on D8556 Test Plan: Run tests Reviewers: michal, tomek Reviewed By: michal Subscribers: ashoat Differential Revision: https://phab.comm.dev/D8557
- Loading branch information
Showing
1 changed file
with
21 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,62 @@ | ||
// @flow | ||
|
||
import initSqlJs from 'sql.js'; | ||
import { getDatabaseModule } from '../db-module.js'; | ||
import { clearSensitiveData } from '../utils/db-utils.js'; | ||
|
||
import { setupSQLiteDB } from './db-queries.js'; | ||
import { | ||
getPersistStorageItem, | ||
removePersistStorageItem, | ||
setPersistStorageItem, | ||
} from './storage-engine-queries.js'; | ||
const FILE_PATH = 'test.sqlite'; | ||
|
||
const TEST_KEY = 'redux-persist'; | ||
const TEST_ITEM = '[[{}]]'; | ||
|
||
describe('redux-persist storage engine queries', () => { | ||
let db; | ||
let queryExecutor; | ||
let dbModule; | ||
|
||
beforeAll(async () => { | ||
const SQL = await initSqlJs(); | ||
db = new SQL.Database(); | ||
dbModule = getDatabaseModule(); | ||
}); | ||
|
||
beforeEach(() => { | ||
setupSQLiteDB(db); | ||
const query = ` | ||
INSERT INTO persist_storage (key, item) | ||
VALUES ($key, $item) | ||
`; | ||
const values = { | ||
$key: TEST_KEY, | ||
$item: TEST_ITEM, | ||
}; | ||
db.exec(query, values); | ||
queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH); | ||
queryExecutor.setPersistStorageItem(TEST_KEY, TEST_ITEM); | ||
}); | ||
|
||
afterEach(() => { | ||
db.exec(`DELETE FROM persist_storage`); | ||
clearSensitiveData(dbModule, FILE_PATH, queryExecutor); | ||
}); | ||
|
||
it('should return the item of an existing key', () => { | ||
expect(getPersistStorageItem(db, TEST_KEY)).toBe(TEST_ITEM); | ||
expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); | ||
}); | ||
|
||
it('should return empty string for a non-existing key', () => { | ||
const nonExistingKey = 'non_existing_key'; | ||
expect(getPersistStorageItem(db, nonExistingKey)).toBe(''); | ||
expect(queryExecutor.getPersistStorageItem(nonExistingKey)).toBe(''); | ||
}); | ||
|
||
it('should set the item of an existing key', () => { | ||
const newItem = '[[{a:2]]'; | ||
setPersistStorageItem(db, TEST_KEY, newItem); | ||
expect(getPersistStorageItem(db, TEST_KEY)).toBe(newItem); | ||
queryExecutor.setPersistStorageItem(TEST_KEY, newItem); | ||
expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(newItem); | ||
}); | ||
|
||
it('should set the item of a non-existing key', () => { | ||
const newEntry = 'testEntry'; | ||
const newData = 'testData'; | ||
setPersistStorageItem(db, newEntry, newData); | ||
expect(getPersistStorageItem(db, newEntry)).toBe(newData); | ||
expect(getPersistStorageItem(db, TEST_KEY)).toBe(TEST_ITEM); | ||
queryExecutor.setPersistStorageItem(newEntry, newData); | ||
expect(queryExecutor.getPersistStorageItem(newEntry)).toBe(newData); | ||
expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); | ||
}); | ||
|
||
it('should remove an existing key', () => { | ||
removePersistStorageItem(db, TEST_KEY); | ||
expect(getPersistStorageItem(db, TEST_KEY)).toBe(''); | ||
queryExecutor.removePersistStorageItem(TEST_KEY); | ||
expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(''); | ||
}); | ||
|
||
it('should do nothing when removing a non-existing key', () => { | ||
const nonExistingName = 'non_existing_name'; | ||
removePersistStorageItem(db, nonExistingName); | ||
expect(getPersistStorageItem(db, nonExistingName)).toBe(''); | ||
expect(getPersistStorageItem(db, TEST_KEY)).toBe(TEST_ITEM); | ||
queryExecutor.removePersistStorageItem(nonExistingName); | ||
expect(queryExecutor.getPersistStorageItem(nonExistingName)).toBe(''); | ||
expect(queryExecutor.getPersistStorageItem(TEST_KEY)).toBe(TEST_ITEM); | ||
}); | ||
}); |