Skip to content

Commit

Permalink
Unit tests (#78)
Browse files Browse the repository at this point in the history
* Fixed pythonic boolean.

* type = module to make imports work.

* Unit tests for stuff in lib.
  • Loading branch information
mdellabitta authored Feb 26, 2024
1 parent a65d857 commit 469746b
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

{
"type": "module",
"scripts": {
"prettier-format-check": "prettier --check src/*",
"prettier-format-fix": "prettier --write src/*"
Expand Down
74 changes: 74 additions & 0 deletions src/lib/data.test.js
Original file line number Diff line number Diff line change
@@ -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, /<section/);
});
});

test("fetchCaseMetadata can load a case metadata file", async (t) => {
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);
});
38 changes: 38 additions & 0 deletions src/lib/fetchOr404.test.js
Original file line number Diff line number Diff line change
@@ -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");
});
4 changes: 2 additions & 2 deletions src/lib/isEmpty.js
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
81 changes: 81 additions & 0 deletions src/lib/isEmpty.test.js
Original file line number Diff line number Diff line change
@@ -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",
},
);
});
16 changes: 16 additions & 0 deletions src/lib/slugify.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 469746b

Please sign in to comment.