|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { modelPositionFromRenderedTopLeft } from "./transformPosition"; |
| 3 | +import { QR_FO_Y_OFFSET_DOTS } from "./bwipConstants"; |
| 4 | +import type { LabelObject } from "../../registry"; |
| 5 | + |
| 6 | +const qrFo: LabelObject = { |
| 7 | + id: "q1", |
| 8 | + type: "qrcode", |
| 9 | + x: 0, |
| 10 | + y: 0, |
| 11 | + rotation: 0, |
| 12 | + positionType: "FO", |
| 13 | + props: { content: "x", magnification: 4, errorCorrection: "Q" }, |
| 14 | +}; |
| 15 | + |
| 16 | +const ellipse: LabelObject = { |
| 17 | + id: "e1", |
| 18 | + type: "ellipse", |
| 19 | + x: 0, |
| 20 | + y: 0, |
| 21 | + rotation: 0, |
| 22 | + props: { width: 150, height: 100, thickness: 3, filled: false, color: "B" }, |
| 23 | +}; |
| 24 | + |
| 25 | +describe("modelPositionFromRenderedTopLeft", () => { |
| 26 | + it("subtracts QR FO Y offset for QR codes with positionType=FO", () => { |
| 27 | + expect(modelPositionFromRenderedTopLeft(qrFo, 100, 200)).toEqual({ |
| 28 | + x: 100, |
| 29 | + y: 200 - QR_FO_Y_OFFSET_DOTS, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("subtracts QR FO Y offset when positionType is undefined (defaults to FO)", () => { |
| 34 | + const obj = { ...qrFo, positionType: undefined }; |
| 35 | + expect(modelPositionFromRenderedTopLeft(obj, 100, 200)).toEqual({ |
| 36 | + x: 100, |
| 37 | + y: 200 - QR_FO_Y_OFFSET_DOTS, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("does not subtract Y offset for QR codes with positionType=FT", () => { |
| 42 | + const obj: LabelObject = { ...qrFo, positionType: "FT" }; |
| 43 | + expect(modelPositionFromRenderedTopLeft(obj, 100, 200)).toEqual({ |
| 44 | + x: 100, |
| 45 | + y: 200, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("returns rendered position unchanged for ellipse", () => { |
| 50 | + expect(modelPositionFromRenderedTopLeft(ellipse, 50, 80)).toEqual({ |
| 51 | + x: 50, |
| 52 | + y: 80, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("preserves x for QR codes (only Y is offset)", () => { |
| 57 | + expect(modelPositionFromRenderedTopLeft(qrFo, -7, 17).x).toBe(-7); |
| 58 | + }); |
| 59 | + |
| 60 | + it("is idempotent under repeated application for non-QR types", () => { |
| 61 | + const once = modelPositionFromRenderedTopLeft(ellipse, 10, 20); |
| 62 | + const twice = modelPositionFromRenderedTopLeft(ellipse, once.x, once.y); |
| 63 | + expect(twice).toEqual(once); |
| 64 | + }); |
| 65 | +}); |
0 commit comments