This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { buildWorkbook } from "../core/xlsx.js"
|
||||
import { unzip } from "./support/unzip.js"
|
||||
|
||||
describe("the workbook writer", () => {
|
||||
const book = buildWorkbook("Approved lines", [
|
||||
["Label", "Value", "Page"],
|
||||
["Total", "12,50", 2],
|
||||
])
|
||||
const files = unzip(book)
|
||||
|
||||
it("writes the parts a spreadsheet application needs to open the file", () => {
|
||||
expect([...files.keys()].sort()).toEqual([
|
||||
"[Content_Types].xml",
|
||||
"_rels/.rels",
|
||||
"xl/_rels/workbook.xml.rels",
|
||||
"xl/styles.xml",
|
||||
"xl/workbook.xml",
|
||||
"xl/worksheets/sheet1.xml",
|
||||
])
|
||||
expect(files.get("xl/workbook.xml")).toContain('<sheet name="Approved lines" sheetId="1" r:id="rId1"/>')
|
||||
})
|
||||
|
||||
it("keeps text as text and a number as a number, in the cell it was given", () => {
|
||||
const sheet = files.get("xl/worksheets/sheet1.xml") ?? ""
|
||||
expect(sheet).toContain('<c r="A1" t="inlineStr" s="1"><is><t xml:space="preserve">Label</t></is></c>')
|
||||
expect(sheet).toContain('<c r="B2" t="inlineStr"><is><t xml:space="preserve">12,50</t></is></c>')
|
||||
expect(sheet).toContain('<c r="C2"><v>2</v></c>')
|
||||
})
|
||||
|
||||
it("writes a formula-looking value as inert text, never as a formula", () => {
|
||||
const sheet =
|
||||
unzip(buildWorkbook("S", [['=HYPERLINK("http://x")', "+1+1", "@SUM(A1)", "-2+3"]])).get(
|
||||
"xl/worksheets/sheet1.xml",
|
||||
) ?? ""
|
||||
expect(sheet).not.toContain("<f>")
|
||||
expect(sheet.match(/t="inlineStr"/g)).toHaveLength(4)
|
||||
expect(sheet).toContain("=HYPERLINK("http://x")")
|
||||
})
|
||||
|
||||
it("escapes markup and drops characters XML cannot carry", () => {
|
||||
const sheet = unzip(buildWorkbook("S", [["a<b>&\u0000c\u0007d"]])).get("xl/worksheets/sheet1.xml") ?? ""
|
||||
expect(sheet).toContain("a<b>&cd")
|
||||
expect([...sheet].some((char) => char.charCodeAt(0) < 9)).toBe(false)
|
||||
})
|
||||
|
||||
it("refuses a sheet name Excel would reject", () => {
|
||||
for (const name of ["", "a/b", "x".repeat(32), "[a]", "a:b"]) {
|
||||
expect(() => buildWorkbook(name, [])).toThrow(/sheet name/)
|
||||
}
|
||||
})
|
||||
|
||||
it("is byte-for-byte reproducible, so an export of the same lines is the same file", () => {
|
||||
expect(Buffer.from(buildWorkbook("S", [["a"]])).equals(Buffer.from(buildWorkbook("S", [["a"]])))).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user