metadata.test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. "use strict";
  2. var _metadataMin = _interopRequireDefault(require("../metadata.min.json"));
  3. var _metadataMin2 = _interopRequireDefault(require("../test/metadata/1.0.0/metadata.min.json"));
  4. var _metadataMin3 = _interopRequireDefault(require("../test/metadata/1.1.11/metadata.min.json"));
  5. var _metadataMin4 = _interopRequireDefault(require("../test/metadata/1.7.34/metadata.min.json"));
  6. var _metadataMin5 = _interopRequireDefault(require("../test/metadata/1.7.37/metadata.min.json"));
  7. var _metadata = _interopRequireWildcard(require("./metadata.js"));
  8. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  9. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  12. describe('metadata', function () {
  13. it('should return undefined for non-defined types', function () {
  14. var FR = new _metadata["default"](_metadataMin["default"]).country('FR');
  15. type(FR.type('FIXED_LINE')).should.equal('undefined');
  16. });
  17. it('should validate country', function () {
  18. var thrower = function thrower() {
  19. return new _metadata["default"](_metadataMin["default"]).country('RUS');
  20. };
  21. thrower.should["throw"]('Unknown country');
  22. });
  23. it('should tell if a country is supported', function () {
  24. (0, _metadata.isSupportedCountry)('RU', _metadataMin["default"]).should.equal(true);
  25. (0, _metadata.isSupportedCountry)('XX', _metadataMin["default"]).should.equal(false);
  26. });
  27. it('should return ext prefix for a country', function () {
  28. (0, _metadata.getExtPrefix)('US', _metadataMin["default"]).should.equal(' ext. ');
  29. (0, _metadata.getExtPrefix)('CA', _metadataMin["default"]).should.equal(' ext. ');
  30. (0, _metadata.getExtPrefix)('GB', _metadataMin["default"]).should.equal(' x'); // expect(getExtPrefix('XX', metadata)).to.equal(undefined)
  31. (0, _metadata.getExtPrefix)('XX', _metadataMin["default"]).should.equal(' ext. ');
  32. });
  33. it('should cover non-occuring edge cases', function () {
  34. new _metadata["default"](_metadataMin["default"]).getNumberingPlanMetadata('999');
  35. });
  36. it('should support deprecated methods', function () {
  37. new _metadata["default"](_metadataMin["default"]).country('US').nationalPrefixForParsing().should.equal('1');
  38. new _metadata["default"](_metadataMin["default"]).chooseCountryByCountryCallingCode('1').nationalPrefixForParsing().should.equal('1');
  39. });
  40. it('should tell if a national prefix is mandatory when formatting a national number', function () {
  41. var meta = new _metadata["default"](_metadataMin["default"]); // No "national_prefix_formatting_rule".
  42. // "national_prefix_is_optional_when_formatting": true
  43. meta.country('US');
  44. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(false); // "national_prefix_formatting_rule": "8 ($1)"
  45. // "national_prefix_is_optional_when_formatting": true
  46. meta.country('RU');
  47. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(false); // "national_prefix": "0"
  48. // "national_prefix_formatting_rule": "0 $1"
  49. meta.country('FR');
  50. meta.numberingPlan.formats()[0].nationalPrefixIsMandatoryWhenFormattingInNationalFormat().should.equal(true);
  51. });
  52. it('should validate metadata', function () {
  53. var thrower = function thrower() {
  54. return (0, _metadata.validateMetadata)();
  55. };
  56. thrower.should["throw"]('`metadata` argument not passed');
  57. thrower = function thrower() {
  58. return (0, _metadata.validateMetadata)(123);
  59. };
  60. thrower.should["throw"]('Got a number: 123.');
  61. thrower = function thrower() {
  62. return (0, _metadata.validateMetadata)('abc');
  63. };
  64. thrower.should["throw"]('Got a string: abc.');
  65. thrower = function thrower() {
  66. return (0, _metadata.validateMetadata)({
  67. a: true,
  68. b: 2
  69. });
  70. };
  71. thrower.should["throw"]('Got an object of shape: { a, b }.');
  72. thrower = function thrower() {
  73. return (0, _metadata.validateMetadata)({
  74. a: true,
  75. countries: 2
  76. });
  77. };
  78. thrower.should["throw"]('Got an object of shape: { a, countries }.');
  79. thrower = function thrower() {
  80. return (0, _metadata.validateMetadata)({
  81. country_calling_codes: true,
  82. countries: 2
  83. });
  84. };
  85. thrower.should["throw"]('Got an object of shape');
  86. thrower = function thrower() {
  87. return (0, _metadata.validateMetadata)({
  88. country_calling_codes: {},
  89. countries: 2
  90. });
  91. };
  92. thrower.should["throw"]('Got an object of shape');
  93. (0, _metadata.validateMetadata)({
  94. country_calling_codes: {},
  95. countries: {},
  96. b: 3
  97. });
  98. });
  99. it('should work around `nonGeographical` typo in metadata generated from `1.7.35` to `1.7.37`', function () {
  100. var meta = new _metadata["default"](_metadataMin5["default"]);
  101. meta.selectNumberingPlan('888');
  102. type(meta.nonGeographic()).should.equal('object');
  103. });
  104. it('should work around `nonGeographic` metadata not existing before `1.7.35`', function () {
  105. var meta = new _metadata["default"](_metadataMin4["default"]);
  106. type(meta.getNumberingPlanMetadata('800')).should.equal('object');
  107. type(meta.getNumberingPlanMetadata('000')).should.equal('undefined');
  108. });
  109. it('should work with metadata from version `1.1.11`', function () {
  110. var meta = new _metadata["default"](_metadataMin3["default"]);
  111. meta.selectNumberingPlan('US');
  112. meta.numberingPlan.possibleLengths().should.deep.equal([10]);
  113. meta.numberingPlan.formats().length.should.equal(1);
  114. meta.numberingPlan.nationalPrefix().should.equal('1');
  115. meta.numberingPlan.nationalPrefixForParsing().should.equal('1');
  116. meta.numberingPlan.type('MOBILE').pattern().should.equal('');
  117. meta.selectNumberingPlan('AG');
  118. meta.numberingPlan.leadingDigits().should.equal('268'); // Should've been "268$1" but apparently there was a bug in metadata generator
  119. // and no national prefix transform rules were written.
  120. expect(meta.numberingPlan.nationalPrefixTransformRule()).to.be["null"];
  121. meta.selectNumberingPlan('AF');
  122. meta.numberingPlan.formats()[0].nationalPrefixFormattingRule().should.equal('0$1');
  123. meta.selectNumberingPlan('RU');
  124. meta.numberingPlan.formats()[0].nationalPrefixIsOptionalWhenFormattingInNationalFormat().should.equal(true);
  125. });
  126. it('should work with metadata from version `1.0.0`', function () {
  127. var meta = new _metadata["default"](_metadataMin2["default"]);
  128. meta.selectNumberingPlan('US');
  129. meta.numberingPlan.formats().length.should.equal(1);
  130. meta.numberingPlan.nationalPrefix().should.equal('1');
  131. meta.numberingPlan.nationalPrefixForParsing().should.equal('1');
  132. type(meta.numberingPlan.type('MOBILE')).should.equal('undefined');
  133. meta.selectNumberingPlan('AG');
  134. meta.numberingPlan.leadingDigits().should.equal('268'); // Should've been "268$1" but apparently there was a bug in metadata generator
  135. // and no national prefix transform rules were written.
  136. expect(meta.numberingPlan.nationalPrefixTransformRule()).to.be["null"];
  137. meta.selectNumberingPlan('AF');
  138. meta.numberingPlan.formats()[0].nationalPrefixFormattingRule().should.equal('0$1');
  139. meta.selectNumberingPlan('RU');
  140. meta.numberingPlan.formats()[0].nationalPrefixIsOptionalWhenFormattingInNationalFormat().should.equal(true);
  141. });
  142. it('should work around "ext" data not present in metadata from version `1.0.0`', function () {
  143. var meta = new _metadata["default"](_metadataMin2["default"]);
  144. meta.selectNumberingPlan('GB');
  145. meta.ext().should.equal(' ext. ');
  146. var metaNew = new _metadata["default"](_metadataMin["default"]);
  147. metaNew.selectNumberingPlan('GB');
  148. metaNew.ext().should.equal(' x');
  149. });
  150. it('should work around "default IDD prefix" data not present in metadata from version `1.0.0`', function () {
  151. var meta = new _metadata["default"](_metadataMin2["default"]);
  152. meta.selectNumberingPlan('AU');
  153. type(meta.defaultIDDPrefix()).should.equal('undefined');
  154. var metaNew = new _metadata["default"](_metadataMin["default"]);
  155. metaNew.selectNumberingPlan('AU');
  156. metaNew.defaultIDDPrefix().should.equal('0011');
  157. });
  158. });
  159. function type(something) {
  160. return _typeof(something);
  161. }
  162. //# sourceMappingURL=metadata.test.js.map