|
| 1 | +import {applyEntityNameCase} from '../string-utils'; |
| 2 | + |
| 3 | +// Some of the test cases were taken from https://github.com/sindresorhus/camelcase/blob/main/test.js |
| 4 | +const camelCaseTestCases = { |
| 5 | + foo: 'foo', |
| 6 | + IDs: 'ids', |
| 7 | + FooIDs: 'fooIds', |
| 8 | + 'foo-bar': 'fooBar', |
| 9 | + 'foo-bar-baz': 'fooBarBaz', |
| 10 | + 'foo--bar': 'fooBar', |
| 11 | + '--foo-bar': 'fooBar', |
| 12 | + '--foo--bar': 'fooBar', |
| 13 | + 'FOO-BAR': 'fooBar', |
| 14 | + '-foo-bar-': 'fooBar', |
| 15 | + '--foo--bar--': 'fooBar', |
| 16 | + 'foo-1': 'foo1', |
| 17 | + 'foo.bar': 'fooBar', |
| 18 | + 'foo..bar': 'fooBar', |
| 19 | + '..foo..bar..': 'fooBar', |
| 20 | + foo_bar: 'fooBar', |
| 21 | + __foo__bar__: 'fooBar', |
| 22 | + 'foo bar': 'fooBar', |
| 23 | + ' foo bar ': 'fooBar', |
| 24 | + '-': '', |
| 25 | + ' - ': '', |
| 26 | + fooBar: 'fooBar', |
| 27 | + 'fooBar-baz': 'fooBarBaz', |
| 28 | + 'fooBarBaz-bazzy': 'fooBarBazBazzy', |
| 29 | + FBBazzy: 'fbBazzy', |
| 30 | + F: 'f', |
| 31 | + FooBar: 'fooBar', |
| 32 | + Foo: 'foo', |
| 33 | + FOO: 'foo', |
| 34 | + '--': '', |
| 35 | + '': '', |
| 36 | + _: '', |
| 37 | + ' ': '', |
| 38 | + '.': '', |
| 39 | + '..': '', |
| 40 | + ' ': '', |
| 41 | + __: '', |
| 42 | + '--__--_--_': '', |
| 43 | + XMLHttpRequest: 'xmlHttpRequest', |
| 44 | + AjaxXMLHttpRequest: 'ajaxXmlHttpRequest', |
| 45 | + 'Ajax-XMLHttpRequest': 'ajaxXmlHttpRequest', |
| 46 | + 'mGridCol6@md': 'mGridCol6Md', |
| 47 | + 'A::a': 'aA', |
| 48 | + Hello1World: 'hello1World', |
| 49 | + Hello11World: 'hello11World', |
| 50 | + hello1world: 'hello1World', |
| 51 | + Hello1World11foo: 'hello1World11Foo', |
| 52 | + Hello1: 'hello1', |
| 53 | + hello1: 'hello1', |
| 54 | + h2w: 'h2W' |
| 55 | +}; |
| 56 | + |
| 57 | +describe('string-utils', () => { |
| 58 | + describe('applyEntityNameCase()', () => { |
| 59 | + it('should format a sentence', () => { |
| 60 | + expect(applyEntityNameCase('Hello World!', 'kebabCase')).toBe('hello-world'); |
| 61 | + }); |
| 62 | + it('should format a filename', () => { |
| 63 | + expect(applyEntityNameCase('filename-without-ext', 'snakeCase')).toBe('filename_without_ext'); |
| 64 | + }); |
| 65 | + it('should format a snake-case', () => { |
| 66 | + expect(applyEntityNameCase('CONST_NAME_EXAMPLE', 'pascalCase')).toBe('ConstNameExample'); |
| 67 | + }); |
| 68 | + it('should format a simple camel-case string', () => { |
| 69 | + expect(applyEntityNameCase('simpleCamelCaseString', 'kebabCase')).toBe('simple-camel-case-string'); |
| 70 | + }); |
| 71 | + it('should format a camel-case string with abbrev', () => { |
| 72 | + expect(applyEntityNameCase('openAPILibrary', 'kebabCase')).toBe('open-api-library'); |
| 73 | + }); |
| 74 | + it('should format a camel-case string with abbrev with numbers', () => { |
| 75 | + expect(applyEntityNameCase('publicV2Api', 'kebabCase')).toBe('public-v2-api'); |
| 76 | + }); |
| 77 | + it('should work for the specified camelCase testcases', () => { |
| 78 | + for (const [input, expected] of Object.entries(camelCaseTestCases)) { |
| 79 | + expect(applyEntityNameCase(input, 'camelCase')).toBe(expected); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments