Skip to content

Commit 8fdd098

Browse files
ollelauribostromMylesBorins
authored andcommitted
test: Adding tests for createSlug.
1 parent 3d6d5ae commit 8fdd098

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/util/createSlug.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ function createSlug(title) {
1313

1414
return slug
1515
.replace(/[^\w\-]+/g, '') // Remove any non word characters
16-
.replace(/\-\-+/g, '-'); // Replace multiple hyphens
16+
.replace(/\-\-+/g, '-') // Replace multiple hyphens with single
17+
.replace(/^-/, '') // Remove any leading hyphen
18+
.replace(/-$/, ''); // Remove any trailing hyphen
1719
}
1820

1921
module.exports = createSlug;

test/util/createSlug.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import createSlug from '../../src/util/createSlug';
2+
3+
describe('Tests for createSlug', () => {
4+
it('generates a slug', () => {
5+
const slugs = [
6+
'How to install Node.js',
7+
'The V8 JavaScript Engine',
8+
'The package.json guide',
9+
'Node.js & something.',
10+
'/usr/local/nodejs/bin',
11+
'a_b_c',
12+
'a, b and c',
13+
'title: subtitle',
14+
'a; b',
15+
'C:\\Program Files\\nodejs',
16+
'a---b'
17+
].map(createSlug);
18+
expect(slugs).toEqual([
19+
'how-to-install-nodejs',
20+
'the-v8-javascript-engine',
21+
'the-package-json-guide',
22+
'nodejs-and-something',
23+
'usr-local-nodejs-bin',
24+
'a-b-c',
25+
'a-b-and-c',
26+
'title-subtitle',
27+
'a-b',
28+
'c-program-files-nodejs',
29+
'a-b'
30+
]);
31+
});
32+
});

0 commit comments

Comments
 (0)