UNPKG

umay-render

Version:

Free, high-performance HTML to PDF and HTML to Image conversion SDK for both browser and Node.js

89 lines (88 loc) 3.68 kB
"use strict"; /// <reference types="jest" /> Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); const http_client_1 = require("../http-client"); // Mock HttpClient jest.mock("../http-client"); describe("UmaySDK", () => { let sdk; beforeEach(() => { // Clear all mocks before each test jest.clearAllMocks(); sdk = new index_1.UmaySDK(); }); describe("toPDF", () => { it("should call HttpClient with correct parameters", async () => { const mockBuffer = Buffer.from("test"); http_client_1.HttpClient.prototype.request.mockResolvedValue(mockBuffer); const html = "<html><body>Test</body></html>"; const options = { format: "A4", landscape: false, }; const result = await sdk.toPDF(html, options); expect(http_client_1.HttpClient.prototype.request).toHaveBeenCalledWith("/render/pdf", { html, options: expect.objectContaining(options), }); expect(result).toBe(mockBuffer); }); it("should use default options when none provided", async () => { const mockBuffer = Buffer.from("test"); http_client_1.HttpClient.prototype.request.mockResolvedValue(mockBuffer); const html = "<html><body>Test</body></html>"; await sdk.toPDF(html); expect(http_client_1.HttpClient.prototype.request).toHaveBeenCalledWith("/render/pdf", { html, options: expect.any(Object), }); }); }); describe("toImage", () => { it("should call HttpClient with correct parameters", async () => { const mockBuffer = Buffer.from("test"); http_client_1.HttpClient.prototype.request.mockResolvedValue(mockBuffer); const html = "<html><body>Test</body></html>"; const options = { quality: 90, fullPage: true, }; const result = await sdk.toImage(html, options); expect(http_client_1.HttpClient.prototype.request).toHaveBeenCalledWith("/render/image", { html, options: expect.objectContaining(options), }); expect(result).toBe(mockBuffer); }); it("should use default options when none provided", async () => { const mockBuffer = Buffer.from("test"); http_client_1.HttpClient.prototype.request.mockResolvedValue(mockBuffer); const html = "<html><body>Test</body></html>"; await sdk.toImage(html); expect(http_client_1.HttpClient.prototype.request).toHaveBeenCalledWith("/render/image", { html, options: expect.any(Object), }); }); it("should work with viewport option", async () => { const mockBuffer = Buffer.from("test"); http_client_1.HttpClient.prototype.request.mockResolvedValue(mockBuffer); const html = "<html><body>Test</body></html>"; const options = { quality: 100, fullPage: false, viewport: { width: 800, height: 600, deviceScaleFactor: 2, }, }; await sdk.toImage(html, options); expect(http_client_1.HttpClient.prototype.request).toHaveBeenCalledWith("/render/image", { html, options: expect.objectContaining(options), }); }); }); });