polish-validators
Version:
A set of validator functions that check common polish numbers.
70 lines (69 loc) • 4.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const iban_1 = require("./iban");
describe('IBAN Validation and Utility Functions', () => {
describe('isIbanValid', () => {
it('should return true for a valid IBAN with correct checksum and formatting', () => {
// AT611904300234573201 is a known valid Austrian IBAN.
expect((0, iban_1.isIbanValid)('PL47 1140 2004 0000 3312 1564 8766')).toBe(true);
expect((0, iban_1.isIbanValid)('03 1560 0013 2026 2754 1000 0001')).toBe(true);
expect((0, iban_1.isIbanValid)('HU23 1140 2004 0000 3102 7648 0004')).toBe(true);
});
it('should return true for a valid IBAN even with extra whitespace', () => {
// removeWhitespace should remove spaces so that the IBAN validates.
expect((0, iban_1.isIbanValid)('AT61 1904300 234573201')).toBe(true);
});
it('should return false for an IBAN with incorrect total length', () => {
// For Austria, the IBAN length must be 20 characters.
expect((0, iban_1.isIbanValid)('AT61190430023457320')).toBe(false);
});
it('should return false for a Polish IBAN with an invalid bank code', () => {
// an IBAN with invalid bank name, but otherwise valid
expect((0, iban_1.isIbanValid)('PL61 9999 1014 0000 0712 1981 8140')).toBe(false);
});
it('should return false for an IBAN with invalid characters (non-whitespace)', () => {
// removeWhitespace only removes whitespace; unexpected characters (e.g. dashes) will cause failure.
expect((0, iban_1.isIbanValid)('AT61-1904300234573201')).toBe(false);
});
});
describe('getCountryIbanDataFromIban', () => {
it('should return correct country data when given a valid two-letter country code', () => {
// The function expects a string that, after cleaning, consists only of two letters.
expect((0, iban_1.getCountryIbanDataFromIban)('pl')).toEqual({ country: 'Polska', length: 28 });
expect((0, iban_1.getCountryIbanDataFromIban)('AT')).toEqual({ country: 'Austria', length: 20 });
});
it('should return correct country data when given an IBAN string (more than two characters)', () => {
// Since the regex only matches exactly two letters, a full IBAN will not match.
expect((0, iban_1.getCountryIbanDataFromIban)('PL47 1140 2004 0000 3312 1564 8766')).toEqual({
country: 'Polska',
length: 28,
});
});
it('should return null when given only a single character', () => {
// Since the regex only matches exactly two letters, a full IBAN will not match.
expect((0, iban_1.getCountryIbanDataFromIban)('P')).toBeNull();
});
it('should return null for non-existent country', () => {
// Since the regex only matches exactly two letters, a full IBAN will not match.
expect((0, iban_1.getCountryIbanDataFromIban)('XX')).toBeNull();
});
});
describe('getBankNameFromIban', () => {
it('should return the bank name for a valid Polish IBAN', () => {
// PL IBAN with bank code "114" should correspond to mBank according to BANK_NAMES.
// Example: "PL27114020040000300201355387" is a valid Polish IBAN.
expect((0, iban_1.getBankNameFromIban)('PL47 1140 2004 0000 3312 1564 8766')).toBe('mBank');
});
it('should return null for a Polish IBAN with invalid bank number', () => {
// For non-Polish IBANs, the function always returns null.
expect((0, iban_1.getBankNameFromIban)('PL61 9999 1014 0000 0712 1981 8140')).toBeNull();
});
it('should return null for a non-Polish IBAN', () => {
// For non-Polish IBANs, the function always returns null.
expect((0, iban_1.getBankNameFromIban)('AT611904300234573201')).toBeNull();
});
it('should return null for an IBAN that does not match the expected bank data format', () => {
expect((0, iban_1.getBankNameFromIban)('INVALIDIBAN')).toBeNull();
});
});
});