librarian-web/__tests__/lib/book.test.ts

118 lines
2.8 KiB
TypeScript

import { DetailedBook } from "@/lib/book";
import { cloneInstance } from "@/lib/utils";
import type { Book } from "@/models/book";
describe("DetailedBook constructor tests", () => {
test("Correct book full", () => {
expect(
() =>
new DetailedBook({
id: 1,
title: "The Title",
category: ["fiction", "signed"],
last_updated: new Date(),
deleted: false,
subtitle: "🐄🐮🐄🐮🐄",
authors: "Polish cow; Tom Scott",
isbn: "1234567890",
lccn: "1234567890",
edition: "1st",
publisher: "The Publisher",
publish_year: 2021,
publish_month: "January",
publish_location: "Poland",
pages: 100,
weight: "100g",
})
).not.toThrow();
});
test("Correct book partial", () => {
expect(
() =>
new DetailedBook({
id: 1,
title: "The Title",
category: ["fiction", "signed"],
isbn: "1234567890",
authors: "Polish cow; Tom Scott",
subtitle: "🐄🐮🐄🐮🐄",
})
).not.toThrow();
});
test("Incorrect book no title", () => {
expect(
() =>
new DetailedBook({
id: 1,
category: ["fiction", "signed"],
last_updated: new Date("2021-01-01T00:00:00Z"),
deleted: false,
subtitle: "🐄🐮🐄🐮🐄",
authors: "Polish cow",
isbn: "1234567890",
lccn: "1234567890",
edition: "1st",
publisher: "The Publisher",
publish_year: 2021,
publish_month: "January",
publish_location: "Poland",
pages: 100,
weight: "100g",
} as Book)
).toThrow();
});
test("Incorrect book bad date", () => {
expect(
() =>
new DetailedBook({
id: 1,
title: "The Title",
category: ["fiction", "signed"],
last_updated: "banana",
deleted: false,
subtitle: "🐄🐮🐄🐮🐄",
authors: "Polish cow",
isbn: "1234567890",
lccn: "1234567890",
edition: "1st",
publisher: "The Publisher",
publish_year: 2021,
publish_month: "January",
publish_location: "Poland",
pages: 100,
weight: "100g",
} as unknown as Book)
).toThrow();
});
});
describe("Getters and setters", () => {
const book = new DetailedBook({
id: 1,
title: "The Title",
category: ["fiction", "signed"],
isbn: "1234567890",
authors: "Polish cow; Tom Scott",
subtitle: "🐄🐮🐄🐮🐄",
});
test("authorsList getter", () => {
const clone = cloneInstance(book);
clone.authors = null;
expect(book.authorsList).toEqual(["Polish cow", "Tom Scott"]);
expect(book.authorsList.find((v) => v.includes(";"))).toBeUndefined();
expect(clone.authorsList).toBeInstanceOf(Array);
expect(clone.authorsList).toHaveLength(0);
});
test("authorsList setter", () => {
const clone = cloneInstance(book);
clone.authorsList = ["Tom Scott", "Polish cow"];
expect(clone.authors).toEqual("Tom Scott; Polish cow");
});
});