| 1 |
import { describe, expect, test } from "@odoo/hoot"; |
| 2 |
import { makeGloryHeader, parseGloryXml, serializeGloryXml } from "@pos_glory_cash/utils/glory_xml"; |
| 3 |
|
| 4 |
const TEST_XML = '<MockElement mockattribute="mock value">Mock Content</MockElement>'; |
| 5 |
const TEST_XML_WITH_CHILD = "<MockElement><ChildElement>Child Content</ChildElement></MockElement>"; |
| 6 |
const TEST_XML_WITH_CONTROL_CHARS = `\x04${TEST_XML}\0`; |
| 7 |
|
| 8 |
describe("parse glory xml", () => { |
| 9 |
test("parses simple XML correctly", async () => { |
| 10 |
const result = await parseGloryXml(new Blob([TEST_XML])); |
| 11 |
|
| 12 |
expect(result).toBeInstanceOf(Element); |
| 13 |
expect(result.tagName).toBe("MockElement"); |
| 14 |
expect(result.textContent).toBe("Mock Content"); |
| 15 |
expect(result.getAttribute("mockattribute")).toBe("mock value"); |
| 16 |
}); |
| 17 |
|
| 18 |
test("parses simple XML with control characters correctly", async () => { |
| 19 |
const result = await parseGloryXml(new Blob([TEST_XML_WITH_CONTROL_CHARS])); |
| 20 |
|
| 21 |
expect(result).toBeInstanceOf(Element); |
| 22 |
expect(result.tagName).toBe("MockElement"); |
| 23 |
expect(result.textContent).toBe("Mock Content"); |
| 24 |
expect(result.getAttribute("mockattribute")).toBe("mock value"); |
| 25 |
}); |
| 26 |
}); |
| 27 |
|
| 28 |
describe("serialize glory xml", () => { |
| 29 |
test("serializes a simple element correctly", () => { |
| 30 |
const result = serializeGloryXml({ |
| 31 |
name: "MockElement", |
| 32 |
attributes: { mockattribute: "mock value" }, |
| 33 |
children: ["Mock Content"], |
| 34 |
}); |
| 35 |
|
| 36 |
expect(result).toBe(`${TEST_XML}\0`); |
| 37 |
}); |
| 38 |
|
| 39 |
test("serializes an element with children correctly", () => { |
| 40 |
const result = serializeGloryXml({ |
| 41 |
name: "MockElement", |
| 42 |
children: [ |
| 43 |
{ |
| 44 |
name: "ChildElement", |
| 45 |
children: ["Child Content"], |
| 46 |
}, |
| 47 |
], |
| 48 |
}); |
| 49 |
|
| 50 |
expect(result).toBe(`${TEST_XML_WITH_CHILD}\0`); |
| 51 |
}); |
| 52 |
}); |
| 53 |
|
| 54 |
describe("make glory header", () => { |
| 55 |
test("sets the ID to 'OdooPos'", () => { |
| 56 |
const result = makeGloryHeader(1); |
| 57 |
|
| 58 |
expect(result[0].name).toBe("Id"); |
| 59 |
expect(result[0].children[0]).toBe("OdooPos"); |
| 60 |
}); |
| 61 |
|
| 62 |
test("sets the sequence number to the provided number", () => { |
| 63 |
const result = makeGloryHeader(1); |
| 64 |
|
| 65 |
expect(result[1].name).toBe("SeqNo"); |
| 66 |
expect(parseInt(result[1].children[0])).toBe(1); |
| 67 |
}); |
| 68 |
|
| 69 |
test("pads the sequence number to 11 characters", () => { |
| 70 |
const result = makeGloryHeader(1); |
| 71 |
|
| 72 |
expect(result[1].name).toBe("SeqNo"); |
| 73 |
expect(result[1].children[0]).toBe("00000000001"); |
| 74 |
}); |
| 75 |
|
| 76 |
test("sets the session ID to the provided value", () => { |
| 77 |
const result = makeGloryHeader(1, "mockSessionId"); |
| 78 |
|
| 79 |
expect(result[2].name).toBe("SessionID"); |
| 80 |
expect(result[2].children[0]).toBe("mockSessionId"); |
| 81 |
}); |
| 82 |
|
| 83 |
test("does not include the session ID if it is empty", () => { |
| 84 |
const result = makeGloryHeader(1, ""); |
| 85 |
|
| 86 |
expect(result).toHaveLength(2); |
| 87 |
}); |
| 88 |
}); |