UNPKG

bookworms

Version:

A cli tool for centralising and generating bookmarks

63 lines (61 loc) 2.42 kB
import { jest } from "@jest/globals"; import { rmSync, readFileSync } from "fs"; import yaml from "js-yaml"; import { parseHTMLtoJSON, generateBookwormJSON, traverseStructure, convertHTMLtoYAML, createBookmarks, } from "../convert-bookmarks.js"; describe("generating bookmarks structure", () => { describe("parseHTMLtoJSON", () => { test("should return a JSON object parsed from the HTML", async () => { expect(await parseHTMLtoJSON("./demo/browsers.html")).toMatchSnapshot(); }); }); describe("generateBookwormJSON", () => { test("should return a JSON that matches bookworms schema", async () => { const json = await parseHTMLtoJSON("./demo/browsers.html"); expect(await generateBookwormJSON(json)).toMatchSnapshot(); }); }); describe("traverseStructure", () => { test("should return a JSON reformatted by traversing parseHTMLtoJSON response", async () => { const json = await parseHTMLtoJSON("./demo/browsers.html"); expect(await traverseStructure(json)).toMatchSnapshot(); }); }); describe("convertHTMLtoYAML", () => { test("should return a JSON reformatted by traversing parseHTMLtoJSON response", async () => { expect(await convertHTMLtoYAML("./demo/browsers.html")).toMatchSnapshot(); }); test("should match the config.yaml structure with exception to descriptions", async () => { const originalYAML = yaml.load( readFileSync("./demo/config/bookmarks.yaml", "utf8") ); const generatedYAML = yaml.load( await convertHTMLtoYAML("./demo/browsers.html") ); expect(generatedYAML.label).toEqual(originalYAML.label); expect(generatedYAML.description).toEqual( "These bookmarks were generated by a browser export" ); expect(generatedYAML.folders[0].label).toEqual( originalYAML.folders[0].label ); expect(generatedYAML.folders[0].description).toBeUndefined(); }); }); describe("createBookmarks", () => { test("should generate YAML and write to disk from HTML file", async () => { const path = "./tmp"; const filename = "generated.yaml"; const bookmarks = "./demo/browsers.html"; await createBookmarks(bookmarks, path, filename); const bookmark = readFileSync(`${path}/${filename}`, "utf8"); expect(bookmark).toMatchSnapshot(); rmSync(path, { recursive: true }); }); }); });