|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { poupeUnicornRules } from '../unicorn'; |
| 4 | + |
| 5 | +interface PreventAbbreviationsOptions { |
| 6 | + allowList: Record<string, boolean> |
| 7 | + replacements: Record<string, boolean | string | string[]> |
| 8 | +} |
| 9 | + |
| 10 | +interface FilenameCaseOptions { |
| 11 | + case: string |
| 12 | + ignore?: string[] |
| 13 | +} |
| 14 | + |
| 15 | +describe('unicorn configuration', () => { |
| 16 | + describe('prevent-abbreviations rule', () => { |
| 17 | + it('should have correct configuration', () => { |
| 18 | + const rule = poupeUnicornRules['unicorn/prevent-abbreviations']; |
| 19 | + expect(rule).toBeDefined(); |
| 20 | + expect(Array.isArray(rule)).toBe(true); |
| 21 | + if (Array.isArray(rule)) { |
| 22 | + expect(rule[0]).toBe('error'); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + it('should allow i, j, k as variable names', () => { |
| 27 | + const rule = poupeUnicornRules['unicorn/prevent-abbreviations']; |
| 28 | + expect(Array.isArray(rule)).toBe(true); |
| 29 | + if (Array.isArray(rule) && rule.length > 1) { |
| 30 | + const options = rule[1] as PreventAbbreviationsOptions; |
| 31 | + expect(options.allowList).toBeDefined(); |
| 32 | + expect(options.allowList.i).toBe(true); |
| 33 | + expect(options.allowList.j).toBe(true); |
| 34 | + expect(options.allowList.k).toBe(true); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not suggest replacements for i, j, k', () => { |
| 39 | + const rule = poupeUnicornRules['unicorn/prevent-abbreviations']; |
| 40 | + expect(Array.isArray(rule)).toBe(true); |
| 41 | + if (Array.isArray(rule) && rule.length > 1) { |
| 42 | + const options = rule[1] as PreventAbbreviationsOptions; |
| 43 | + expect(options.replacements).toBeDefined(); |
| 44 | + // i, j, k should not be in replacements object since they're in omitReplacementList |
| 45 | + expect(options.replacements.i).toBeUndefined(); |
| 46 | + expect(options.replacements.j).toBeUndefined(); |
| 47 | + expect(options.replacements.k).toBeUndefined(); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('should allow other common abbreviations', () => { |
| 52 | + const rule = poupeUnicornRules['unicorn/prevent-abbreviations']; |
| 53 | + expect(Array.isArray(rule)).toBe(true); |
| 54 | + if (Array.isArray(rule) && rule.length > 1) { |
| 55 | + const options = rule[1] as PreventAbbreviationsOptions; |
| 56 | + const commonAbbreviations = ['env', 'err', 'fn', 'pkg', 'props', 'utils']; |
| 57 | + |
| 58 | + for (const abbr of commonAbbreviations) { |
| 59 | + expect(options.allowList[abbr]).toBe(true); |
| 60 | + } |
| 61 | + } |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('filename-case rule', () => { |
| 66 | + it('should enforce kebab-case with exceptions for uppercase markdown files', () => { |
| 67 | + const rule = poupeUnicornRules['unicorn/filename-case']; |
| 68 | + expect(rule).toBeDefined(); |
| 69 | + expect(Array.isArray(rule)).toBe(true); |
| 70 | + |
| 71 | + if (Array.isArray(rule) && rule.length > 1) { |
| 72 | + const severity = rule[0]; |
| 73 | + const options = rule[1] as FilenameCaseOptions; |
| 74 | + expect(severity).toBe('error'); |
| 75 | + expect(options.case).toBe('kebabCase'); |
| 76 | + expect(options.ignore).toBeDefined(); |
| 77 | + expect(options.ignore?.[0]).toBe(String.raw`^[A-Z][A-Z0-9\-_]*\.md$`); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments