Skip to content

Commit

Permalink
Fix tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tech4him1 committed Mar 17, 2018
1 parent 202e985 commit 7cdf7d7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
27 changes: 17 additions & 10 deletions src/lib/__tests__/urlHelper.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Map } from 'immutable';
import { sanitizeURI, sanitizeSlug } from '../urlHelper';

describe('sanitizeURI', () => {
Expand All @@ -22,7 +23,7 @@ describe('sanitizeURI', () => {

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

Expand Down Expand Up @@ -71,14 +72,14 @@ describe('sanitizeSlug', ()=> {
});

it('throws an error for non-string replacements', () => {
expect(() => sanitizeSlug('test', { sanitize_replacement: {} })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', { sanitize_replacement: [] })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', { sanitize_replacement: false })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', { sanitize_replacement: null } )).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', { sanitize_replacement: 11232 })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: {} }))).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: [] }))).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: false }))).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: null } ))).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: 11232 }))).toThrowError("`options.replacement` must be a string.");
// do not test undefined for this variant since a default is set in the cosntructor.
//expect(() => sanitizeSlug('test', { sanitize_replacement: undefined })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', { sanitize_replacement: ()=>{} })).toThrowError("`options.replacement` must be a string.");
expect(() => sanitizeSlug('test', Map({ sanitize_replacement: ()=>{} }))).toThrowError("`options.replacement` must be a string.");
});

it('should keep valid URI chars (letters digits _ - . ~)', () => {
Expand All @@ -89,13 +90,19 @@ describe('sanitizeSlug', ()=> {

it('should remove accents if set', () => {
expect(
sanitizeSlug("ěščřžý", { clean_accents: true })
sanitizeSlug("ěščřžý", Map({ clean_accents: true }))
).toEqual('escrzy');
});

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

it('should clean accents and strip non-latin chars in "ascii" mode with `clean_accents` set', () => {
expect(
sanitizeSlug("ěščřžý日本語のタイトル", Map({ encoding: 'ascii', clean_accents: true }))
).toEqual('escrzy');
});

Expand All @@ -109,7 +116,7 @@ describe('sanitizeSlug', ()=> {
});

it('uses alternate replacements', () => {
expect(sanitizeSlug('test test ', { sanitize_replacement: '_' })).toEqual('test_test');
expect(sanitizeSlug('test test ', Map({ sanitize_replacement: '_' }))).toEqual('test_test');
});

});
4 changes: 2 additions & 2 deletions src/lib/urlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import url from 'url';
import diacritics from 'diacritics';
import sanitizeFilename from 'sanitize-filename';
import { isString, escapeRegExp, flow, partialRight } from 'lodash';
import { stringOptions } from './functionHelper';
import { Map } from 'immutable';

function getUrl(urlString, direct) {
return `${ direct ? '/#' : '' }${ urlString }`;
Expand Down Expand Up @@ -66,7 +66,7 @@ export function sanitizeURI(str, { replacement = "", encoding = "unicode" } = {}
return Array.from(str).map(char => (validChar(char) ? char : replacement)).join('');
}

export function sanitizeSlug(str, options) {
export function sanitizeSlug(str, options = Map()) {
const encoding = options.get('encoding', 'unicode');
const stripDiacritics = options.get('clean_accents', false);
const replacement = options.get('sanitize_replacement', '-');
Expand Down

0 comments on commit 7cdf7d7

Please sign in to comment.