Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #78

Merged
merged 3 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh dear, I am such a python programmer.....

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and I missed a typo!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crap, I'll put up another PR!

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);
});
Loading