|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { iban } from "../index"; |
| 3 | + |
| 4 | +describe("IBAN Validation", () => { |
| 5 | + it("should validate correct IBANs", () => { |
| 6 | + const validIbans = [ |
| 7 | + "DE89370400440532013000", |
| 8 | + "GB29NWBK60161331926819", |
| 9 | + "FR1420041010050500013M02606", |
| 10 | + "NL91ABNA0417164300", |
| 11 | + ]; |
| 12 | + |
| 13 | + for (const validIban of validIbans) { |
| 14 | + expect(iban.isValid(validIban)).toBe(true); |
| 15 | + } |
| 16 | + }); |
| 17 | + |
| 18 | + it("should not validate incorrect IBANs", () => { |
| 19 | + const invalidIbans = [ |
| 20 | + "DE89370400440532013001", |
| 21 | + "GB29NWBK6016133192681", |
| 22 | + "FR1420041010050500013M0260699", |
| 23 | + "XX91ABNA0417164300", |
| 24 | + "", |
| 25 | + "NOT-AN-IBAN", |
| 26 | + ]; |
| 27 | + |
| 28 | + for (const invalidIban of invalidIbans) { |
| 29 | + expect(iban.isValid(invalidIban)).not.toBe(true); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it("should validate IBANs with spaces when clean option is true", () => { |
| 34 | + const formattedIbans = [ |
| 35 | + "DE89 3704 0044 0532 0130 00", |
| 36 | + "GB29 NWBK 6016 1331 9268 19", |
| 37 | + "FR14 2004 1010 0505 0001 3M02 606", |
| 38 | + ]; |
| 39 | + |
| 40 | + for (const formattedIban of formattedIbans) { |
| 41 | + expect(iban.isValid(formattedIban, true)).toBe(true); |
| 42 | + } |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("IBAN Formatting", () => { |
| 47 | + it("should format IBANs with default spacing", () => { |
| 48 | + const testCases = [ |
| 49 | + { |
| 50 | + input: "DE89370400440532013000", |
| 51 | + expected: "DE89 3704 0044 0532 0130 00", |
| 52 | + }, |
| 53 | + { |
| 54 | + input: "GB29NWBK60161331926819", |
| 55 | + expected: "GB29 NWBK 6016 1331 9268 19", |
| 56 | + }, |
| 57 | + ]; |
| 58 | + |
| 59 | + for (const { input, expected } of testCases) { |
| 60 | + expect(iban.printFormat(input)).toBe(expected); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it("should format IBANs with custom separator", () => { |
| 65 | + const testCases = [ |
| 66 | + { |
| 67 | + input: "DE89370400440532013000", |
| 68 | + separator: "-", |
| 69 | + expected: "DE89-3704-0044-0532-0130-00", |
| 70 | + }, |
| 71 | + { |
| 72 | + input: "GB29NWBK60161331926819", |
| 73 | + separator: "*", |
| 74 | + expected: "GB29*NWBK*6016*1331*9268*19", |
| 75 | + }, |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const { input, separator, expected } of testCases) { |
| 79 | + expect(iban.printFormat(input, separator)).toBe(expected); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + it("should clean and format IBANs with special characters", () => { |
| 84 | + const testCases = [ |
| 85 | + { |
| 86 | + input: "DE89-3704.0044/0532-0130-00", |
| 87 | + expected: "DE89 3704 0044 0532 0130 00", |
| 88 | + }, |
| 89 | + { |
| 90 | + input: "GB29/NWBK.6016-1331.9268.19", |
| 91 | + expected: "GB29 NWBK 6016 1331 9268 19", |
| 92 | + }, |
| 93 | + ]; |
| 94 | + |
| 95 | + for (const { input, expected } of testCases) { |
| 96 | + expect(iban.printFormat(input)).toBe(expected); |
| 97 | + } |
| 98 | + }); |
| 99 | +}); |
0 commit comments