| 1 |
import { expect, getFixture, test } from "@odoo/hoot"; |
| 2 |
import { mockFetch } from "@odoo/hoot-mock"; |
| 3 |
import { Component, xml } from "@odoo/owl"; |
| 4 |
import { allowTranslations, mountWithCleanup } from "@web/../tests/web_test_helpers"; |
| 5 |
import { htmlToCanvas } from "@point_of_sale/app/services/render_service"; |
| 6 |
import { definePosModels } from "../data/generate_model_definitions"; |
| 7 |
|
| 8 |
definePosModels(); |
| 9 |
odoo.pos_session_id = 1; // Ensure the session ID is set for lazy getters |
| 10 |
|
| 11 |
test("test the render service", async () => { |
| 12 |
class ComponentToBeRendered extends Component { |
| 13 |
static props = ["name"]; |
| 14 |
static template = xml` |
| 15 |
<div> It's me, <t t-esc="props.name" />! </div> |
| 16 |
`; |
| 17 |
} |
| 18 |
|
| 19 |
allowTranslations(); // this is needed because we are not loading the localization service |
| 20 |
const comp = await mountWithCleanup("none"); |
| 21 |
const renderedComp = await comp.env.services.renderer.toHtml(ComponentToBeRendered, { |
| 22 |
name: "Mario", |
| 23 |
}); |
| 24 |
expect(renderedComp).toHaveOuterHTML("<div> It's me, Mario! </div>"); |
| 25 |
}); |
| 26 |
|
| 27 |
test("htmlToCanvas", async () => { |
| 28 |
// htmlToCanvas fetches some fonts useless for the test, we mock it to avoid warnings |
| 29 |
mockFetch(() => ""); |
| 30 |
const target = getFixture(); |
| 31 |
const node = document.createElement("div"); |
| 32 |
node.classList.add("render-container"); |
| 33 |
target.appendChild(node); |
| 34 |
|
| 35 |
const asciiChars = Array.from({ length: 256 }, (_, i) => String.fromCharCode(i)).join(""); |
| 36 |
node.textContent = asciiChars; |
| 37 |
|
| 38 |
let canvas = null; |
| 39 |
try { |
| 40 |
canvas = await htmlToCanvas(node, { addClass: "pos-receipt-print" }); |
| 41 |
} catch (error) { |
| 42 |
// htmlToCanvas create an <img> by setting a svg to its src attribute |
| 43 |
// if this fails, an Event of type "error" is thrown |
| 44 |
if (error.constructor.name !== "Event") { |
| 45 |
throw error; |
| 46 |
} |
| 47 |
} |
| 48 |
expect(canvas).not.toBe(null, { |
| 49 |
message: "htmlToCanvas should work with all ascii characters", |
| 50 |
}); |
| 51 |
}); |