77 lines
2.3 KiB
TypeScript
77 lines
2.3 KiB
TypeScript
import assert from "node:assert/strict"
|
|
import { describe, it } from "node:test"
|
|
import { computeGraphicLayoutStyle, resizeWithHandle } from "./docs-graphic-layout.ts"
|
|
import { normalizeImportedGraphics } from "./docs-graphic-import.ts"
|
|
import { DOCS_GRAPHIC_DEFAULTS } from "./docs-graphic-types.ts"
|
|
|
|
describe("docs-graphic", () => {
|
|
it("computeGraphicLayoutStyle applies float wrap", () => {
|
|
const layout = computeGraphicLayoutStyle({
|
|
...DOCS_GRAPHIC_DEFAULTS,
|
|
graphicType: "image",
|
|
wrap: "square",
|
|
floatSide: "left",
|
|
})
|
|
assert.equal(layout.inner.float, "left")
|
|
})
|
|
|
|
it("computeGraphicLayoutStyle applies absolute placement", () => {
|
|
const layout = computeGraphicLayoutStyle({
|
|
...DOCS_GRAPHIC_DEFAULTS,
|
|
graphicType: "shape",
|
|
placement: "absolute",
|
|
x: 40,
|
|
y: 20,
|
|
})
|
|
assert.equal(layout.inner.position, "absolute")
|
|
assert.equal(layout.inner.left, 40)
|
|
assert.equal(layout.inner.top, 20)
|
|
})
|
|
|
|
it("resizeWithHandle respects minimum size", () => {
|
|
const next = resizeWithHandle("se", 120, 80, -200, -200)
|
|
assert.equal(next.width, 24)
|
|
assert.equal(next.height, 24)
|
|
})
|
|
|
|
it("resizeWithHandle respects aspect lock", () => {
|
|
const next = resizeWithHandle("se", 200, 100, 100, 50, 24, true)
|
|
assert.equal(next.width, 300)
|
|
assert.equal(next.height, 150)
|
|
})
|
|
|
|
it("normalizeImportedGraphics upgrades standalone image paragraph", () => {
|
|
const result = normalizeImportedGraphics({
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "paragraph",
|
|
content: [{ type: "image", attrs: { src: "data:image/png;base64,abc", width: 200 } }],
|
|
},
|
|
],
|
|
})
|
|
assert.equal(result.content?.[0]?.type, "docsGraphic")
|
|
assert.equal((result.content?.[0]?.attrs as { graphicType?: string }).graphicType, "image")
|
|
})
|
|
|
|
it("normalizeImportedGraphics maps docx wrap aliases", () => {
|
|
const result = normalizeImportedGraphics({
|
|
type: "doc",
|
|
content: [
|
|
{
|
|
type: "docsGraphic",
|
|
attrs: {
|
|
graphicType: "image",
|
|
src: "https://example.com/a.png",
|
|
wrap: "topAndBottom",
|
|
placement: "anchored",
|
|
},
|
|
},
|
|
],
|
|
})
|
|
const attrs = result.content?.[0]?.attrs as { wrap?: string; placement?: string }
|
|
assert.equal(attrs.wrap, "top-bottom")
|
|
assert.equal(attrs.placement, "absolute")
|
|
})
|
|
})
|