Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3391048
D7124.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
D7124.diff
View Options
diff --git a/web/database/queries/draft-queries.test.js b/web/database/queries/draft-queries.test.js
new file mode 100644
--- /dev/null
+++ b/web/database/queries/draft-queries.test.js
@@ -0,0 +1,114 @@
+// @flow
+
+import initSqlJs from 'sql.js';
+
+import { setupSQLiteDB } from './db-queries.js';
+import {
+ getAllDrafts,
+ moveDraft,
+ removeAllDrafts,
+ updateDraft,
+} from './draft-queries.js';
+
+describe('Draft Store queries', () => {
+ let db;
+
+ beforeAll(async () => {
+ const SQL = await initSqlJs();
+ db = new SQL.Database();
+ });
+
+ beforeEach(() => {
+ setupSQLiteDB(db);
+ db.exec(`
+ INSERT INTO drafts VALUES ("thread_a", "draft a");
+ INSERT INTO drafts VALUES ("thread_b", "draft b");
+ `);
+ });
+
+ afterEach(() => {
+ db.exec(`DELETE FROM drafts`);
+ });
+
+ it('should return all drafts', () => {
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(2);
+ });
+
+ it('should remove all drafts', () => {
+ removeAllDrafts(db);
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(0);
+ });
+
+ it('should update draft text', () => {
+ const key = 'thread_b';
+ const text = 'updated message';
+ updateDraft(db, key, text);
+
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(2);
+
+ const draft = drafts.find(d => d.key === key);
+ expect(draft?.text).toBe(text);
+ });
+
+ it('should insert not existing draft', () => {
+ const key = 'new_key';
+ const text = 'some message';
+ updateDraft(db, key, text);
+
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(3);
+
+ const draft = drafts.find(d => d.key === key);
+ expect(draft?.text).toBe(text);
+ });
+
+ it('should move draft to a new key', () => {
+ const newKey = 'new_key';
+ const oldKey = 'thread_a';
+ const draftText = 'draft a';
+ moveDraft(db, oldKey, newKey);
+
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(2);
+
+ const oldKeyDraft = drafts.find(d => d.key === oldKey);
+ expect(oldKeyDraft).toBeUndefined();
+
+ const newKeyDraft = drafts.find(d => d.key === newKey);
+ expect(newKeyDraft?.text).toBe(draftText);
+ });
+
+ it('should not change anything if oldKey not exists', () => {
+ const newKey = 'new_key';
+ const oldKey = 'missing_key';
+ moveDraft(db, oldKey, newKey);
+
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(2);
+
+ const oldKeyDraft = drafts.find(d => d.key === oldKey);
+ expect(oldKeyDraft).toBeUndefined();
+
+ const newKeyDraft = drafts.find(d => d.key === newKey);
+ expect(newKeyDraft).toBeUndefined();
+ });
+
+ it('should move and replace if newKey exists', () => {
+ const newKey = 'thread_b';
+ const oldKey = 'thread_a';
+ const draftText = 'draft a';
+ moveDraft(db, oldKey, newKey);
+
+ const drafts = getAllDrafts(db);
+ expect(drafts.length).toBe(1);
+
+ const oldKeyDraft = drafts.find(d => d.key === oldKey);
+ expect(oldKeyDraft).toBeUndefined();
+
+ const newKeyDraft = drafts.find(d => d.key === newKey);
+ expect(newKeyDraft?.text).toBe(draftText);
+ });
+});
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 1, 2:05 AM (21 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2603277
Default Alt Text
D7124.diff (3 KB)
Attached To
Mode
D7124: [web-db] add unit tests for draft store queries
Attached
Detach File
Event Timeline
Log In to Comment