1
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
3
import { BrowserDB } from '../../core/db';
4
import { DB_File } from '../../schemas/files';
5
import { FileModel } from '../file';
10
describe('_FileModel', () => {
11
let fileData: DB_File;
16
data: new ArrayBuffer(10),
17
fileType: 'image/png',
25
afterEach(async () => {
27
const db = new BrowserDB();
28
await db.files.clear();
32
it('should create a file record', async () => {
34
const fileData: DB_File = {
35
data: new ArrayBuffer(10),
36
fileType: 'image/png',
42
const result = await FileModel.create(fileData);
44
expect(result).toHaveProperty('id');
45
expect(result.id).toMatch(/^file-/);
48
const fileInDb = await FileModel.findById(result.id);
50
expect(fileInDb).toEqual(expect.objectContaining(fileData));
53
it('should find a file by id', async () => {
55
const createdFile = await FileModel.create(fileData);
56
const foundFile = await FileModel.findById(createdFile.id);
58
expect(foundFile).toEqual(expect.objectContaining(fileData));
61
it('should delete a file by id', async () => {
63
const createdFile = await FileModel.create(fileData);
64
await FileModel.delete(createdFile.id);
67
const fileInDb = await FileModel.findById(createdFile.id);
68
expect(fileInDb).toBeUndefined();
71
it('should clear all files', async () => {
73
const createdFile = await FileModel.create(fileData);
74
const createdFile2 = await FileModel.create(fileData);
75
await FileModel.clear();
78
const fileInDb = await FileModel.findById(createdFile.id);
79
expect(fileInDb).toBeUndefined();
80
const fileInDb2 = await FileModel.findById(createdFile2.id);
81
expect(fileInDb2).toBeUndefined();