diff --git a/docs/src/modules/utils/textToHash.js b/docs/src/modules/utils/textToHash.js index e78c38c7d8be10..88df88d0341dae 100644 --- a/docs/src/modules/utils/textToHash.js +++ b/docs/src/modules/utils/textToHash.js @@ -9,6 +9,11 @@ function makeUnique(hash, unique, i = 1) { return makeUnique(hash, unique, i + 1); } +/** + * @param {string} text - HTML from e.g. parseMarkdown#render + * @param {Record} [unique] - Ensures that each output is unique in `unique` + * @returns {string} that is safe to use in fragment links + */ export default function textToHash(text, unique = {}) { return makeUnique( encodeURI( diff --git a/docs/src/modules/utils/textToHash.test.js b/docs/src/modules/utils/textToHash.test.js index 8ed6a3de145183..da29012c89af99 100644 --- a/docs/src/modules/utils/textToHash.test.js +++ b/docs/src/modules/utils/textToHash.test.js @@ -1,27 +1,35 @@ -import { assert } from 'chai'; +import { expect } from 'chai'; +import { render as renderMarkdown } from './parseMarkdown'; import textToHash from './textToHash'; describe('textToHash', () => { - it('should hash correctly', () => { - assert.strictEqual( - textToHash('createMuiTheme(options) => theme'), - 'createmuitheme-options-theme', - ); - assert.strictEqual(textToHash('Typography - Font family'), 'typography-font-family'); - assert.strictEqual(textToHash('barre d'application'), 'barre-dapplication'); - assert.strictEqual( - textToHash('createGenerateClassName([options]) => class name generator'), - 'creategenerateclassname-options-class-name-generator', - ); - assert.strictEqual( - textToHash('@material-ui/core/styles vs @material-ui/styles'), - 'material-ui-core-styles-vs-material-ui-styles', - ); + it('should hash as expected', () => { + const table = [ + ['createMuiTheme(options) => theme', 'createmuitheme-options-theme'], + ['Typography - Font family', 'typography-font-family'], + ["barre d'application", 'barre-dapplication'], + [ + 'createGenerateClassName([options]) => class name generator', + 'creategenerateclassname-options-class-name-generator', + ], + [ + '@material-ui/core/styles vs @material-ui/styles', + 'material-ui-core-styles-vs-material-ui-styles', + ], + ['Blog 📝', 'blog'], + ]; + table.forEach((entry, index) => { + const [markdown, expected] = entry; + const text = renderMarkdown(markdown); + const actual = textToHash(text); + + expect(actual).to.equal(expected, `snapshot #${index} matches`); + }); }); it('should generate a unique hash', () => { const unique = {}; - assert.strictEqual(textToHash('Styling solution', unique), 'styling-solution'); - assert.strictEqual(textToHash('Styling solution', unique), 'styling-solution-2'); + expect(textToHash('Styling solution', unique)).to.equal('styling-solution'); + expect(textToHash('Styling solution', unique)).to.equal('styling-solution-2'); }); });