Files
ToolBox/tests/unit/variableName.test.js
T
renjue f1502ed08c
CI / docker (push) Successful in 11m42s
初始化 ToolBox 开发者工具箱。
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-25 11:15:38 +08:00

73 lines
1.9 KiB
JavaScript

import { describe, it, expect } from 'vitest'
import {
parseToWords,
toCamelCase,
toPascalCase,
toSnakeCase,
toKebabCase,
toConstantCase,
} from '../../src/utils/variableName.js'
describe('variableName', () => {
describe('parseToWords', () => {
it('returns empty array for blank input', () => {
expect(parseToWords('')).toEqual([])
expect(parseToWords(' ')).toEqual([])
})
it('parses snake_case', () => {
expect(parseToWords('hello_world')).toEqual(['hello', 'world'])
})
it('parses kebab-case', () => {
expect(parseToWords('hello-world')).toEqual(['hello', 'world'])
})
it('parses camelCase', () => {
expect(parseToWords('helloWorld')).toEqual(['hello', 'world'])
})
it('parses PascalCase', () => {
expect(parseToWords('HelloWorld')).toEqual(['hello', 'world'])
})
it('parses consecutive uppercase (XMLHttpRequest)', () => {
expect(parseToWords('XMLHttpRequest')).toEqual(['xml', 'http', 'request'])
})
it('parses numbers in identifiers', () => {
expect(parseToWords('item2')).toEqual(['item', '2'])
expect(parseToWords('temp2Detail')).toEqual(['temp', '2', 'detail'])
})
})
describe('case conversions', () => {
const words = ['hello', 'world']
it('converts to camelCase', () => {
expect(toCamelCase(words)).toBe('helloWorld')
expect(toCamelCase([])).toBe('')
})
it('converts to PascalCase', () => {
expect(toPascalCase(words)).toBe('HelloWorld')
})
it('converts to snake_case', () => {
expect(toSnakeCase(words)).toBe('hello_world')
})
it('converts to kebab-case', () => {
expect(toKebabCase(words)).toBe('hello-world')
})
it('converts to CONSTANT_CASE', () => {
expect(toConstantCase(words)).toBe('HELLO_WORLD')
})
it('preserves numeric words in camelCase', () => {
expect(toCamelCase(['item', '2'])).toBe('item2')
})
})
})