2023-07-29 08:31:34 +02:00
|
|
|
import { _Slugger } from '../../src/Slugger.js';
|
2023-06-09 22:10:12 -05:00
|
|
|
|
|
|
|
describe('Test slugger functionality', () => {
|
|
|
|
it('should use lowercase slug', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('Test')).toBe('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be unique to avoid collisions 1280', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('test')).toBe('test');
|
|
|
|
expect(slugger.slug('test')).toBe('test-1');
|
|
|
|
expect(slugger.slug('test')).toBe('test-2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be unique when slug ends with number', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('test 1')).toBe('test-1');
|
|
|
|
expect(slugger.slug('test')).toBe('test');
|
|
|
|
expect(slugger.slug('test')).toBe('test-2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be unique when slug ends with hyphen number', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('foo')).toBe('foo');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-1');
|
|
|
|
expect(slugger.slug('foo 1')).toBe('foo-1-1');
|
|
|
|
expect(slugger.slug('foo-1')).toBe('foo-1-2');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should allow non-latin chars', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('привет')).toBe('привет');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove ampersands 857', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('This & That Section')).toBe('this--that-section');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove periods', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('file.txt')).toBe('filetxt');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should remove html tags', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('<em>html</em>')).toBe('html');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not increment seen when using dryrun option', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section');
|
|
|
|
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should still return the next unique id when using dryrun', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('<h1>This Section</h1>')).toBe('this-section');
|
|
|
|
expect(slugger.slug('<h1>This Section</h1>', { dryrun: true })).toBe('this-section-1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be repeatable in a sequence', () => {
|
2023-07-29 08:31:34 +02:00
|
|
|
const slugger = new _Slugger();
|
2023-06-09 22:10:12 -05:00
|
|
|
expect(slugger.slug('foo')).toBe('foo');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-1');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-2');
|
|
|
|
expect(slugger.slug('foo', { dryrun: true })).toBe('foo-3');
|
|
|
|
expect(slugger.slug('foo', { dryrun: true })).toBe('foo-3');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-3');
|
|
|
|
expect(slugger.slug('foo')).toBe('foo-4');
|
|
|
|
});
|
|
|
|
});
|