Spaces:
Runtime error
Runtime error
| // Story 7.3: Tests for slug generation utility | |
| import { describe, it, expect } from 'vitest'; | |
| import { generateCreatorSlug, ensureUniqueSlug, isValidSlug } from '../slug'; | |
| describe('generateCreatorSlug', () => { | |
| it('should convert to lowercase', () => { | |
| expect(generateCreatorSlug('Tech Reviews Pro')).toBe('tech-reviews-pro'); | |
| }); | |
| it('should replace spaces with hyphens', () => { | |
| expect(generateCreatorSlug('Multiple Spaces')).toBe('multiple-spaces'); | |
| }); | |
| it('should remove special characters', () => { | |
| expect(generateCreatorSlug('Mr. Beast!!!')).toBe('mr-beast'); | |
| expect(generateCreatorSlug('Tech@Reviews#Pro')).toBe('techreviewspro'); | |
| }); | |
| it('should remove consecutive hyphens', () => { | |
| expect(generateCreatorSlug('Tech---Reviews')).toBe('tech-reviews'); | |
| }); | |
| it('should trim hyphens from start and end', () => { | |
| expect(generateCreatorSlug(' -Tech Reviews- ')).toBe('tech-reviews'); | |
| }); | |
| it('should handle empty string', () => { | |
| expect(generateCreatorSlug('')).toBe(''); | |
| }); | |
| it('should handle only special characters', () => { | |
| expect(generateCreatorSlug('!!!')).toBe(''); | |
| }); | |
| it('should handle complex channel names', () => { | |
| expect(generateCreatorSlug('Linus Tech Tips')).toBe('linus-tech-tips'); | |
| expect(generateCreatorSlug('MKBHD')).toBe('mkbhd'); | |
| expect(generateCreatorSlug('The Verge')).toBe('the-verge'); | |
| }); | |
| }); | |
| describe('ensureUniqueSlug', () => { | |
| it('should return original slug if no collision', () => { | |
| expect(ensureUniqueSlug('tech-reviews', [])).toBe('tech-reviews'); | |
| expect(ensureUniqueSlug('tech-reviews', ['other-slug'])).toBe('tech-reviews'); | |
| }); | |
| it('should append -2 for first collision', () => { | |
| expect(ensureUniqueSlug('tech-reviews', ['tech-reviews'])).toBe('tech-reviews-2'); | |
| }); | |
| it('should increment counter for multiple collisions', () => { | |
| expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-2'])).toBe('tech-reviews-3'); | |
| expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-2', 'tech-reviews-3'])).toBe('tech-reviews-4'); | |
| }); | |
| it('should handle non-sequential existing slugs', () => { | |
| expect(ensureUniqueSlug('tech-reviews', ['tech-reviews', 'tech-reviews-5'])).toBe('tech-reviews-2'); | |
| }); | |
| }); | |
| describe('isValidSlug', () => { | |
| it('should validate correct slugs', () => { | |
| expect(isValidSlug('tech-reviews')).toBe(true); | |
| expect(isValidSlug('tech-reviews-pro')).toBe(true); | |
| expect(isValidSlug('mkbhd')).toBe(true); | |
| expect(isValidSlug('tech123')).toBe(true); | |
| }); | |
| it('should reject uppercase letters', () => { | |
| expect(isValidSlug('Tech-Reviews')).toBe(false); | |
| expect(isValidSlug('MKBHD')).toBe(false); | |
| }); | |
| it('should reject special characters', () => { | |
| expect(isValidSlug('tech@reviews')).toBe(false); | |
| expect(isValidSlug('tech_reviews')).toBe(false); | |
| expect(isValidSlug('tech.reviews')).toBe(false); | |
| }); | |
| it('should reject slugs starting or ending with hyphen', () => { | |
| expect(isValidSlug('-tech-reviews')).toBe(false); | |
| expect(isValidSlug('tech-reviews-')).toBe(false); | |
| }); | |
| it('should reject consecutive hyphens', () => { | |
| expect(isValidSlug('tech--reviews')).toBe(false); | |
| }); | |
| it('should reject empty string', () => { | |
| expect(isValidSlug('')).toBe(false); | |
| }); | |
| it('should reject spaces', () => { | |
| expect(isValidSlug('tech reviews')).toBe(false); | |
| }); | |
| }); | |