1 | import Hashids from '../lib/hashids'
|
2 |
|
3 | const hashids = new Hashids()
|
4 |
|
5 | describe('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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 | })
|