bad-input.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import Hashids from '../lib/hashids';
  2. import { assert } from 'chai';
  3. const hashids = new Hashids();
  4. describe('bad input', () => {
  5. it(`should throw an error when small alphabet`, () => {
  6. assert.throws(() => {
  7. const hashidsIgnored = new Hashids('', 0, '1234567890');
  8. });
  9. });
  10. it(`should throw an error when alphabet has spaces`, () => {
  11. assert.throws(() => {
  12. const hashidsIgnored = new Hashids('', 0, 'a cdefghijklmnopqrstuvwxyz');
  13. });
  14. });
  15. it(`should return an empty string when encoding nothing`, () => {
  16. const id = hashids.encode();
  17. assert.equal(id, '');
  18. });
  19. it(`should return an empty string when encoding an empty array`, () => {
  20. const id = hashids.encode([]);
  21. assert.equal(id, '');
  22. });
  23. it(`should return an empty string when encoding a negative number`, () => {
  24. const id = hashids.encode(-1);
  25. assert.equal(id, '');
  26. });
  27. it(`should return an empty string when encoding a string with non-numeric characters`, () => {
  28. assert.equal(hashids.encode('6B'), '');
  29. assert.equal(hashids.encode('123a'), '');
  30. });
  31. it(`should return an empty string when encoding infinity`, () => {
  32. const id = hashids.encode(Infinity);
  33. assert.equal(id, '');
  34. });
  35. it(`should return an empty string when encoding a null`, () => {
  36. const id = hashids.encode(null);
  37. assert.equal(id, '');
  38. });
  39. it(`should return an empty string when encoding a NaN`, () => {
  40. const id = hashids.encode(NaN);
  41. assert.equal(id, '');
  42. });
  43. it(`should return an empty string when encoding an undefined`, () => {
  44. const id = hashids.encode(undefined);
  45. assert.equal(id, '');
  46. });
  47. it(`should return an empty string when encoding an array with non-numeric input`, () => {
  48. const id = hashids.encode(['z']);
  49. assert.equal(id, '');
  50. });
  51. it(`should return an empty array when decoding nothing`, () => {
  52. const numbers = hashids.decode();
  53. assert.deepEqual(numbers, []);
  54. });
  55. it(`should return an empty string when encoding non-numeric input`, () => {
  56. const id = hashids.encode('z');
  57. assert.equal(id, '');
  58. });
  59. it(`should return an empty array when decoding invalid id`, () => {
  60. const numbers = hashids.decode('f');
  61. assert.deepEqual(numbers, []);
  62. });
  63. it(`should return an empty string when encoding non-hex input`, () => {
  64. const id = hashids.encodeHex('z');
  65. assert.equal(id, '');
  66. });
  67. it(`should return an empty array when hex-decoding invalid id`, () => {
  68. const numbers = hashids.decodeHex('f');
  69. assert.deepEqual(numbers, []);
  70. });
  71. });