From 9ed8d053b0ae3cc78de5a204e0e4413e7350f077 Mon Sep 17 00:00:00 2001 From: mohamedsalem401 Date: Thu, 8 Feb 2024 01:52:28 +0200 Subject: [PATCH] Fix failing tests --- __mocks__/content/hi.canvas | 0 package-lock.json | 4 +- src/lib/indexFolder.ts | 3 - src/tests/documentTypes.spec.ts | 107 ++++++++++++++++---------------- 4 files changed, 54 insertions(+), 60 deletions(-) create mode 100644 __mocks__/content/hi.canvas diff --git a/__mocks__/content/hi.canvas b/__mocks__/content/hi.canvas new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index 4133ad1..1a35faa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mddb", - "version": "0.9.1", + "version": "0.9.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "mddb", - "version": "0.9.1", + "version": "0.9.3", "license": "MIT", "dependencies": { "@portaljs/remark-wiki-link": "^1.0.4", diff --git a/src/lib/indexFolder.ts b/src/lib/indexFolder.ts index 4184ce3..87b16a4 100644 --- a/src/lib/indexFolder.ts +++ b/src/lib/indexFolder.ts @@ -58,9 +58,6 @@ export function indexFolder( "Validation Failed: Unable to validate files against the specified scheme. Ensure that the file formats and content adhere to the specified scheme." ); } - throw new Error( - "Validation Failed: Unable to validate files against the specified scheme. Ensure that the file formats and content adhere to the specified scheme." - ); } files.push(fileObject); diff --git a/src/tests/documentTypes.spec.ts b/src/tests/documentTypes.spec.ts index cdca9eb..4b62a9d 100644 --- a/src/tests/documentTypes.spec.ts +++ b/src/tests/documentTypes.spec.ts @@ -4,63 +4,60 @@ import { MarkdownDB } from "../lib/markdowndb"; import { z } from "zod"; describe("Document Types Schema Validate Testing", () => { - const pathToContentFixture = "__mocks__/content"; - let mddb: MarkdownDB; + const pathToContentFixture = "__mocks__/content"; + let mddb: MarkdownDB; - beforeAll(async () => { - const dbConfig = { - client: "sqlite3", - connection: { - filename: "markdown.db", - }, - }; - mddb = new MarkdownDB(dbConfig); - await mddb.init(); - }); + beforeAll(async () => { + const dbConfig = { + client: "sqlite3", + connection: { + filename: "markdown.db", + }, + }; + mddb = new MarkdownDB(dbConfig); + await mddb.init(); + }); - afterAll(async () => { - await mddb.db.destroy(); + afterAll(async () => { + await mddb.db.destroy(); + }); + + test("Should check if the title field is created and save in db", async () => { + await mddb.indexFolder({ + folderPath: pathToContentFixture, + customConfig: { + computedFields: [ + (fileInfo: FileInfo, ast: Root) => { + fileInfo.title = "Hello"; + }, + ], + schemas: { + blog: z.object({ + title: z.string(), + }), + }, + }, }); + const dbFiles = await mddb.getFiles({ filetypes: ["blog"] }); + for (const file of dbFiles) { + expect(file.title).toBe("Hello"); + } + }); - test("Test that the 'indexFolder' function throws a validation error for a missing field in the blog schema.", async () => { - try { - await mddb.indexFolder({ - folderPath: pathToContentFixture, - customConfig: { - schemas: { - blog: z.object({ - missingField: z.string(), - }), - }, - }, - }); - - fail("Expected a validation error due to a missing field in the blog schema, but none was thrown."); - } catch (error: any) { - expect(error).toBeInstanceOf(Error); - expect(error.message).toContain("Validation Failed: Unable to validate files against the specified scheme."); - ; - } - }); - test("Should check if the title field is created and save in db", async () => { - await mddb.indexFolder({ - folderPath: pathToContentFixture, - customConfig: { - computedFields: [ - (fileInfo: FileInfo, ast: Root) => { - fileInfo.title = "Hello"; - }, - ], - schemas: { - blog: z.object({ - title: z.string(), - }), - }, - }, - }); - const dbFiles = await mddb.getFiles({ filetypes: ["blog"] }); - for (const file of dbFiles) { - expect(file.title).toBe("Hello"); - } - }); + test("Test that the 'indexFolder' function throws a validation error for a missing field in the blog schema.", async () => { + await expect( + mddb.indexFolder({ + folderPath: pathToContentFixture, + customConfig: { + schemas: { + blog: z.object({ + missingField: z.string(), + }), + }, + }, + }) + ).rejects.toThrow( + "Validation Failed: Unable to validate files against the specified scheme." + ); + }); });