From 135aa2dedaeb2ab431c25c0027acd4db158540dc Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 11 Aug 2022 19:37:18 -0400 Subject: [PATCH] test: to-valid-identifier-name --- packages/mdx/package.json | 2 +- packages/mdx/test/to-valid-identifier-name.js | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 packages/mdx/test/to-valid-identifier-name.js diff --git a/packages/mdx/package.json b/packages/mdx/package.json index 0de70ca05..fc5412fa7 100644 --- a/packages/mdx/package.json +++ b/packages/mdx/package.json @@ -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" }, diff --git a/packages/mdx/test/to-valid-identifier-name.js b/packages/mdx/test/to-valid-identifier-name.js new file mode 100644 index 000000000..6e1334449 --- /dev/null +++ b/packages/mdx/test/to-valid-identifier-name.js @@ -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()