From e0e561e6d0e2c146ebb54e1ec350cfa6bb27d981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Thu, 30 Apr 2020 12:11:12 +0200 Subject: [PATCH] add test for escapeQuery() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Pierre Noël --- .../lib/__tests__/stringFormatting.test.js | 25 +++++++++++++++++++ packages/strapi-utils/lib/stringFormatting.js | 4 +-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 packages/strapi-utils/lib/__tests__/stringFormatting.test.js diff --git a/packages/strapi-utils/lib/__tests__/stringFormatting.test.js b/packages/strapi-utils/lib/__tests__/stringFormatting.test.js new file mode 100644 index 0000000000..2ebe5cafd4 --- /dev/null +++ b/packages/strapi-utils/lib/__tests__/stringFormatting.test.js @@ -0,0 +1,25 @@ +const { escapeQuery } = require('../stringFormatting'); + +describe('Escape Query', () => { + const testData = [ + // [query, charsToEscape, escapeChar, expectedResult] + ['123', '[%\\', '\\', '123'], + ['12%3', '[%\\', '\\', '12\\%3'], + ['1[2%3', '[%\\', '\\', '1\\[2\\%3'], + ['1\\23', '[%\\', '\\', '1\\\\23'], + ['123\\', '[%\\', '\\', '123\\\\'], + ['\\', '[%\\', '\\', '\\\\'], + ['123', '[%\\', '+', '123'], + ['12%3', '[%\\', '+', '12+%3'], + ['1[2%3', '[%\\', '+', '1+[2+%3'], + ['1\\23', '[%\\', '+', '1+\\23'], + ]; + + test.each(testData)( + 'Escaping %s from %s with %s', + (query, charsToEscape, escapeChar, expectedResult) => { + const result = escapeQuery(query, charsToEscape, escapeChar); + expect(result).toEqual(expectedResult); + } + ); +}); diff --git a/packages/strapi-utils/lib/stringFormatting.js b/packages/strapi-utils/lib/stringFormatting.js index 553b2e1c83..691e0831e4 100644 --- a/packages/strapi-utils/lib/stringFormatting.js +++ b/packages/strapi-utils/lib/stringFormatting.js @@ -6,13 +6,13 @@ const nameToSlug = name => slugify(name, { separator: '-' }); const nameToCollectionName = name => slugify(name, { separator: '_' }); -const escapeQuery = (query, charsToEscape, espaceChar = '\\') => { +const escapeQuery = (query, charsToEscape, escapeChar = '\\') => { return query .split('') .reduce( (escapedQuery, char) => charsToEscape.includes(char) - ? `${escapedQuery}${espaceChar}${char}` + ? `${escapedQuery}${escapeChar}${char}` : `${escapedQuery}${char}`, '' );