Skip to content

Commit

Permalink
[web] rework draft tests to use CommQueryExecutor
Browse files Browse the repository at this point in the history
Summary:
Updates test to new module

Depends on D8555

Test Plan: Run tests

Reviewers: michal, tomek

Reviewed By: michal

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D8556
  • Loading branch information
xsanm committed Jul 27, 2023
1 parent cfb5ea1 commit c5eeca8
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions web/database/queries/draft-queries.test.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
// @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 {
getAllDrafts,
moveDraft,
removeAllDrafts,
updateDraft,
} from './draft-queries.js';
const FILE_PATH = 'test.sqlite';

describe('Draft Store queries', () => {
let db;
let queryExecutor;
let dbModule;

beforeAll(async () => {
const SQL = await initSqlJs();
db = new SQL.Database();
dbModule = getDatabaseModule();
});

beforeEach(() => {
setupSQLiteDB(db);
db.exec(`
INSERT INTO drafts VALUES ("thread_a", "draft a");
INSERT INTO drafts VALUES ("thread_b", "draft b");
`);
queryExecutor = new dbModule.SQLiteQueryExecutor(FILE_PATH);
queryExecutor.updateDraft('thread_a', 'draft a');
queryExecutor.updateDraft('thread_b', 'draft b');
});

afterEach(() => {
db.exec(`DELETE FROM drafts`);
clearSensitiveData(dbModule, FILE_PATH, queryExecutor);
});

it('should return all drafts', () => {
const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(2);
});

it('should remove all drafts', () => {
removeAllDrafts(db);
const drafts = getAllDrafts(db);
queryExecutor.removeAllDrafts();
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(0);
});

it('should update draft text', () => {
const key = 'thread_b';
const text = 'updated message';
updateDraft(db, key, text);
queryExecutor.updateDraft(key, text);

const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(2);

const draft = drafts.find(d => d.key === key);
Expand All @@ -56,9 +49,9 @@ describe('Draft Store queries', () => {
it('should insert not existing draft', () => {
const key = 'new_key';
const text = 'some message';
updateDraft(db, key, text);
queryExecutor.updateDraft(key, text);

const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(3);

const draft = drafts.find(d => d.key === key);
Expand All @@ -69,9 +62,9 @@ describe('Draft Store queries', () => {
const newKey = 'new_key';
const oldKey = 'thread_a';
const draftText = 'draft a';
moveDraft(db, oldKey, newKey);
queryExecutor.moveDraft(oldKey, newKey);

const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(2);

const oldKeyDraft = drafts.find(d => d.key === oldKey);
Expand All @@ -84,9 +77,9 @@ describe('Draft Store queries', () => {
it('should not change anything if oldKey not exists', () => {
const newKey = 'new_key';
const oldKey = 'missing_key';
moveDraft(db, oldKey, newKey);
queryExecutor.moveDraft(oldKey, newKey);

const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(2);

const oldKeyDraft = drafts.find(d => d.key === oldKey);
Expand All @@ -100,9 +93,9 @@ describe('Draft Store queries', () => {
const newKey = 'thread_b';
const oldKey = 'thread_a';
const draftText = 'draft a';
moveDraft(db, oldKey, newKey);
queryExecutor.moveDraft(oldKey, newKey);

const drafts = getAllDrafts(db);
const drafts = queryExecutor.getAllDrafts();
expect(drafts.length).toBe(1);

const oldKeyDraft = drafts.find(d => d.key === oldKey);
Expand Down

0 comments on commit c5eeca8

Please sign in to comment.