Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] Improve textToHash test #20770

Merged
merged 4 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/modules/utils/textToHash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, boolean>} [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(
Expand Down
44 changes: 26 additions & 18 deletions docs/src/modules/utils/textToHash.test.js
Original file line number Diff line number Diff line change
@@ -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&#39;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');
});
});