-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuDB.test.js
70 lines (60 loc) · 2.12 KB
/
uDB.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
const uDB = require("./uDB");
const testStore = "test-store";
describe("Test the database", () => {
beforeEach(() => {
new uDB(testStore).clear();
});
afterEach(() => {
new uDB(testStore).clear();
});
test("List empty store", () => {
expect(new uDB(testStore).getAll()).toEqual([]);
});
test("Insert documents", () => {
const db = new uDB(testStore);
expect(db.getAll().length).toBe(0);
const doc = { foo: "bar" };
const docInserted = db.put(doc);
expect(db.getAll().length).toBe(1);
const docInserted2 = db.put(doc);
expect(db.getAll().length).toBe(2);
expect(docInserted.foo).toBe(doc.foo);
expect(docInserted2.foo).toBe(doc.foo);
expect(docInserted._id).not.toEqual(docInserted2._id);
});
test("Update document", () => {
const db = new uDB(testStore);
expect(db.getAll().length).toBe(0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
expect(db.getAll().length).toBe(1);
docInserted.foo = "bazz";
const docInserted2 = db.put(docInserted);
expect(db.getAll().length).toBe(1);
expect(docInserted._id).toEqual(docInserted2._id);
});
test("Find document", () => {
const db = new uDB(testStore);
expect(db.getAll().length).toBe(0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
expect(db.get(docInserted._id)).toEqual(docInserted);
expect(db.get("no-such-id")).toBe(null);
});
test("Delete document", () => {
const db = new uDB(testStore);
expect(db.getAll().length).toBe(0);
const doc = { foo: "bar" };
let docInserted = db.put(doc);
expect(db.getAll().length).toBe(1);
expect(db.delete(docInserted._id).length).toBe(0);
});
test("List stores", () => {
const doc = { foo: "bar" };
new uDB(testStore).put(doc);
const stores = uDB.getStores();
expect(stores[0]).toBe(testStore);
expect(new uDB(stores[0]).getAll()[0].foo).toBe(doc.foo);
});
});