Skip to content

Commit 135aa2d

Browse files
committed
test: to-valid-identifier-name
1 parent 389b5a5 commit 135aa2d

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"scripts": {
8585
"prepack": "npm run build",
8686
"build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
87-
"test-api": "uvu test \"^(compile|evaluate)\\.js$\"",
87+
"test-api": "uvu test \"^(compile|evaluate|to-valid-identifier-name)\\.js$\"",
8888
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
8989
"test": "npm run build && npm run test-coverage"
9090
},
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {test} from 'uvu'
2+
import * as assert from 'uvu/assert'
3+
import {toValidIdentifierName} from '../lib/util/to-valid-identifier-name.js'
4+
5+
/** @param {string} invalidChar */
6+
function toTestFailureMessage(invalidChar) {
7+
return `Invalid characters like "${invalidChar}" should be converted to underscores "_"`
8+
}
9+
10+
test('toValidIdentifierName', () => {
11+
// Valid strings left untouched
12+
assert.equal(toValidIdentifierName('test'), 'test')
13+
assert.equal(toValidIdentifierName('camelCase'), 'camelCase')
14+
// Invalid cont character -> underscore
15+
assert.equal(
16+
toValidIdentifierName('custom-element'),
17+
'custom_element',
18+
toTestFailureMessage('-')
19+
)
20+
assert.equal(
21+
toValidIdentifierName('custom element'),
22+
'custom_element',
23+
toTestFailureMessage(' ')
24+
)
25+
// Invalid starting character -> underscore
26+
assert.equal(
27+
toValidIdentifierName('-badStarting'),
28+
'_badStarting',
29+
toTestFailureMessage('-')
30+
)
31+
assert.equal(
32+
toValidIdentifierName('1badStarting'),
33+
'_badStarting',
34+
toTestFailureMessage('1')
35+
)
36+
assert.equal(
37+
toValidIdentifierName(' badStarting'),
38+
'_badStarting',
39+
toTestFailureMessage(' ')
40+
)
41+
// Empty string -> underscore
42+
assert.equal('_', toValidIdentifierName(''))
43+
})
44+
45+
test.run()

0 commit comments

Comments
 (0)