Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Feb 23, 2018
1 parent 99e208c commit d58e606
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/lib/__tests__/urlHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,58 @@
import { sanitizeIRI, sanitizeSlug } from '../urlHelper';
import { sanitizeURI, sanitizeSlug } from '../urlHelper';

describe('sanitizeIRI', () => {
// `sanitizeIRI` tests from RFC 3987
describe('sanitizeURI', () => {
// `sanitizeURI` tests from RFC 3987
it('should keep valid URI chars (letters digits _ - . ~)', () => {
expect(
sanitizeIRI("This, that-one_or.the~other 123!")
sanitizeURI("This, that-one_or.the~other 123!")
).toEqual('Thisthat-one_or.the~other123');
});

it('should not remove accents', () => {
expect(
sanitizeIRI("ěščřžý")
sanitizeURI("ěščřžý")
).toEqual('ěščřžý');
});

it('should keep valid non-latin chars (ucschars in RFC 3987)', () => {
expect(
sanitizeIRI("日本語のタイトル")
sanitizeURI("日本語のタイトル")
).toEqual('日本語のタイトル');
});

it('should not keep valid non-latin chars (ucschars in RFC 3987) if set to ASCII mode', () => {
expect(
sanitizeURI("ěščřžý日本語のタイトル", { type: 'ascii' })
).toEqual('');
});

it('should not normalize Unicode strings', () => {
expect(
sanitizeIRI('\u017F\u0323\u0307')
sanitizeURI('\u017F\u0323\u0307')
).toEqual('\u017F\u0323\u0307');
expect(
sanitizeIRI('\u017F\u0323\u0307')
sanitizeURI('\u017F\u0323\u0307')
).not.toEqual('\u1E9B\u0323');
});

it('should allow a custom replacement character', () => {
expect(
sanitizeIRI("duck\\goose.elephant", { replacement: '-' })
sanitizeURI("duck\\goose.elephant", { replacement: '-' })
).toEqual('duck-goose.elephant');
});

it('should not allow an improper replacement character', () => {
expect(() => {
sanitizeIRI("I! like! dollars!", { replacement: '$' });
sanitizeURI("I! like! dollars!", { replacement: '$' });
}).toThrow();
});

it('should not actually URI-encode the characters', () => {
expect(
sanitizeIRI("🎉")
sanitizeURI("🎉")
).toEqual('🎉');
expect(
sanitizeIRI("🎉")
sanitizeURI("🎉")
).not.toEqual("%F0%9F%8E%89");
});
});
Expand Down Expand Up @@ -81,6 +87,18 @@ describe('sanitizeSlug', ()=> {
).toEqual('This-that-one_or.the~other-123');
});

it('should remove accents in "latin" mode', () => {
expect(
sanitizeSlug("ěščřžý", { slugType: 'latin' })
).toEqual('escrzy');
});

it('should remove non-latin chars in "latin" mode', () => {
expect(
sanitizeURI("ěščřžý日本語のタイトル", { slugType: 'latin' })
).toEqual('escrzy-');
});

it('removes double replacements', () => {
expect(sanitizeSlug('test--test')).toEqual('test-test');
expect(sanitizeSlug('test test')).toEqual('test-test');
Expand Down

0 comments on commit d58e606

Please sign in to comment.