|
| 1 | +import { pxToPt, getNameStyles } from "./utils" |
| 2 | + |
| 3 | +jest.mock("@react-pdf/renderer", () => ({ |
| 4 | + Document: "Document", |
| 5 | + Page: "Page", |
| 6 | + View: "View", |
| 7 | + Text: "Text", |
| 8 | + Svg: "Svg", |
| 9 | + G: "G", |
| 10 | + Path: "Path", |
| 11 | + Font: { |
| 12 | + register: jest.fn(), |
| 13 | + registerHyphenationCallback: jest.fn(), |
| 14 | + }, |
| 15 | + Image: "Image", |
| 16 | + pdf: jest.fn(), |
| 17 | +})) |
| 18 | + |
| 19 | +describe("Certificate PDF", () => { |
| 20 | + describe("pxToPt", () => { |
| 21 | + it("converts pixels to points correctly", () => { |
| 22 | + expect(pxToPt(96)).toBeCloseTo(57.6) // 96 * (72/96) * 0.8 |
| 23 | + expect(pxToPt(52)).toBeCloseTo(31.2) // 52 * 0.75 * 0.8 |
| 24 | + expect(pxToPt(206)).toBeCloseTo(123.6) |
| 25 | + expect(pxToPt(950)).toBeCloseTo(570) |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + describe("Name scaling to ensure page fit", () => { |
| 30 | + const baseFontSize = pxToPt(52) |
| 31 | + const baselineTop = pxToPt(206) |
| 32 | + |
| 33 | + it("keeps short names <= 23 chars at full size", () => { |
| 34 | + const shortName = "Wolfgang Amadeus Mozart" |
| 35 | + const styles = getNameStyles(shortName) |
| 36 | + |
| 37 | + expect(styles.fontSize).toBeCloseTo(baseFontSize) |
| 38 | + expect(styles.top).toBeCloseTo(baselineTop) |
| 39 | + }) |
| 40 | + |
| 41 | + it("scales medium-length names <= 33 chars proportionally", () => { |
| 42 | + const mediumName = "Sir Bartholomew Fizzlewhisk III" |
| 43 | + const styles = getNameStyles(mediumName) |
| 44 | + |
| 45 | + expect(styles.fontSize).toBeLessThan(baseFontSize) |
| 46 | + expect(styles.fontSize).toBeGreaterThan(baseFontSize * 0.35) |
| 47 | + |
| 48 | + expect(styles.top).toBeGreaterThan(baselineTop) |
| 49 | + }) |
| 50 | + |
| 51 | + it("scales longer names <= 47 chars more aggressively", () => { |
| 52 | + const longName = "Dr. Maximilian Thunderbolt von Schnitzelhausen" |
| 53 | + const styles = getNameStyles(longName) |
| 54 | + |
| 55 | + expect(styles.fontSize).toBeLessThan(baseFontSize) |
| 56 | + expect(styles.top).toBeGreaterThan(baselineTop) |
| 57 | + |
| 58 | + const mediumName = "Sir Bartholomew Fizzlewhisk III" |
| 59 | + const mediumStyles = getNameStyles(mediumName) |
| 60 | + expect(styles.fontSize).toBeLessThan(mediumStyles.fontSize) |
| 61 | + }) |
| 62 | + |
| 63 | + it("applies minimum scale factor of 35% for very long names <= 112 chars", () => { |
| 64 | + const veryLongName = |
| 65 | + "His Excellency Count Maximilian Cornelius Archibald Pumpernickel-Wigglesworth-Thunderbolt-Whiskerdoodle III" |
| 66 | + const styles = getNameStyles(veryLongName) |
| 67 | + |
| 68 | + const minFontSize = baseFontSize * 0.35 |
| 69 | + expect(styles.fontSize).toBeCloseTo(minFontSize) |
| 70 | + |
| 71 | + const maxOffset = baseFontSize - minFontSize |
| 72 | + expect(styles.top).toBeCloseTo(baselineTop + maxOffset) |
| 73 | + }) |
| 74 | + |
| 75 | + it("maintains baseline alignment when scaling names", () => { |
| 76 | + const names = [ |
| 77 | + "Wolfgang Amadeus Mozart", |
| 78 | + "Sir Bartholomew Fizzlewhisk III", |
| 79 | + "Princess Anastasia Beauregard-Winterstone", |
| 80 | + "Dr. Maximilian Thunderbolt von Schnitzelhausen", |
| 81 | + "Captain Cornelius Pumpernickel-Whiskerdoodle Jones", |
| 82 | + "Lady Penelope Wigglesworth-Featherstone de la Fontaine", |
| 83 | + "Professor Archibald Bartholomew Higginbotham-Waffletop IV", |
| 84 | + ] |
| 85 | + |
| 86 | + names.forEach((name) => { |
| 87 | + const styles = getNameStyles(name) |
| 88 | + |
| 89 | + // The baseline position should be consistent: baseline = top + fontSize |
| 90 | + const baseline = styles.top + styles.fontSize |
| 91 | + expect(baseline).toBeCloseTo(baselineTop + baseFontSize, 1) |
| 92 | + }) |
| 93 | + }) |
| 94 | + }) |
| 95 | +}) |
0 commit comments