|
| 1 | +import { optId } from "./index"; |
| 2 | + |
| 3 | +declare global { |
| 4 | + namespace jest { |
| 5 | + interface Matchers<R> { |
| 6 | + toBeDistinct(): R; |
| 7 | + toMatchPattern(pattern: RegExp): R; |
| 8 | + } |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +beforeAll(() => { |
| 13 | + expect.extend({ |
| 14 | + toBeDistinct(received) { |
| 15 | + const pass = |
| 16 | + Array.isArray(received) && |
| 17 | + new Set(received).size === received.length; |
| 18 | + if (pass) { |
| 19 | + return { |
| 20 | + message: () => `expected [${received}] array is unique`, |
| 21 | + pass: true, |
| 22 | + }; |
| 23 | + } else { |
| 24 | + return { |
| 25 | + message: () => |
| 26 | + `expected [${received}] array is not to unique`, |
| 27 | + pass: false, |
| 28 | + }; |
| 29 | + } |
| 30 | + }, |
| 31 | + toMatchPattern(received, pattern: RegExp) { |
| 32 | + if (typeof received !== "string") { |
| 33 | + return { |
| 34 | + message: () => `expected [${received}] is not a string`, |
| 35 | + pass: false, |
| 36 | + }; |
| 37 | + } |
| 38 | + const pass = pattern.test(received); |
| 39 | + if (pass) { |
| 40 | + return { |
| 41 | + message: () => |
| 42 | + `expected [${received}] string matches pattern`, |
| 43 | + pass: true, |
| 44 | + }; |
| 45 | + } else { |
| 46 | + return { |
| 47 | + message: () => |
| 48 | + `expected [${received}] string does not match pattern`, |
| 49 | + pass: false, |
| 50 | + }; |
| 51 | + } |
| 52 | + }, |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +it("exports correct interface", () => { |
| 57 | + expect(optId).toBeDefined(); |
| 58 | + expect(optId).toHaveProperty("generate"); |
| 59 | +}); |
| 60 | + |
| 61 | +it("generates an id", () => { |
| 62 | + expect(optId.generate()).toHaveLength(1); |
| 63 | +}); |
| 64 | + |
| 65 | +it("generates an id that is 32 characters in length", () => { |
| 66 | + const [id] = optId.generate(); |
| 67 | + expect(id).toHaveLength(32); |
| 68 | +}); |
| 69 | + |
| 70 | +it("generates an id that only contains valid hex characters", () => { |
| 71 | + const [id] = optId.generate(); |
| 72 | + expect(id).toMatchPattern(/^[a-f0-9]{32}$/); |
| 73 | +}); |
| 74 | + |
| 75 | +it("generates n ids", () => { |
| 76 | + const ids = optId.generate(10); |
| 77 | + expect(ids).toHaveLength(10); |
| 78 | +}); |
| 79 | + |
| 80 | +it("generates n ids that are unique", () => { |
| 81 | + const ids = optId.generate(10); |
| 82 | + expect(ids).toBeDistinct(); |
| 83 | +}); |
| 84 | + |
| 85 | +it("generates n ids that are in ascending order", () => { |
| 86 | + const ids = optId.generate(20); |
| 87 | + |
| 88 | + const sortedIds = [...ids].sort((a, b) => { |
| 89 | + a = a |
| 90 | + .match(/[a-f0-9]{0,2}/g)! |
| 91 | + .reverse() |
| 92 | + .join(""); |
| 93 | + b = b |
| 94 | + .match(/[a-f0-9]{0,2}/g)! |
| 95 | + .reverse() |
| 96 | + .join(""); |
| 97 | + return a < b ? -1 : a > b ? 1 : 0; |
| 98 | + }); |
| 99 | + |
| 100 | + expect(ids).toStrictEqual(sortedIds); |
| 101 | +}); |
0 commit comments