|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + camelCaseToKebabCase, |
| 4 | + capitalize, |
| 5 | + kebabCaseToCamelCase, |
| 6 | + toSentenceCase, |
| 7 | + toTitleCase, |
| 8 | +} from './case-conversions.js'; |
| 9 | + |
| 10 | +describe('capitalize', () => { |
| 11 | + it('should transform the first string letter to upper case', () => { |
| 12 | + expect(capitalize('code')).toBe('Code'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('should lowercase all but the the first string letter', () => { |
| 16 | + expect(capitalize('PushUp')).toBe('Pushup'); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should leave the first string letter in upper case', () => { |
| 20 | + expect(capitalize('Code')).toBe('Code'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should accept empty string', () => { |
| 24 | + expect(capitalize('')).toBe(''); |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +describe('kebabCaseToCamelCase', () => { |
| 29 | + it('should convert simple kebab-case to camelCase', () => { |
| 30 | + expect(kebabCaseToCamelCase('hello-world')).toBe('helloWorld'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should handle multiple hyphens', () => { |
| 34 | + expect(kebabCaseToCamelCase('this-is-a-long-string')).toBe( |
| 35 | + 'thisIsALongString', |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should preserve numbers', () => { |
| 40 | + expect(kebabCaseToCamelCase('user-123-test')).toBe('user123Test'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should handle single word', () => { |
| 44 | + expect(kebabCaseToCamelCase('hello')).toBe('hello'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should handle empty string', () => { |
| 48 | + expect(kebabCaseToCamelCase('')).toBe(''); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('camelCaseToKebabCase', () => { |
| 53 | + it('should convert camelCase to kebab-case', () => { |
| 54 | + expect(camelCaseToKebabCase('myTestString')).toBe('my-test-string'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle acronyms properly', () => { |
| 58 | + expect(camelCaseToKebabCase('APIResponse')).toBe('api-response'); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle consecutive uppercase letters correctly', () => { |
| 62 | + expect(camelCaseToKebabCase('myXMLParser')).toBe('my-xml-parser'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle single-word camelCase', () => { |
| 66 | + expect(camelCaseToKebabCase('singleWord')).toBe('single-word'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not modify already kebab-case strings', () => { |
| 70 | + expect(camelCaseToKebabCase('already-kebab')).toBe('already-kebab'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should not modify non-camelCase inputs', () => { |
| 74 | + expect(camelCaseToKebabCase('not_camelCase')).toBe('not_camel-case'); |
| 75 | + }); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('toTitleCase', () => { |
| 79 | + it('should capitalize each word in a simple sentence', () => { |
| 80 | + expect(toTitleCase('hello world')).toBe('Hello World'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should capitalize each word in a longer sentence', () => { |
| 84 | + expect(toTitleCase('this is a title')).toBe('This Is a Title'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should convert PascalCase to title case', () => { |
| 88 | + expect(toTitleCase('FormatToTitleCase')).toBe('Format to Title Case'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should convert camelCase to title case', () => { |
| 92 | + expect(toTitleCase('thisIsTest')).toBe('This Is Test'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should convert kebab-case to title case', () => { |
| 96 | + expect(toTitleCase('hello-world-example')).toBe('Hello World Example'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should convert snake_case to title case', () => { |
| 100 | + expect(toTitleCase('snake_case_example')).toBe('Snake Case Example'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should capitalize a single word', () => { |
| 104 | + expect(toTitleCase('hello')).toBe('Hello'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should handle numbers in words correctly', () => { |
| 108 | + expect(toTitleCase('chapter1Introduction')).toBe('Chapter 1 Introduction'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should handle numbers in slugs correctly', () => { |
| 112 | + expect(toTitleCase('version2Release')).toBe('Version 2 Release'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should handle acronyms properly', () => { |
| 116 | + expect(toTitleCase('apiResponse')).toBe('Api Response'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should handle mixed-case inputs correctly', () => { |
| 120 | + expect(toTitleCase('thisIs-mixed_CASE')).toBe('This Is Mixed CASE'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should not modify already formatted title case text', () => { |
| 124 | + expect(toTitleCase('Hello World')).toBe('Hello World'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should return an empty string when given an empty input', () => { |
| 128 | + expect(toTitleCase('')).toBe(''); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('toSentenceCase', () => { |
| 133 | + it('should convert a simple sentence to sentence case', () => { |
| 134 | + expect(toSentenceCase('hello world')).toBe('Hello world'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('should maintain a correctly formatted sentence', () => { |
| 138 | + expect(toSentenceCase('This is a test')).toBe('This is a test'); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should convert PascalCase to sentence case', () => { |
| 142 | + expect(toSentenceCase('FormatToSentenceCase')).toBe( |
| 143 | + 'Format to sentence case', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should convert camelCase to sentence case', () => { |
| 148 | + expect(toSentenceCase('thisIsTest')).toBe('This is test'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should convert kebab-case to sentence case', () => { |
| 152 | + expect(toSentenceCase('hello-world-example')).toBe('Hello world example'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should convert snake_case to sentence case', () => { |
| 156 | + expect(toSentenceCase('snake_case_example')).toBe('Snake case example'); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should capitalize a single word', () => { |
| 160 | + expect(toSentenceCase('hello')).toBe('Hello'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should handle numbers in words correctly', () => { |
| 164 | + expect(toSentenceCase('chapter1Introduction')).toBe( |
| 165 | + 'Chapter 1 introduction', |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it('should handle numbers in slugs correctly', () => { |
| 170 | + expect(toSentenceCase('version2Release')).toBe('Version 2 release'); |
| 171 | + }); |
| 172 | + |
| 173 | + it('should handle acronyms properly', () => { |
| 174 | + expect(toSentenceCase('apiResponse')).toBe('Api response'); |
| 175 | + }); |
| 176 | + |
| 177 | + it('should handle mixed-case inputs correctly', () => { |
| 178 | + expect(toSentenceCase('thisIs-mixed_CASE')).toBe('This is mixed case'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should not modify already formatted sentence case text', () => { |
| 182 | + expect(toSentenceCase('This is a normal sentence.')).toBe( |
| 183 | + 'This is a normal sentence.', |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should return an empty string when given an empty input', () => { |
| 188 | + expect(toSentenceCase('')).toBe(''); |
| 189 | + }); |
| 190 | +}); |
0 commit comments