|
| 1 | +import { describe, it, expect } from 'vitest' |
| 2 | +import { generateId, isValidNanoId, NANOID_LENGTH, nanoid } from '../nanoid' |
| 3 | + |
| 4 | +describe('nanoid utilities', () => { |
| 5 | + describe('generateId', () => { |
| 6 | + it('should generate a string of correct length', () => { |
| 7 | + const id = generateId() |
| 8 | + expect(id).toHaveLength(NANOID_LENGTH) |
| 9 | + }) |
| 10 | + |
| 11 | + it('should generate unique IDs', () => { |
| 12 | + const ids = new Set<string>() |
| 13 | + const count = 1000 |
| 14 | + |
| 15 | + for (let i = 0; i < count; i++) { |
| 16 | + ids.add(generateId()) |
| 17 | + } |
| 18 | + |
| 19 | + // All IDs should be unique |
| 20 | + expect(ids.size).toBe(count) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should only contain alphanumeric characters', () => { |
| 24 | + const id = generateId() |
| 25 | + expect(id).toMatch(/^[A-Za-z0-9]+$/) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should not contain special characters like _ or -', () => { |
| 29 | + // Generate multiple IDs to increase confidence |
| 30 | + for (let i = 0; i < 100; i++) { |
| 31 | + const id = generateId() |
| 32 | + expect(id).not.toContain('_') |
| 33 | + expect(id).not.toContain('-') |
| 34 | + } |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + describe('nanoid', () => { |
| 39 | + it('should be a function that generates IDs', () => { |
| 40 | + const id = nanoid() |
| 41 | + expect(typeof id).toBe('string') |
| 42 | + expect(id).toHaveLength(NANOID_LENGTH) |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + describe('isValidNanoId', () => { |
| 47 | + it('should return true for valid NanoIDs', () => { |
| 48 | + const id = generateId() |
| 49 | + expect(isValidNanoId(id)).toBe(true) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should return false for empty string', () => { |
| 53 | + expect(isValidNanoId('')).toBe(false) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should return false for null or undefined', () => { |
| 57 | + expect(isValidNanoId(null as unknown as string)).toBe(false) |
| 58 | + expect(isValidNanoId(undefined as unknown as string)).toBe(false) |
| 59 | + }) |
| 60 | + |
| 61 | + it('should return false for wrong length', () => { |
| 62 | + expect(isValidNanoId('abc')).toBe(false) |
| 63 | + expect(isValidNanoId('a'.repeat(NANOID_LENGTH + 1))).toBe(false) |
| 64 | + expect(isValidNanoId('a'.repeat(NANOID_LENGTH - 1))).toBe(false) |
| 65 | + }) |
| 66 | + |
| 67 | + it('should return false for IDs with invalid characters', () => { |
| 68 | + expect(isValidNanoId('abc!@#$%^&*()')).toBe(false) |
| 69 | + expect(isValidNanoId('abc_defghijk')).toBe(false) // underscore not allowed |
| 70 | + expect(isValidNanoId('abc-defghijk')).toBe(false) // dash not allowed |
| 71 | + }) |
| 72 | + |
| 73 | + it('should return true for all valid characters', () => { |
| 74 | + // Test a valid 12-char string with only alphanumeric |
| 75 | + expect(isValidNanoId('aB1cD2eF3gH4')).toBe(true) |
| 76 | + expect(isValidNanoId('ABCDEFGHIJKL')).toBe(true) |
| 77 | + expect(isValidNanoId('123456789012')).toBe(true) |
| 78 | + expect(isValidNanoId('abcdefghijkl')).toBe(true) |
| 79 | + }) |
| 80 | + |
| 81 | + it('should return false for UUID format', () => { |
| 82 | + // UUIDs are 36 chars with dashes, should be invalid |
| 83 | + const uuid = '550e8400-e29b-41d4-a716-446655440000' |
| 84 | + expect(isValidNanoId(uuid)).toBe(false) |
| 85 | + }) |
| 86 | + }) |
| 87 | + |
| 88 | + describe('NANOID_LENGTH', () => { |
| 89 | + it('should be 12', () => { |
| 90 | + expect(NANOID_LENGTH).toBe(12) |
| 91 | + }) |
| 92 | + }) |
| 93 | +}) |
0 commit comments