diff --git a/package.json b/package.json index 6ee8279..59d1bbb 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,6 @@ + { + "type": "module", "scripts": { "prettier-format-check": "prettier --check src/*", "prettier-format-fix": "prettier --write src/*" diff --git a/src/lib/data.test.js b/src/lib/data.test.js new file mode 100644 index 0000000..7862f3d --- /dev/null +++ b/src/lib/data.test.js @@ -0,0 +1,74 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { + fetchJurisdictionsData, + fetchReporterData, + fetchVolumesData, + fetchCasesList, + fetchCaselawBody, + fetchCaseMetadata, + getBreadcrumbLinks, +} from "./data.js"; + +global.window = { BUCKET_ROOT: "https://cap-redacted-demo.lil.tools" }; + +test("fetchJurisdictionsData fetches jurisdictions ", async (t) => { + await fetchJurisdictionsData((data) => { + assert.ok(data instanceof Object); + assert.ok(Object.keys(data).length > 3); + assert.ok(Object.keys(data).includes("Arkansas")); + }); +}); + +test("fetchReporterData can fetch metadata for a reporter", async (t) => { + await fetchReporterData("ark", (data) => { + assert.ok(data instanceof Object); + assert.strictEqual(data.slug, "ark"); + }); +}); + +test("fetchVolumeData can fetch a list of volumes", async (t) => { + await fetchVolumesData("ark", (data) => { + assert.ok(data instanceof Array); + assert.ok(data.length > 100); + }); +}); + +test("fetchCasesList can load a list of cases", async (t) => { + await fetchCasesList("ark", "1", (data) => { + assert.ok(data instanceof Array); + assert.ok(data.length > 50); + }); +}); + +test("fetchCaselawBody can load a case html file", async (t) => { + await fetchCaselawBody("ark", "1", "0011-01", (data) => { + // literal strings arent instances of String, so we have to use typeof + assert.strictEqual(typeof data, "string"); + assert.match(data, /
{ + await fetchCaseMetadata("ark", "1", "0011-01", (data) => { + assert.ok(data instanceof Object); + assert.strictEqual(data.name, "Goings against Mills"); + }); +}); + +test("getBreadcrumbLinks", async (t) => { + const reporterData = { + slug: "ark", + short_name: "Arkansas", + }; + const volume = "1"; + const expected = [ + { + url: "/caselaw/?reporter=ark", + name: "Reporter Arkansas", + }, + { url: "/caselaw/?reporter=ark&volume=1", name: "Volume 1" }, + ]; + const links = getBreadcrumbLinks(reporterData, volume); + assert.deepStrictEqual(links, expected); +}); diff --git a/src/lib/fetchOr404.test.js b/src/lib/fetchOr404.test.js new file mode 100644 index 0000000..e30ed6e --- /dev/null +++ b/src/lib/fetchOr404.test.js @@ -0,0 +1,38 @@ +import { test, before } from "node:test"; +import assert from "node:assert/strict"; + +import { fetchOr404 } from "./fetchOr404.js"; + +let newLocation = ""; + +const window = { + location: { + replace: function (url) { + newLocation = url; + }, + search: "foo", + }, +}; + +global.window = window; +global.location = window.location; + +before(() => (newLocation = "")); + +test("fetchOr404 works when the fetches work.", async (t) => { + const fetches = [ + () => Promise.resolve("first fetch"), + () => Promise.resolve("second fetch"), + ]; + await fetchOr404(...fetches); + assert.strictEqual(newLocation, ""); +}); + +test("fetchOr404 redirects when a fetch fails.", async (t) => { + const fetches = [ + () => Promise.resolve("first fetch"), + () => Promise.reject("second fetch"), + ]; + await fetchOr404(...fetches); + assert.equal(newLocation, "./not-found/foo"); +}); diff --git a/src/lib/isEmpty.js b/src/lib/isEmpty.js index 1a172c0..1e7398d 100644 --- a/src/lib/isEmpty.js +++ b/src/lib/isEmpty.js @@ -1,12 +1,12 @@ export const isEmpty = (obj) => { if (obj === null || obj === undefined) { - return True; + return true; } else if (Array.isArray(obj) || typeof obj === "string") { return obj.length === 0; } else if ( ["boolean", "number", "bigint", "symbol", "function"].includes(typeof obj) ) { - throw new Error(f`Unsuported type: ${typeof obj}`); + throw new Error(`Unsuported type: ${typeof obj}`); } else { return Object.keys(obj).length === 0; } diff --git a/src/lib/isEmpty.test.js b/src/lib/isEmpty.test.js new file mode 100644 index 0000000..0f448ac --- /dev/null +++ b/src/lib/isEmpty.test.js @@ -0,0 +1,81 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; + +import { isEmpty } from "./isEmpty.js"; + +test("isEmpty returns true for empty objects", (t) => { + const emptyObj = {}; + assert(isEmpty(emptyObj)); +}); + +test("isEmpty returns true for empty arrays", (t) => { + const emptyArray = []; + assert.ok(isEmpty(emptyArray)); +}); + +test("isEmpty returns true for empty strings", (t) => { + const emptyString = ""; + assert.ok(isEmpty(emptyString)); +}); + +test("isEmpty returns false for non-empty objects", (t) => { + const nonEmptyObj = { a: 1 }; + assert.ok(!isEmpty(nonEmptyObj)); +}); + +test("isEmpty returns false for non-empty arrays", (t) => { + const nonEmptyArray = [1]; + assert.ok(!isEmpty(nonEmptyArray)); +}); + +test("isEmpty returns false for non-empty strings", (t) => { + const nonEmptyString = "a"; + assert.ok(!isEmpty(nonEmptyString)); +}); + +test("isEmpty returns false for null", (t) => { + assert.ok(isEmpty(null)); +}); + +test("isEmpty throws for unsupported types", (t) => { + assert.throws( + () => { + isEmpty(true); + }, + { + message: "Unsuported type: boolean", + }, + ); + assert.throws( + () => { + isEmpty(1); + }, + { + message: "Unsuported type: number", + }, + ); + assert.throws( + () => { + isEmpty(BigInt(9007199254740991)); + }, + { + message: "Unsuported type: bigint", + }, + ); + assert.throws( + () => { + isEmpty(Symbol("foo")); + }, + { + message: "Unsuported type: symbol", + }, + ); + assert.throws( + () => { + isEmpty(() => {}); + }, + { + message: "Unsuported type: function", + }, + ); +}); diff --git a/src/lib/slugify.test.js b/src/lib/slugify.test.js new file mode 100644 index 0000000..11935c1 --- /dev/null +++ b/src/lib/slugify.test.js @@ -0,0 +1,16 @@ +import { test } from "node:test"; +import assert from "node:assert/strict"; + +import { slugify } from "./slugify.js"; + +test("slugify replaces spaces with hyphens", (t) => { + const actual = slugify("hello world"); + const expected = "hello-world"; + assert.equal(actual, expected); +}); + +test("slugify replaces special characters with empty strings", (t) => { + const actual = slugify("hello!@#$%^&*()"); + const expected = "hello"; + assert.equal(actual, expected); +});