index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var expect = require('chai').expect;
  2. var urlSlug = require('../index');
  3. describe('module', function () {
  4. it('should include constructor as a property', function () {
  5. expect(urlSlug.UrlSlug.constructor).to.be.equal(urlSlug.constructor);
  6. });
  7. it('should contain convert and revert methods', function () {
  8. expect(urlSlug.convert).to.be.a('function');
  9. expect(urlSlug.revert).to.be.a('function');
  10. });
  11. it('should call convert if called as a function', function () {
  12. expect(urlSlug).to.be.equal(urlSlug.convert);
  13. });
  14. describe('instance', function () {
  15. var instance = new urlSlug.UrlSlug;
  16. it('should contain lowercase, uppercase and titlecase builtin transformers', function () {
  17. expect(instance.transformers).to.contain.all.keys('lowercase', 'uppercase', 'titlecase');
  18. });
  19. it('should set "-" as default separator', function () {
  20. expect(instance.separator).to.be.equal('-');
  21. });
  22. it('should set "lowercase" as default transformer', function () {
  23. expect(instance.transform).to.be.equal(instance.transformers.lowercase);
  24. });
  25. describe('transformers', function () {
  26. it('should contain a working lowercase', function () {
  27. expect(instance.transformers.lowercase('TEST STRING')).to.be.equal('test string');
  28. });
  29. it('should contain a working uppercase', function () {
  30. expect(instance.transformers.uppercase('test string')).to.be.equal('TEST STRING');
  31. });
  32. it('should contain a working titlecase', function () {
  33. expect(instance.transformers.titlecase('tesT strinG')).to.be.equal('Test String');
  34. });
  35. });
  36. describe('prepare', function () {
  37. it('should not accept nothing but a string as a separator', function () {
  38. expect(instance.prepare.bind(instance, 123)).to.throw(/^Invalid separator, must be a string/);
  39. });
  40. it('should accept all characters defined as unreserved in RFC 3986 as a separator', function () {
  41. expect(instance.prepare.bind(instance, '-._~')).to.not.throw(/^Invalid separator, has invalid characters/);
  42. });
  43. it('should not accept a separator character not defined as unreserved in RFC 3986', function () {
  44. expect(instance.prepare.bind(instance, '+')).to.throw(/^Invalid separator, has invalid characters/);
  45. });
  46. it('should accept false as transform', function () {
  47. expect(instance.prepare.bind(instance, '', false)).not.to.throw(/^Invalid transform, must be a function/);
  48. });
  49. it('should accept all builtin presets as transform', function () {
  50. expect(instance.prepare.bind(instance, '', 'lowercase')).to.not.throw(/^Invalid transform, must be a function/);
  51. expect(instance.prepare.bind(instance, '', 'uppercase')).to.not.throw(/^Invalid transform, must be a function/);
  52. expect(instance.prepare.bind(instance, '', 'titlecase')).to.not.throw(/^Invalid transform, must be a function/);
  53. });
  54. it('should accept a function as transform', function () {
  55. expect(instance.prepare.bind(instance, '', function () {})).to.not.throw(/^Invalid transform, must be a function/);
  56. });
  57. it('should only accept false, a function or a builtin preset as transform', function () {
  58. expect(instance.prepare.bind(instance, '', true)).to.throw(/^Invalid transform, must be a builtin transform or a function/);
  59. expect(instance.prepare.bind(instance, '', 'nonexistent')).to.throw(/^Invalid transform, must be a builtin transform or a function/);
  60. expect(instance.prepare.bind(instance, '', {})).to.throw(/^Invalid transform, must be a builtin transform or a function/);
  61. });
  62. it('should return an object with all available options', function () {
  63. expect(instance.prepare()).to.contain.all.keys('separator', 'transform');
  64. });
  65. });
  66. describe('convert', function () {
  67. it('should not accept nothing but a string as input', function () {
  68. expect(instance.convert.bind(instance, 123)).to.throw(/^Invalid value, must be a string/);
  69. });
  70. it('should return a default slug if no options are set', function () {
  71. expect(instance.convert('Url Slug')).to.be.equal('url-slug');
  72. });
  73. it('should remove accents', function () {
  74. expect(instance.convert('á é í ó ú')).to.be.equal('a-e-i-o-u');
  75. });
  76. it('should convert to upper case and use default separator', function () {
  77. expect(instance.convert('a bronx tale', null, 'uppercase')).to.be.equal('A-BRONX-TALE');
  78. });
  79. it('should use underscore separators and title case', function () {
  80. expect(instance.convert('tom jobim', '_', 'titlecase')).to.be.equal('Tom_Jobim');
  81. });
  82. it('should allow multiple characters in separator and not change the case', function () {
  83. expect(instance.convert('Charly García', '-._~-._~', false)).to.be.equal('Charly-._~-._~Garcia');
  84. });
  85. it('should return a camel case string', function () {
  86. expect(instance.convert('java script', '', 'titlecase')).to.be.equal('JavaScript');
  87. });
  88. it('should break a camel case string', function () {
  89. expect(instance.convert('javaScript')).to.be.equal('java-script');
  90. });
  91. it('should return only consonants', function () {
  92. var transform = function (string) {
  93. return string.replace(/[aeiou]/gi, '');
  94. }
  95. expect(instance.convert('React', '', transform)).to.be.equal('Rct');
  96. });
  97. });
  98. describe('revert', function () {
  99. it('should not accept nothing but a string as input', function () {
  100. expect(instance.revert.bind(instance, 123)).to.throw(/^Invalid value, must be a string/);
  101. });
  102. it('should use automatic reversion and maintain input case', function () {
  103. expect(instance.revert('UrlSlug-url_slug')).to.be.equal('Url Slug url slug');
  104. });
  105. it('should break only on camel case and convert input to upper case', function () {
  106. expect(instance.revert('ClaudioBaglioni_is-Italian', '', 'uppercase')).to.be.equal('CLAUDIO BAGLIONI_IS-ITALIAN');
  107. });
  108. it('should return the title of a Pink Floyd track', function () {
  109. expect(instance.revert('comfortably-._~numb', '-._~', 'titlecase')).to.be.equal('Comfortably Numb');
  110. });
  111. });
  112. });
  113. });