From cbfbc4244d5086919a4c7fcd8bf926d82f7d5827 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sun, 26 Apr 2020 10:12:46 +0200 Subject: [PATCH 1/4] test(docs): Test actual markdown in textToHash Use markdown and render that instead of assuming a rendered output. Improves readability of tests by avoiding special chars --- docs/src/modules/utils/textToHash.test.js | 39 +++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/docs/src/modules/utils/textToHash.test.js b/docs/src/modules/utils/textToHash.test.js index 8ed6a3de145183..2126c95a67c6d6 100644 --- a/docs/src/modules/utils/textToHash.test.js +++ b/docs/src/modules/utils/textToHash.test.js @@ -1,22 +1,29 @@ -import { assert } from 'chai'; +import { assert, 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', + ], + ]; + 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', () => { From 85ebe9c2265622b743bd9f0ffa70cfb0591f51d2 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sun, 26 Apr 2020 10:14:34 +0200 Subject: [PATCH 2/4] document textToHash --- docs/src/modules/utils/textToHash.js | 5 +++++ 1 file changed, 5 insertions(+) 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( From 0e892a127afc8dda3ad5c321d110b71cd03e1d11 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sun, 26 Apr 2020 10:21:09 +0200 Subject: [PATCH 3/4] Add test for emojis --- docs/src/modules/utils/textToHash.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/modules/utils/textToHash.test.js b/docs/src/modules/utils/textToHash.test.js index 2126c95a67c6d6..3b53d60cc5e37d 100644 --- a/docs/src/modules/utils/textToHash.test.js +++ b/docs/src/modules/utils/textToHash.test.js @@ -16,6 +16,7 @@ describe('textToHash', () => { '@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; From c5565137b349f635c722b94fac4dac45093cf906 Mon Sep 17 00:00:00 2001 From: Sebastian Silbermann Date: Sun, 26 Apr 2020 10:21:42 +0200 Subject: [PATCH 4/4] assert -> expect --- docs/src/modules/utils/textToHash.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/src/modules/utils/textToHash.test.js b/docs/src/modules/utils/textToHash.test.js index 3b53d60cc5e37d..da29012c89af99 100644 --- a/docs/src/modules/utils/textToHash.test.js +++ b/docs/src/modules/utils/textToHash.test.js @@ -1,4 +1,4 @@ -import { assert, expect } from 'chai'; +import { expect } from 'chai'; import { render as renderMarkdown } from './parseMarkdown'; import textToHash from './textToHash'; @@ -29,7 +29,7 @@ describe('textToHash', () => { 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'); }); });