import { StringUtils } from '../../src/utils/StringUtils'; import { isEmptyCases, replaceAccentuatedCharsCases, replaceAccentuatedCharsKeepSymbolsCases, replaceBlankCharactersCases, replaceToSpaceCases, replaceAllCases, removeSpecialCharactersCases, isStringCases, decodeHtmlEntitiesCases, hashCodeCases, getBooleanValueCases, padStartCases, padEndCases, compareCases, toCamelCaseCases, toPascalCaseCases, toSnakeCaseCases, toKebabCaseCases, formatBytesCases, prettyPrecisionCases, isCaseableCases, isLowerCaseCases, getOppositeCaseCases, getSpecialCharactersCases, getArgumentNumberCases, highlightValueCases } from '../testCases/StringUtilsTestCases'; describe('StringUtils', function() { describe('isEmpty', () => { isEmptyCases.forEach(e => { it(`should return ${e.valueOutput} when value is ${e.valueInput}`, () => { expect(StringUtils.isEmpty(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('replaceAccentuatedChars', () => { it('should return the original value when called with an object without a toString method', () => { const objWithoutToString = { value: 'someValue' }; jest.spyOn(StringUtils, 'replaceAccentuatedChars'); StringUtils.replaceAccentuatedChars(objWithoutToString as any); expect(StringUtils.replaceAccentuatedChars).toHaveBeenCalledWith(objWithoutToString as any); }); it('should return the original value when called with undefined', () => { const undefinedArg: any = undefined; jest.spyOn(StringUtils, 'replaceAccentuatedChars'); StringUtils.replaceAccentuatedChars(undefinedArg); expect(StringUtils.replaceAccentuatedChars).toHaveBeenCalledWith(undefinedArg); }); replaceAccentuatedCharsCases.forEach(e => { it(`should return ${e.valueOutput} when value is ${e.valueInput}`, () => { expect(StringUtils.replaceAccentuatedChars(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('replaceAccentuatedCharsKeepSymbols', () => { replaceAccentuatedCharsKeepSymbolsCases.forEach(e => { it(`should return ${e.valueOutput} when value is ${e.valueInput}`, () => { expect(StringUtils.replaceAccentuatedCharsKeepSymbols(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('replaceBlankCharacters', () => { replaceBlankCharactersCases.forEach(e => { it(`should return ${e.valueOutput} when value is ${e.valueInput}`, () => { expect(StringUtils.replaceBlankCharacters(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('replaceToSpace', () => { replaceToSpaceCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}" and replaceList is ${JSON.stringify(e.replaceList)}`, () => { expect(StringUtils.replaceToSpace(e.valueInput, e.replaceList)).toBe(e.valueOutput); }); }); }); describe('replaceAll', () => { replaceAllCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}", strFrom is "${e.strFrom}", and strTo is "${e.strTo}"`, () => { expect(StringUtils.replaceAll(e.valueInput, e.strFrom, e.strTo)).toBe(e.valueOutput); }); }); }); describe('removeSpecialCharacters', () => { removeSpecialCharactersCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.removeSpecialCharacters(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('isString', () => { isStringCases.forEach(e => { it(`should return ${e.valueOutput} when value is ${JSON.stringify(e.valueInput)}`, () => { expect(StringUtils.isString(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('decodeHtmlEntities', () => { decodeHtmlEntitiesCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.decodeHtmlEntities(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('hashCode', () => { hashCodeCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.hashCode(e.valueInput)).toBe(e.valueOutput); }); }); }); describe('getBooleanValue', () => { getBooleanValueCases.forEach(e => { it(`should return ${e.valueOutput} when value is "${e.valueInput}" and defaultValue is ${e.defaultValue}`, () => { expect(StringUtils.getBooleanValue(e.valueInput as any, e.defaultValue)).toBe(e.valueOutput); }); }); }); describe('padStart', () => { padStartCases.forEach(e => { const { str, len, pad } = e.valueInput; it(`should return "${e.valueOutput}" when str="${str}", len=${len}, pad="${pad}"`, () => { expect(StringUtils.padStart(str as any, len, pad)).toBe(e.valueOutput); }); }); }); describe('padEnd', () => { padEndCases.forEach(e => { const { str, len, pad } = e.valueInput; it(`should return "${e.valueOutput}" when str="${str}", len=${len}, pad="${pad}"`, () => { expect(StringUtils.padEnd(str as any, len, pad)).toBe(e.valueOutput); }); }); }); describe('compare', () => { compareCases.forEach(e => { const { a, b } = e.valueInput; it(`should return ${e.valueOutput} when a="${a}" and b="${b}"`, () => { expect(StringUtils.compare(a as any, b as any)).toBe(e.valueOutput); }); }); }); describe('toCamelCase', () => { toCamelCaseCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.toCamelCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('toPascalCase', () => { toPascalCaseCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.toPascalCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('toSnakeCase', () => { toSnakeCaseCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.toSnakeCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('toKebabCase', () => { toKebabCaseCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.toKebabCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('formatBytes', () => { formatBytesCases.forEach(e => { it(`should return "${e.valueOutput}" when value is ${e.valueInput}`, () => { expect(StringUtils.formatBytes(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('prettyPrecision', () => { prettyPrecisionCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils['prettyPrecision'](e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('isCaseable', () => { isCaseableCases.forEach(e => { it(`should return ${e.valueOutput} when value is "${e.valueInput}"`, () => { expect(StringUtils.isCaseable(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('isLowerCase', () => { isLowerCaseCases.forEach(e => { it(`should return ${e.valueOutput} when value is "${e.valueInput}"`, () => { expect(StringUtils.isLowerCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('getOppositeCase', () => { getOppositeCaseCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput}"`, () => { expect(StringUtils.getOppositeCase(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('getSpecialCharacters', () => { getSpecialCharactersCases.forEach(e => { it(`should return ${JSON.stringify(e.valueOutput)} when value is "${e.valueInput}"`, () => { expect(StringUtils.getSpecialCharacters(e.valueInput as any)).toEqual(e.valueOutput); }); }); }); describe('getArgumentNumber', () => { getArgumentNumberCases.forEach(e => { it(`should return ${e.valueOutput} when value is "${e.valueInput}"`, () => { expect(StringUtils.getArgumentNumber(e.valueInput as any)).toBe(e.valueOutput); }); }); }); describe('highlightValue', () => { highlightValueCases.forEach(e => { it(`should return "${e.valueOutput}" when value is "${e.valueInput.value}" and argument is "${e.valueInput.argument}"`, () => { expect(StringUtils.highlightValue(e.valueInput.argument as any, e.valueInput.matchFields, e.valueInput.value, e.valueInput.fieldMD, e.valueInput.forceMatch)).toBe(e.valueOutput); }); }); }); });