PhoneNumber.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import metadata from '../metadata.min.json' assert { type: 'json' };
  2. import PhoneNumber from './PhoneNumber.js';
  3. describe('PhoneNumber', function () {
  4. it('should validate constructor arguments', function () {
  5. expect(function () {
  6. return new PhoneNumber();
  7. }).to["throw"]('`countryCallingCode` not passed');
  8. expect(function () {
  9. return new PhoneNumber('7');
  10. }).to["throw"]('`nationalNumber` not passed');
  11. expect(function () {
  12. return new PhoneNumber('7', '8005553535');
  13. }).to["throw"]('`metadata` not passed');
  14. });
  15. it('should accept country code argument', function () {
  16. var phoneNumber = new PhoneNumber('RU', '8005553535', metadata);
  17. phoneNumber.countryCallingCode.should.equal('7');
  18. phoneNumber.country.should.equal('RU');
  19. phoneNumber.number.should.equal('+78005553535');
  20. });
  21. it('should format number with options', function () {
  22. var phoneNumber = new PhoneNumber('7', '8005553535', metadata);
  23. phoneNumber.ext = '123';
  24. phoneNumber.format('NATIONAL', {
  25. formatExtension: function formatExtension(number, extension) {
  26. return "".concat(number, " \u0434\u043E\u0431. ").concat(extension);
  27. }
  28. }).should.equal('8 (800) 555-35-35 доб. 123');
  29. });
  30. it('should compare phone numbers', function () {
  31. new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553535', metadata)).should.equal(true);
  32. new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('7', '8005553535', metadata)).should.equal(true);
  33. new PhoneNumber('RU', '8005553535', metadata).isEqual(new PhoneNumber('RU', '8005553536', metadata)).should.equal(false);
  34. });
  35. it('should tell if a number is non-geographic', function () {
  36. new PhoneNumber('7', '8005553535', metadata).isNonGeographic().should.equal(false);
  37. new PhoneNumber('870', '773111632', metadata).isNonGeographic().should.equal(true);
  38. });
  39. it('should allow setting extension', function () {
  40. var phoneNumber = new PhoneNumber('1', '2133734253', metadata);
  41. phoneNumber.setExt('1234');
  42. phoneNumber.ext.should.equal('1234');
  43. phoneNumber.formatNational().should.equal('(213) 373-4253 ext. 1234');
  44. });
  45. it('should return possible countries', function () {
  46. // "599": [
  47. // "CW", // "possible_lengths": [7, 8]
  48. // "BQ" // "possible_lengths": [7]
  49. // ]
  50. var phoneNumber = new PhoneNumber('599', '123456', metadata);
  51. expect(phoneNumber.country).to.be.undefined;
  52. phoneNumber.getPossibleCountries().should.deep.equal([]);
  53. phoneNumber = new PhoneNumber('599', '1234567', metadata);
  54. expect(phoneNumber.country).to.be.undefined;
  55. phoneNumber.getPossibleCountries().should.deep.equal(['CW', 'BQ']);
  56. phoneNumber = new PhoneNumber('599', '12345678', metadata);
  57. expect(phoneNumber.country).to.be.undefined;
  58. phoneNumber.getPossibleCountries().should.deep.equal(['CW']);
  59. phoneNumber = new PhoneNumber('599', '123456789', metadata);
  60. expect(phoneNumber.country).to.be.undefined;
  61. phoneNumber.getPossibleCountries().should.deep.equal([]);
  62. });
  63. it('should return possible countries in case of ambiguity', function () {
  64. var phoneNumber = new PhoneNumber('1', '2223334444', metadata);
  65. expect(phoneNumber.country).to.be.undefined;
  66. phoneNumber.getPossibleCountries().indexOf('US').should.equal(0);
  67. phoneNumber.getPossibleCountries().length.should.equal(25);
  68. }); // it('should return empty possible countries when no national number has been input', () => {
  69. // const phoneNumber = new PhoneNumber('1', '', metadata)
  70. // expect(phoneNumber.country).to.be.undefined
  71. // phoneNumber.getPossibleCountries().should.deep.equal([])
  72. // })
  73. it('should return empty possible countries when not enough national number digits have been input', function () {
  74. var phoneNumber = new PhoneNumber('1', '222', metadata);
  75. expect(phoneNumber.country).to.be.undefined;
  76. phoneNumber.getPossibleCountries().should.deep.equal([]);
  77. });
  78. it('should return possible countries in case of no ambiguity', function () {
  79. var phoneNumber = new PhoneNumber('US', '2133734253', metadata);
  80. phoneNumber.country.should.equal('US');
  81. phoneNumber.getPossibleCountries().should.deep.equal(['US']);
  82. });
  83. it('should return empty possible countries in case of an unknown calling code', function () {
  84. var phoneNumber = new PhoneNumber('777', '123', metadata);
  85. expect(phoneNumber.country).to.be.undefined;
  86. phoneNumber.getPossibleCountries().should.deep.equal([]);
  87. }); // it('should validate phone number length', () => {
  88. // const phoneNumber = new PhoneNumber('RU', '800', metadata)
  89. // expect(phoneNumber.validateLength()).to.equal('TOO_SHORT')
  90. //
  91. // const phoneNumberValid = new PhoneNumber('RU', '8005553535', metadata)
  92. // expect(phoneNumberValid.validateLength()).to.be.undefined
  93. // })
  94. });
  95. //# sourceMappingURL=PhoneNumber.test.js.map