import { describe, it, expect } from 'vitest' import { deleteObject, paramsInterfaceValidKey, getTreeObjById, ObjectInterface } from '../util' import { __getItem, __setItem } from '../storage' describe('deleteObject', () => { const array: ObjectInterface[] = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }, { id: 4, name: 'David' }, { id: 5, name: 'Eve' }] it('should delete object with matching value', () => { deleteObject('Bob', array, 'name') expect(array).toEqual([{ id: 1, name: 'Alice' }, { id: 3, name: 'Charlie' }, { id: 4, name: 'David' }, { id: 5, name: 'Eve' }]) }) it('should delete multiple objects with matching value', () => { deleteObject('Alice', array, 'name') expect(array).toEqual([{ id: 3, name: 'Charlie' }, { id: 4, name: 'David' }, { id: 5, name: 'Eve' }]) }) it('should not delete any objects if no match found', () => { deleteObject('Frank', array, 'name') expect(array).toEqual([{ id: 3, name: 'Charlie' }, { id: 4, name: 'David' }, { id: 5, name: 'Eve' }]) }) }) describe('__getItem', () => { it('should return value from sessionStorage', () => { const key = 'testKey' const value: ObjectInterface = { id: 1, name: 'testValue' } sessionStorage.setItem(key, JSON.stringify(value)) expect(__getItem(key)).toEqual(value) }) it('should return value from localStorage', () => { const key = 'testKey' const value: ObjectInterface = { id: 1, name: 'testValue' } localStorage.setItem(key, JSON.stringify(value)) expect(__getItem(key, 'localStorage')).toEqual(value) }) it('should return string value if not JSON parsable', () => { const key = 'testKey' const value = 'notJsonParsable' sessionStorage.setItem(key, value) expect(__getItem(key)).toEqual(value) }) }) describe('__setItem', () => { it('should set value in sessionStorage', () => { const key = 'testKey' const value = 'testValue' __setItem(key, value) expect(sessionStorage.getItem(key)).toEqual(value) }) it('should set value in localStorage', () => { const key = 'testKey' const value = 'testValue' __setItem(key, value, 'localStorage') expect(localStorage.getItem(key)).toEqual(value) }) it('should convert object to JSON string before setting', () => { const key = 'testKey' const value = { id: 1, name: 'testValue' } __setItem(key, value) expect(sessionStorage.getItem(key)).toEqual(JSON.stringify(value)) }) it('should not convert string to JSON string', () => { const key = 'testKey' const value = 'notJsonParsable' __setItem(key, value) expect(sessionStorage.getItem(key)).toEqual(value) }) }) describe('paramsInterfaceValidKey', () => { it('should add valid keys from params to obj', () => { const obj = {} const params = { id: 1, name: 'test' } paramsInterfaceValidKey(obj, params) expect(obj).toEqual(params) }) it('should not add keys with undefined values from params to obj', () => { const obj = {} const params = { id: 1, name: undefined } paramsInterfaceValidKey(obj, params) expect(obj).toEqual({ id: 1 }) }) }) describe('getTreeObjById', () => { const data = [ { id: '1', name: 'node 1', children: [ { id: '2', name: 'node 2', children: [ { id: '3', name: 'node 3' }, { id: '4', name: 'node 4' } ] }] } ] it('should return undefined if id is not found', () => { expect(getTreeObjById(data, '5')).toEqual(undefined) }) it('should return the correct object if id is found', () => { expect(getTreeObjById(data, '3')).toEqual({ id: '3', name: 'node 3' }) }) it('should be able to search for id in a different key', () => { expect(getTreeObjById(data, '2', 'nodes')).toEqual(undefined) expect(getTreeObjById(data, '2', 'children')).toEqual({ id: '2', name: 'node 2', children: [{ id: '3', name: 'node 3' }, { id: '4', name: 'node 4' }] }) }) })