From 564c47c4940b0803facff201ee278065218131f3 Mon Sep 17 00:00:00 2001 From: Caleb Date: Sat, 17 Mar 2018 16:19:12 -0600 Subject: [PATCH] Fix tests. --- src/lib/__tests__/urlHelper.spec.js | 27 +++++++++++++++++---------- src/lib/urlHelper.js | 4 ++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/lib/__tests__/urlHelper.spec.js b/src/lib/__tests__/urlHelper.spec.js index c80542c9974e..6ffaf795587c 100644 --- a/src/lib/__tests__/urlHelper.spec.js +++ b/src/lib/__tests__/urlHelper.spec.js @@ -1,3 +1,4 @@ +import { Map } from 'immutable'; import { sanitizeURI, sanitizeSlug } from '../urlHelper'; describe('sanitizeURI', () => { @@ -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(''); }); @@ -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 _ - . ~)', () => { @@ -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'); }); @@ -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'); }); }); diff --git a/src/lib/urlHelper.js b/src/lib/urlHelper.js index 86aaafc22391..4ef79b8d21bf 100644 --- a/src/lib/urlHelper.js +++ b/src/lib/urlHelper.js @@ -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 }`; @@ -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', '-');