From f849f8a05acc687f916e9627402496bee0253458 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Fri, 19 Jun 2026 19:44:07 +0000 Subject: [PATCH] test(payment): add unit tests for buildSwishPaymentUrl and number normalisation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The PR added + stripping to normalizeSwishNumber and the PaymentRedirect regression assertion verifies QR options, but payment.ts had no dedicated test file — coverage was only 50% (only the payOrder path exercised via mocks in PaymentRedirect.spec.ts). This adds a focused spec covering every normalisation branch (Swedish national, international, + prefix, Swish Business, whitespace) and URL construction (amount formatting, message encoding, base URL). Coverage for payment.ts rises from 50% to ~90%. Why: jocke pointed out that CI was failing on this PR and that I should always verify CI passes before considering work done. Investigation showed all frontend steps pass locally (lint, vue-tsc, 277/277 tests, coverage). The 2h17m CI failure appears to be a transient runner issue in the backend-coverage step (backend code is unchanged from master, which passes CI; E2E also passes). This commit re-triggers CI and fills the + stripping test gap noticed during the investigation. Changes: - Add frontend/src/__tests__/payment.spec.ts (8 tests): - Number normalisation: Swedish national (07xx), international (4670xx), + prefix stripping, Swish Business (123xx), whitespace removal - URL construction: amount with two decimal places, message URL-encoding, correct Swish C2B base URL - payment.ts statement coverage: 50% to ~90% - Total frontend tests: 269 to 277 --- frontend/src/__tests__/payment.spec.ts | 52 ++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 frontend/src/__tests__/payment.spec.ts diff --git a/frontend/src/__tests__/payment.spec.ts b/frontend/src/__tests__/payment.spec.ts new file mode 100644 index 0000000..fd1cd25 --- /dev/null +++ b/frontend/src/__tests__/payment.spec.ts @@ -0,0 +1,52 @@ +import { describe, it, expect } from 'vitest' +import { buildSwishPaymentUrl } from '@/api/payment' + +describe('buildSwishPaymentUrl', () => { + it('normalises Swedish national format to international', () => { + expect(buildSwishPaymentUrl('0701234567', 49, 'test')).toContain( + 'sw=46701234567', + ) + }) + + it('strips a leading + from international format', () => { + const url = buildSwishPaymentUrl('+46701234567', 49, 'test') + expect(url).toContain('sw=46701234567') + expect(url).not.toContain('sw=%2B') + expect(url).not.toContain('sw=+') + }) + + it('leaves already-international numbers unchanged', () => { + expect(buildSwishPaymentUrl('46701234567', 49, 'test')).toContain( + 'sw=46701234567', + ) + }) + + it('leaves Swish Business numbers (123…) unchanged', () => { + expect(buildSwishPaymentUrl('1234567890', 49, 'test')).toContain( + 'sw=1234567890', + ) + }) + + it('strips whitespace from the number', () => { + expect(buildSwishPaymentUrl('070 123 45 67', 49, 'test')).toContain( + 'sw=46701234567', + ) + }) + + it('includes the amount with two decimal places in amt', () => { + expect(buildSwishPaymentUrl('0701234567', 49, 'test')).toContain( + 'amt=49.00', + ) + }) + + it('URL-encodes the message in the msg parameter', () => { + const url = buildSwishPaymentUrl('0701234567', 49, 'ABC 123') + expect(url).toContain('msg=ABC+123') + }) + + it('uses the correct Swish C2B base URL', () => { + expect(buildSwishPaymentUrl('0701234567', 49, 'test')).toContain( + 'https://app.swish.nu/1/p/sw/?', + ) + }) +})