From 4060e6c44b9272a9ff7681ef93ae5b1c0505f4cd Mon Sep 17 00:00:00 2001 From: KoloMl Date: Wed, 25 Jun 2025 19:13:04 +0400 Subject: [PATCH] Covering utils with tests --- tests/lib/utils.spec.ts | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/lib/utils.spec.ts diff --git a/tests/lib/utils.spec.ts b/tests/lib/utils.spec.ts new file mode 100644 index 0000000..c24a98d --- /dev/null +++ b/tests/lib/utils.spec.ts @@ -0,0 +1,43 @@ +import { randomString } from "$tests/utils"; +import { escapeRegExp, findDeepObject } from "$lib/utils"; +import { randomInt } from "node:crypto"; + +describe('findDeepObject', () => { + const targetObject = { + somewhere: { + deep: { + stringValue: randomString(), + numericValue: randomInt(0, 1000), + } + } + }; + + it('should just return null when nothing is found', () => { + const nonExistentValue = findDeepObject(targetObject, ['completely', 'wrong']); + expect(nonExistentValue).toBe(null); + }); + + it('should retrieve something stored deep inside object', () => { + const returnedDeepObject = findDeepObject(targetObject, ['somewhere', 'deep']); + expect(returnedDeepObject).toBe(targetObject.somewhere.deep); + }); + + it('should return null if value located on given path is not an object', () => { + const returnedForStringValue = findDeepObject(targetObject, ['somewhere', 'deep', 'stringValue']); + expect(returnedForStringValue).not.toBe(targetObject.somewhere.deep.stringValue); + expect(returnedForStringValue).toBe(null); + + const returnedForNumericValue = findDeepObject(targetObject, ['somewhere', 'deep', 'numericValue']); + expect(returnedForNumericValue).not.toBe(targetObject.somewhere.deep.numericValue); + expect(returnedForNumericValue).toBe(null); + }); +}); + +describe('escapeRegExp', () => { + const specialCharactersToMatch = "$[(?:)]{}()*./\\+?|^"; + + it('should sufficiently enough escape special characters', () => { + const generatedRegExp = new RegExp(`^${escapeRegExp(specialCharactersToMatch)}$`, 'm'); + expect(generatedRegExp.test(specialCharactersToMatch)).toBe(true); + }); +});