From 60f90d6a37049dfdbcb477ab743a0b8904b4134e Mon Sep 17 00:00:00 2001 From: xenticore Date: Mon, 24 Mar 2025 19:38:14 -0400 Subject: [PATCH] test: add price formatting tests --- __tests__/formatting.test.ts | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 __tests__/formatting.test.ts diff --git a/__tests__/formatting.test.ts b/__tests__/formatting.test.ts new file mode 100644 index 0000000..7c4bf84 --- /dev/null +++ b/__tests__/formatting.test.ts @@ -0,0 +1,49 @@ +import { describe, it, expect, test, jest, mock, beforeEach } from "bun:test"; +import { formatPrice } from '../src/content/utils/formatting'; +import { $T } from '../src/content/utils/localization'; + +mock.module('../src/content/utils/localization', () => ({ + $T: mock((str) => str) +})); + +mock.module('../src/content/config', () => ({ + conversion_ref_usd: 0.05 // Mock conversion rate +})) + +describe('formatPrice', () => { + beforeEach(() => { + jest.clearAllMocks() + }); + + test('formats price with keys and metal', () => { + expect(formatPrice(2, 10, 50)).toBe('2.2 keys (US$5.50)'); + expect($T).toHaveBeenCalledWith('%@ keys'); + }); + + test('formats price with metal only', () => { + expect(formatPrice(0, 15.75, 50)).toBe('15.75 ref (US$0.79)'); + }); + + test('formats price with metal only and whole number', () => { + expect(formatPrice(0, 3, 50)).toBe('3 ref (US$0.16)'); + }); + + test('uses singular key form', () => { + expect(formatPrice(1, 0, 50)).toBe('1 key (US$2.50)'); + expect($T).toHaveBeenCalledWith('%@ key'); + }); + + test('rounds USD up to nearest cent', () => { + expect(formatPrice(3, 7.33, 35)).toBe('3.21 keys (US$5.62)'); // (3*35 +7.33)*0.05 = 5.6165 → 5.62 + }); + + test('handles different locale formatting', () => { + expect(formatPrice(2, 10, 50, 'de')).toMatch(/2,2 keys \(US\$5,50\)/); + expect(formatPrice(0, 15.75, 50, 'de')).toMatch(/15,75 ref \(US\$0,79\)/); + }); + + test('handles zero values', () => { + expect(formatPrice(0, 0, 50)).toBe('0 ref (US$0.00)'); + expect(formatPrice(0, 0, 50, 'de')).toMatch(/0 ref \(US\$0,00\)/); + }); +}); \ No newline at end of file