UNPKG

3.72 kBPlain TextView Raw
1import Hashids from '../lib/hashids'
2
3const hashids = new Hashids()
4
5describe('bad input', () => {
6 test(`should throw an error when small alphabet`, () => {
7 expect(() => {
8 void new Hashids('', 0, '1234567890')
9 }).toThrow()
10 })
11
12 test(`should throw an error when alphabet not a string`, () => {
13 expect(() => {
14 // @ts-ignore
15 void new Hashids('', 0, 7)
16 }).toThrow(TypeError)
17 })
18
19 test(`should not throw an error when alphabet has spaces`, () => {
20 expect(() => {
21 void new Hashids('', 0, 'a cdefghijklmnopqrstuvwxyz')
22 }).not.toThrow()
23 })
24
25 test(`should return an empty string when encoding nothing`, () => {
26 const id = hashids.encode()
27 expect(id).toEqual('')
28 })
29
30 test(`should return an empty string when encoding an empty array`, () => {
31 const id = hashids.encode([])
32 expect(id).toEqual('')
33 })
34
35 test(`should return an empty string when encoding a negative number`, () => {
36 const id = hashids.encode(-1)
37 expect(id).toEqual('')
38 })
39
40 test(`should return an empty string when encoding a string with non-numeric characters`, () => {
41 expect(hashids.encode('6B')).toEqual('')
42 expect(hashids.encode('123a')).toEqual('')
43 })
44
45 test(`should return an empty string when encoding infinity`, () => {
46 const id = hashids.encode(Infinity)
47 expect(id).toEqual('')
48 })
49
50 test(`should return an empty string when encoding a null`, () => {
51 // @ts-ignore
52 const id = hashids.encode(null)
53 expect(id).toEqual('')
54 })
55
56 test(`should return an empty string when encoding a NaN`, () => {
57 const id = hashids.encode(NaN)
58 expect(id).toEqual('')
59 })
60
61 test(`should return an empty string when encoding an undefined`, () => {
62 // @ts-ignore
63 const id = hashids.encode(undefined)
64 expect(id).toEqual('')
65 })
66
67 test(`should return an empty string when encoding an array with non-numeric input`, () => {
68 const id = hashids.encode(['z'])
69 expect(id).toEqual('')
70 })
71
72 test(`should return an empty array when decoding nothing`, () => {
73 // @ts-ignore
74 const numbers = hashids.decode()
75 expect(numbers).toEqual([])
76 })
77
78 test(`should return an empty array when decoding an empty string`, () => {
79 const numbers = hashids.decode('')
80 expect(numbers).toEqual([])
81 })
82
83 test(`should return an empty string when encoding non-numeric input`, () => {
84 const id = hashids.encode('z')
85 expect(id).toEqual('')
86 })
87
88 test(`should return an empty array when decoding invalid id`, () => {
89 const numbers = hashids.decode('f')
90 expect(numbers).toEqual([])
91 })
92
93 test(`should return an empty string when encoding non-hex input`, () => {
94 const id = hashids.encodeHex('z')
95 expect(id).toEqual('')
96 })
97
98 test(`should return an empty string when hex-decoding invalid id`, () => {
99 const hex = hashids.decodeHex('f')
100 expect(hex).toEqual('')
101 })
102
103 // reproduction from https://github.com/niieani/hashids.js/issues/126
104 test(`should throw an error when an id to be decoded contains chars that do not exist in the alphabet (multiple)`, () => {
105 const hashids = new Hashids('', 6, 'abcdefghjklmnpqrstuvwxyz23456789')
106 expect(hashids.isValidId('[object Object]')).toBe(false)
107 expect(() => {
108 hashids.decode('[object Object]')
109 }).toThrow(Error)
110 })
111
112 // reproduction from https://github.com/niieani/hashids.js/issues/126
113 test(`should throw an error when an id to be decoded contains chars that do not exist in the alphabet (single)`, () => {
114 const hashids = new Hashids('', 6, 'abcdefghjklmnpqrstuvwxyz23456789')
115 expect(hashids.isValidId('a1bcdef')).toBe(false)
116 expect(() => {
117 hashids.decode('a1bcdef')
118 }).toThrow(Error)
119 })
120})