Skip to content

Commit

Permalink
test: to-valid-identifier-name
Browse files Browse the repository at this point in the history
  • Loading branch information
bholmesdev committed Aug 11, 2022
1 parent 389b5a5 commit 135aa2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"scripts": {
"prepack": "npm run build",
"build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
"test-api": "uvu test \"^(compile|evaluate)\\.js$\"",
"test-api": "uvu test \"^(compile|evaluate|to-valid-identifier-name)\\.js$\"",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run test-coverage"
},
Expand Down
45 changes: 45 additions & 0 deletions packages/mdx/test/to-valid-identifier-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {test} from 'uvu'
import * as assert from 'uvu/assert'
import {toValidIdentifierName} from '../lib/util/to-valid-identifier-name.js'

/** @param {string} invalidChar */
function toTestFailureMessage(invalidChar) {
return `Invalid characters like "${invalidChar}" should be converted to underscores "_"`
}

test('toValidIdentifierName', () => {
// Valid strings left untouched
assert.equal(toValidIdentifierName('test'), 'test')
assert.equal(toValidIdentifierName('camelCase'), 'camelCase')
// Invalid cont character -> underscore
assert.equal(
toValidIdentifierName('custom-element'),
'custom_element',
toTestFailureMessage('-')
)
assert.equal(
toValidIdentifierName('custom element'),
'custom_element',
toTestFailureMessage(' ')
)
// Invalid starting character -> underscore
assert.equal(
toValidIdentifierName('-badStarting'),
'_badStarting',
toTestFailureMessage('-')
)
assert.equal(
toValidIdentifierName('1badStarting'),
'_badStarting',
toTestFailureMessage('1')
)
assert.equal(
toValidIdentifierName(' badStarting'),
'_badStarting',
toTestFailureMessage(' ')
)
// Empty string -> underscore
assert.equal('_', toValidIdentifierName(''))
})

test.run()

0 comments on commit 135aa2d

Please sign in to comment.