/** * @copyright NHCarrigan * @license Naomi's Public License * @author Naomi Carrigan */ import { BookStatus } from "../src/lib/book.types"; import type { Book, CreateBookDto, UpdateBookDto } from "../src/lib/book.types"; describe("book Types", () => { describe("bookStatus enum", () => { it("should have the correct values", () => { expect(BookStatus.reading).toBe("READING"); expect(BookStatus.finished).toBe("FINISHED"); expect(BookStatus.toRead).toBe("TO_READ"); }); it("should have all expected enum values", () => { const values = Object.values(BookStatus); expect(values).toHaveLength(3); expect(values).toContain("READING"); expect(values).toContain("FINISHED"); expect(values).toContain("TO_READ"); }); }); describe("book interface", () => { it("should accept valid book object with minimal fields", () => { const book: Book = { author: "F. Scott Fitzgerald", createdAt: new Date("2024-01-15"), dateAdded: new Date("2024-01-15"), id: "book123", links: [], status: BookStatus.reading, tags: [], title: "The Great Gatsby", updatedAt: new Date("2024-01-16"), }; expect(book.isbn).toBeUndefined(); expect(book.dateFinished).toBeUndefined(); expect(book.rating).toBeUndefined(); expect(book.notes).toBeUndefined(); expect(book.coverImage).toBeUndefined(); }); it("should accept valid book object with all fields", () => { const fullBook: Book = { author: "George Orwell", coverImage: "https://example.com/1984-cover.jpg", createdAt: new Date("2024-01-01"), dateAdded: new Date("2024-01-01"), dateFinished: new Date("2024-01-20"), id: "book456", isbn: "978-0-452-28423-4", links: [ { title: "Goodreads", url: "https://goodreads.com/book/1984" }, { title: "Wikipedia", url: "https://wikipedia.org/wiki/1984" }, ], notes: "A dystopian masterpiece that remains relevant", rating: 5, status: BookStatus.finished, tags: [ "dystopian", "classic", "political" ], title: "1984", updatedAt: new Date("2024-01-20"), }; expect(fullBook.isbn).toBe("978-0-452-28423-4"); expect(fullBook.rating).toBe(5); expect(fullBook.tags).toHaveLength(3); expect(fullBook.links).toHaveLength(2); }); }); describe("createBookDto interface", () => { it("should accept DTO with required fields only", () => { const createDto: CreateBookDto = { author: "Harper Lee", status: BookStatus.toRead, title: "To Kill a Mockingbird", }; expect(createDto.isbn).toBeUndefined(); expect(createDto.rating).toBeUndefined(); expect(createDto.notes).toBeUndefined(); expect(createDto.coverImage).toBeUndefined(); expect(createDto.tags).toBeUndefined(); expect(createDto.links).toBeUndefined(); }); it("should accept DTO with all fields", () => { const fullCreateDto: CreateBookDto = { author: "J.R.R. Tolkien", coverImage: "https://example.com/hobbit-cover.jpg", isbn: "978-0-547-92822-7", links: [ { title: "Author Website", url: "https://tolkien.com" } ], notes: "Starting the journey to Middle-earth", rating: 4, status: BookStatus.reading, tags: [ "fantasy", "adventure", "classic" ], title: "The Hobbit", }; expect(fullCreateDto.isbn).toBe("978-0-547-92822-7"); expect(fullCreateDto.rating).toBe(4); expect(fullCreateDto.tags).toEqual([ "fantasy", "adventure", "classic" ]); expect(fullCreateDto.links).toHaveLength(1); }); }); describe("updateBookDto type", () => { it("should accept empty update DTO", () => { const emptyUpdate: UpdateBookDto = {}; expect(emptyUpdate).toEqual({}); }); it("should accept partial updates including dateFinished", () => { const partialUpdate: UpdateBookDto = { dateFinished: new Date("2024-02-01"), rating: 5, status: BookStatus.finished, }; expect(partialUpdate.status).toBe(BookStatus.finished); expect(partialUpdate.dateFinished).toEqual(new Date("2024-02-01")); expect(partialUpdate.rating).toBe(5); expect(partialUpdate.title).toBeUndefined(); }); it("should accept full update", () => { const fullUpdate: UpdateBookDto = { author: "Different Author", coverImage: "https://example.com/new-cover.jpg", dateFinished: new Date("2024-02-15"), isbn: "978-1-234-56789-0", links: [ { title: "New Link", url: "https://newlink.com" } ], notes: "Updated notes after finishing", rating: 3, status: BookStatus.finished, tags: [ "updated", "tags" ], title: "Updated Title", }; expect(fullUpdate.title).toBe("Updated Title"); expect(fullUpdate.dateFinished).toEqual(new Date("2024-02-15")); expect(fullUpdate.links).toHaveLength(1); }); }); });