/ tests / index.test.ts
index.test.ts
  1  import { describe, expect, test } from "bun:test";
  2  import {
  3    Div,
  4    div,
  5    font,
  6    Fonts,
  7    MemoryFont,
  8    Page,
  9    page,
 10    PDF,
 11    pdf,
 12    Text,
 13    text
 14  } from "~/index";
 15  
 16  describe("Main exports", () => {
 17    describe("Element exports", () => {
 18      test("should export Div class and div helper", () => {
 19        expect(Div).toBeDefined();
 20        expect(typeof Div).toBe("function");
 21        expect(div).toBeDefined();
 22        expect(typeof div).toBe("function");
 23  
 24        const divInstance = div();
 25        expect(divInstance).toBeInstanceOf(Div);
 26      });
 27  
 28      test("should export Page class and page helper", () => {
 29        expect(Page).toBeDefined();
 30        expect(typeof Page).toBe("function");
 31        expect(page).toBeDefined();
 32        expect(typeof page).toBe("function");
 33  
 34        const pageInstance = page();
 35        expect(pageInstance).toBeInstanceOf(Page);
 36      });
 37  
 38      test("should export PDF class and pdf helper", () => {
 39        expect(PDF).toBeDefined();
 40        expect(typeof PDF).toBe("function");
 41        expect(pdf).toBeDefined();
 42        expect(typeof pdf).toBe("function");
 43  
 44        const pdfInstance = pdf();
 45        expect(pdfInstance).toBeInstanceOf(PDF);
 46      });
 47  
 48      test("should export Text class and text helper", () => {
 49        expect(Text).toBeDefined();
 50        expect(typeof Text).toBe("function");
 51        expect(text).toBeDefined();
 52        expect(typeof text).toBe("function");
 53  
 54        const textInstance = text("Hello");
 55        expect(textInstance).toBeInstanceOf(Text);
 56      });
 57    });
 58  
 59    describe("Font exports", () => {
 60      test("should export MemoryFont class", () => {
 61        expect(MemoryFont).toBeDefined();
 62        expect(typeof MemoryFont).toBe("function");
 63  
 64        const memoryFont = new MemoryFont("test-font");
 65        expect(memoryFont).toBeInstanceOf(MemoryFont);
 66      });
 67  
 68      test("should export font helper function", () => {
 69        expect(font).toBeDefined();
 70        expect(typeof font).toBe("function");
 71  
 72        const fontInstance = font("test-font");
 73        expect(fontInstance).toBeInstanceOf(MemoryFont);
 74      });
 75  
 76      test("should export Fonts constant", () => {
 77        expect(Fonts).toBeDefined();
 78        expect(typeof Fonts).toBe("object");
 79  
 80        // Check some standard fonts
 81        expect(Fonts.Helvetica).toBeDefined();
 82        expect(Fonts.TimesRoman).toBeDefined();
 83        expect(Fonts.Courier).toBeDefined();
 84      });
 85    });
 86  
 87    describe("Integration test", () => {
 88      test("should create a complete PDF structure using exports", () => {
 89        const customFont = font(Fonts.Helvetica);
 90  
 91        const headerDiv = div()
 92          .w(500)
 93          .h(60)
 94          .bg(0x333333)
 95          .flex()
 96          .itemsCenter()
 97          .justifyCenter()
 98          .child(text("Document Header").font(customFont));
 99  
100        const contentDiv = div()
101          .flex()
102          .flexCol()
103          .child(text("Content line 1"))
104          .child(text("Content line 2"));
105  
106        const footerDiv = div()
107          .w(500)
108          .h(40)
109          .bg(0x666666)
110          .child(text("Footer"));
111  
112        const documentPage = page()
113          .size([500, 700])
114          .child(headerDiv)
115          .child(contentDiv)
116          .child(footerDiv);
117  
118        const document = pdf()
119          .author("Test Author")
120          .font(customFont)
121          .script("init", "console.log('PDF ready');")
122          .child(documentPage);
123  
124        // Verify all components are properly instantiated
125        expect(document).toBeInstanceOf(PDF);
126        expect(documentPage).toBeInstanceOf(Page);
127        expect(headerDiv).toBeInstanceOf(Div);
128        expect(contentDiv).toBeInstanceOf(Div);
129        expect(footerDiv).toBeInstanceOf(Div);
130        expect(customFont).toBeInstanceOf(MemoryFont);
131      });
132    });
133  
134    describe("API consistency", () => {
135      test("should have consistent method chaining across all elements", () => {
136        const customFont = font(Fonts.TimesRoman);
137  
138        // Test that all elements support method chaining
139        const divElement = div()
140          .w(100)
141          .h(100)
142          .bg(0xFF0000)
143          .flex()
144          .itemsCenter();
145  
146        const textElement = text("Sample")
147          .font(customFont)
148          .textCenter()
149          .leading(1.5);
150  
151        const pageElement = page()
152          .w(400)
153          .h(600)
154          .child(divElement)
155          .child(textElement);
156  
157        const pdfElement = pdf()
158          .author("Chaining Test")
159          .font(customFont)
160          .child(pageElement);
161  
162        expect(divElement).toBeInstanceOf(Div);
163        expect(textElement).toBeInstanceOf(Text);
164        expect(pageElement).toBeInstanceOf(Page);
165        expect(pdfElement).toBeInstanceOf(PDF);
166      });
167    });
168  });