You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
5.3 KiB

  1. // run in browser
  2. if ('undefined' != typeof require) {
  3. var expect = require('chai').expect,
  4. IBAN = require('../iban.js');
  5. }
  6. describe('IBAN', function(){
  7. describe('.isValid', function(){
  8. it('should return false when input is not a String', function(){
  9. expect(IBAN.isValid(1)).to.be.false;
  10. expect(IBAN.isValid([])).to.be.false;
  11. expect(IBAN.isValid({})).to.be.false;
  12. expect(IBAN.isValid(true)).to.be.false;
  13. });
  14. it('should return false for an unknown country code digit', function(){
  15. expect(IBAN.isValid('ZZ68539007547034')).to.be.false;
  16. });
  17. it('should return true for a valid belgian IBAN', function(){
  18. expect(IBAN.isValid('BE68539007547034')).to.be.true;
  19. });
  20. it('should return true for a valid Dutch IBAN', function(){
  21. expect(IBAN.isValid('NL86INGB0002445588')).to.be.true;
  22. });
  23. it('should return true for a valid Moldovan IBAN', function(){
  24. expect(IBAN.isValid('MD75EX0900002374642125EU')).to.be.true;
  25. });
  26. it('should return true for a valid Saint-Lucia IBAN', function(){
  27. expect(IBAN.isValid('LC55HEMM000100010012001200023015')).to.be.true;
  28. });
  29. it('should return false for an incorrect check digit', function(){
  30. expect(IBAN.isValid('BE68539007547035')).to.be.false;
  31. });
  32. it('should return true for a valid Côte d\'Ivoire IBAN', function(){
  33. expect(IBAN.isValid('CI93CI0080111301134291200589')).to.be.true;
  34. });
  35. it('should return true for all examples', function(){
  36. Object.keys(IBAN.countries).forEach(function(countryCode){
  37. expect(IBAN.isValid(IBAN.countries[countryCode].example)).to.be.true;
  38. });
  39. });
  40. it('should return false for all examples when modifying just one digit', function(){
  41. Object.keys(IBAN.countries).forEach(function(countryCode){
  42. var num = IBAN.countries[countryCode].example;
  43. num = num.slice(0, -1) + ((parseInt(num.slice(-1), 10) + 1) % 10);
  44. expect(IBAN.isValid(num)).to.be.false;
  45. });
  46. });
  47. it('should return true for a valid Egypt IBAN', function(){
  48. expect(IBAN.isValid('EG800002000156789012345180002')).to.be.true;
  49. });
  50. });
  51. describe('.electronicFormat', function(){
  52. it('should format a e-formatted belgian IBAN', function(){
  53. expect(IBAN.electronicFormat('BE68539007547034')).to.equal('BE68539007547034');
  54. });
  55. it('should format a print-formatted belgian IBAN', function(){
  56. expect(IBAN.electronicFormat('BE68 5390 0754 7034')).to.equal('BE68539007547034');
  57. });
  58. });
  59. describe('.printFormat', function(){
  60. it('should format a e-formatted belgian IBAN', function(){
  61. expect(IBAN.printFormat('BE68539007547034')).to.equal('BE68 5390 0754 7034');
  62. });
  63. it('should format a print-formatted belgian IBAN', function(){
  64. expect(IBAN.printFormat('BE68 5390 0754 7034')).to.equal('BE68 5390 0754 7034');
  65. });
  66. });
  67. describe('.toBBAN', function(){
  68. it('should output the right BBAN from a Belgian IBAN', function(){
  69. expect(IBAN.toBBAN('BE68 5390 0754 7034', '-')).to.equal('539-0075470-34');
  70. });
  71. it('should use space as default separator', function(){
  72. expect(IBAN.toBBAN('BE68 5390 0754 7034')).to.equal('539 0075470 34');
  73. });
  74. });
  75. describe('.fromBBAN', function(){
  76. it('should output the right IBAN from a Belgian BBAN', function(){
  77. expect(IBAN.fromBBAN('BE', '539007547034')).to.equal('BE68539007547034');
  78. });
  79. it('should output the right IBAN from a Belgian BBAN, ignoring format', function(){
  80. expect(IBAN.fromBBAN('BE', '539-0075470-34')).to.equal('BE68539007547034');
  81. });
  82. it('should throw an error if the BBAN is invalid', function(){
  83. expect(function(){
  84. IBAN.fromBBAN('BE', '1539-0075470-34');
  85. }).to.throw(Error).and.throw(/Invalid BBAN/);
  86. });
  87. });
  88. describe('.isValidBBAN', function(){
  89. it('should return false when input is not a String', function(){
  90. expect(IBAN.isValidBBAN('BE', 1)).to.be.false;
  91. expect(IBAN.isValidBBAN('BE', {})).to.be.false;
  92. expect(IBAN.isValidBBAN('BE', [])).to.be.false;
  93. expect(IBAN.isValidBBAN('BE', true)).to.be.false;
  94. });
  95. it('should validate a correct Belgian BBAN', function(){
  96. expect(IBAN.isValidBBAN('BE', '539007547034')).to.be.true;
  97. });
  98. it('should return true for a valid Dutch IBAN', function(){
  99. expect(IBAN.isValidBBAN('NL', 'INGB0002445588')).to.be.true;
  100. });
  101. it('should validate a correct Belgian BBAN, ignoring format', function(){
  102. expect(IBAN.isValidBBAN('BE', '539-0075470-34')).to.be.true;
  103. });
  104. it('should detect invalid BBAN length', function(){
  105. expect(IBAN.isValidBBAN('BE', '1539-0075470-34')).to.be.false;
  106. });
  107. it('should detect invalid BBAN format', function(){
  108. expect(IBAN.isValidBBAN('BE', 'ABC-0075470-34')).to.be.false;
  109. });
  110. });
  111. });