getting-started/app/spec/persistence/sqlite.spec.js
Michael Irwin 5bb26cc53a
Initial commit
Signed-off-by: mikesir87 <mikesir87@gmail.com>

Imported from dockersamples/101-tutorial, removed other languages
for now, and replaced PWD references with Docker Desktop.
2020-02-05 22:04:43 -05:00

65 lines
1.4 KiB
JavaScript

const db = require('../../src/persistence/sqlite');
const fs = require('fs');
const ITEM = {
id: '7aef3d7c-d301-4846-8358-2a91ec9d6be3',
name: 'Test',
completed: false,
};
beforeEach(() => {
if (fs.existsSync('/etc/todos/todo.db')) {
fs.unlinkSync('/etc/todos/todo.db');
}
});
test('it initializes correctly', async () => {
await db.init();
});
test('it can store and retrieve items', async () => {
await db.init();
await db.storeItem(ITEM);
const items = await db.getItems();
expect(items.length).toBe(1);
expect(items[0]).toEqual(ITEM);
});
test('it can update an existing item', async () => {
await db.init();
const initialItems = await db.getItems();
expect(initialItems.length).toBe(0);
await db.storeItem(ITEM);
await db.updateItem(
ITEM.id,
Object.assign({}, ITEM, { completed: !ITEM.completed }),
);
const items = await db.getItems();
expect(items.length).toBe(1);
expect(items[0].completed).toBe(!ITEM.completed);
});
test('it can remove an existing item', async () => {
await db.init();
await db.storeItem(ITEM);
await db.removeItem(ITEM.id);
const items = await db.getItems();
expect(items.length).toBe(0);
});
test('it can get a single item', async () => {
await db.init();
await db.storeItem(ITEM);
const item = await db.getItem(ITEM.id);
expect(item).toEqual(ITEM);
});