Skip to content

Commit

Permalink
[web] rework storage engine tests to use CommQueryExecutor
Browse files Browse the repository at this point in the history
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
xsanm committed Jul 27, 2023
1 parent c5eeca8 commit ba914f5
Showing 1 changed file with 21 additions and 33 deletions.
54 changes: 21 additions & 33 deletions web/database/queries/storage-engine-queries.test.js
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);
});
});

0 comments on commit ba914f5

Please sign in to comment.