PhoneNumber.test.js 5.5 KB

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