strapi/packages/strapi-utils/lib/__tests__/string-formatting.test.js
Alexandre Bodin 07e7cfc0bd Make lint stricter and fix the errors
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
2020-11-02 19:41:42 +01:00

68 lines
1.9 KiB
JavaScript

'use strict';
const { escapeQuery, stringIncludes, stringEquals } = require('../string-formatting');
describe('string-formatting', () => {
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);
}
);
});
describe('stringIncludes', () => {
const tests = [
[['1', '2', '3'], '1', true],
[['1', '2', '3'], '4', false],
[[1, 2, 3], 1, true],
[[1, 2, 3], 4, false],
[[1, 2, 3], '1', true],
[[1, 2, 3], '4', false],
[[1, 2, 3], '01', false],
[['1', '2', '3'], 1, true],
[['1', '2', '3'], 4, false],
[['01', '02', '03'], 1, false],
];
test.each(tests)('%p includes %p : %p', (arr, val, expectedResult) => {
const result = stringIncludes(arr, val);
expect(result).toBe(expectedResult);
});
});
describe('stringEquals', () => {
const tests = [
['1', '1', true],
['1', '4', false],
[1, 1, true],
[1, 4, false],
[1, '1', true],
[1, '4', false],
[1, '01', false],
['1', 1, true],
['1', 4, false],
['01', 1, false],
];
test.each(tests)('%p includes %p : %p', (a, b, expectedResult) => {
const result = stringEquals(a, b);
expect(result).toBe(expectedResult);
});
});
});