|
| 1 | +import { |
| 2 | + generateKey, |
| 3 | + ensureArrayKeys, |
| 4 | + createReference, |
| 5 | + createReferenceWithKey, |
| 6 | +} from '@/lib/sanity/helpers' |
| 7 | + |
| 8 | +describe('generateKey', () => { |
| 9 | + it('should generate a unique key with default prefix', () => { |
| 10 | + const key1 = generateKey() |
| 11 | + const key2 = generateKey() |
| 12 | + |
| 13 | + expect(key1).toMatch(/^item-/) |
| 14 | + expect(key2).toMatch(/^item-/) |
| 15 | + expect(key1).not.toBe(key2) |
| 16 | + }) |
| 17 | + |
| 18 | + it('should generate a unique key with custom prefix', () => { |
| 19 | + const key1 = generateKey('section') |
| 20 | + const key2 = generateKey('section') |
| 21 | + |
| 22 | + expect(key1).toMatch(/^section-/) |
| 23 | + expect(key2).toMatch(/^section-/) |
| 24 | + expect(key1).not.toBe(key2) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should generate unique keys on rapid successive calls', () => { |
| 28 | + const keys = Array.from({ length: 100 }, () => generateKey()) |
| 29 | + const uniqueKeys = new Set(keys) |
| 30 | + |
| 31 | + expect(uniqueKeys.size).toBe(keys.length) |
| 32 | + }) |
| 33 | +}) |
| 34 | + |
| 35 | +describe('ensureArrayKeys', () => { |
| 36 | + it('should add _key to objects without one', () => { |
| 37 | + const input = [{ name: 'Item 1' }, { name: 'Item 2' }] |
| 38 | + const result = ensureArrayKeys(input) |
| 39 | + |
| 40 | + expect(result[0]).toHaveProperty('_key') |
| 41 | + expect(result[1]).toHaveProperty('_key') |
| 42 | + expect(result[0]._key).toMatch(/^item-/) |
| 43 | + expect(result[1]._key).toMatch(/^item-/) |
| 44 | + expect(result[0]._key).not.toBe(result[1]._key) |
| 45 | + }) |
| 46 | + |
| 47 | + it('should preserve existing _key values', () => { |
| 48 | + const input = [{ name: 'Item 1', _key: 'existing-key' }, { name: 'Item 2' }] |
| 49 | + const result = ensureArrayKeys(input) |
| 50 | + |
| 51 | + expect(result[0]._key).toBe('existing-key') |
| 52 | + expect(result[1]._key).toMatch(/^item-/) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should use custom prefix', () => { |
| 56 | + const input = [{ name: 'Section 1' }] |
| 57 | + const result = ensureArrayKeys(input, 'section') |
| 58 | + |
| 59 | + expect(result[0]._key).toMatch(/^section-/) |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +describe('createReference', () => { |
| 64 | + it('should create a reference object', () => { |
| 65 | + const ref = createReference('doc-id-123') |
| 66 | + |
| 67 | + expect(ref).toEqual({ |
| 68 | + _type: 'reference', |
| 69 | + _ref: 'doc-id-123', |
| 70 | + }) |
| 71 | + }) |
| 72 | +}) |
| 73 | + |
| 74 | +describe('createReferenceWithKey', () => { |
| 75 | + it('should create a reference with _key', () => { |
| 76 | + const ref = createReferenceWithKey('doc-id-123') |
| 77 | + |
| 78 | + expect(ref._type).toBe('reference') |
| 79 | + expect(ref._ref).toBe('doc-id-123') |
| 80 | + expect(ref._key).toMatch(/^ref-/) |
| 81 | + }) |
| 82 | + |
| 83 | + it('should use custom prefix for _key', () => { |
| 84 | + const ref = createReferenceWithKey('doc-id-123', 'speaker') |
| 85 | + |
| 86 | + expect(ref._type).toBe('reference') |
| 87 | + expect(ref._ref).toBe('doc-id-123') |
| 88 | + expect(ref._key).toMatch(/^speaker-/) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should generate unique keys for different references', () => { |
| 92 | + const ref1 = createReferenceWithKey('doc-1') |
| 93 | + const ref2 = createReferenceWithKey('doc-2') |
| 94 | + |
| 95 | + expect(ref1._key).not.toBe(ref2._key) |
| 96 | + }) |
| 97 | +}) |
0 commit comments