import { deepCopy, hasKey, srfFilePath2, createUUID, isExist, isExistAndNotEmpty, hasFunction, isEmpty, isUndefined, notEmpty, typeOf, dateFormat, verifyDynamicLogic, verifyNodeLinkCond, debounce, throttle, getSessionStorage, setSessionStorage, srfpluralize, deepObjectMerge, computedNavData, getRouteParams, fillStrData, formatActionData, getLocal, UpperFirstLetter, getBoxSize, formatUrlString, mergeViewParams, transformRawData, transformDoData, } from '@/core/utils/util'; import { ControlVOBase } from '@/core/modules/control-vo/control-vo-base'; describe('hasKey unit test', () => { test('hasKey result is true', () => { const data = { name: 'zhaangsan', age: 24 }; expect(hasKey(data, 'name')).toBe(true); }); test('hasKey result is false', () => { const data = { name: 'zhaangsan', age: 24 }; expect(hasKey(data, 'height')).toBe(false); }); }); describe('deepCopy unit test', () => { test('deepCopy', () => { const obj = { a: { b: 11 }, c: 22 }; expect(deepCopy(obj)).toStrictEqual({ a: { b: 11 }, c: 22 }); }); }); describe('srfFilePath2 unit test', () => { test('srfFilePath2', () => { const string = 'TestViewName'; expect(srfFilePath2(string)).toBe('test-view-name'); }); test('srfFilePath2 empty', () => { const string = ''; expect(srfFilePath2(string)).toBe(''); }); }); describe('createUUID unit test', () => { test('createUUID', () => { expect(createUUID()).toBeTypeOf('string'); }); }); describe('isExist unit test', () => { test('isExist undefined', () => { const obj = undefined; expect(isExist(obj)).toBe(false); }); test('isExist null', () => { const obj = null; expect(isExist(obj)).toBe(false); }); test('isExist str', () => { const obj = ''; expect(isExist(obj)).toBe(true); }); test('isExist number', () => { const obj = 0; expect(isExist(obj)).toBe(true); }); test('isExist arr', () => { const obj = ['']; expect(isExist(obj)).toBe(true); }); test('isExist function', () => { const obj = () => {}; expect(isExist(obj)).toBe(true); }); }); describe('isExistAndNotEmpty unit test', () => { test('isExistAndNotEmpty undefined', () => { const obj = undefined; expect(isExistAndNotEmpty(obj)).toBe(false); }); test('isExistAndNotEmpty null', () => { const obj = null; expect(isExistAndNotEmpty(obj)).toBe(false); }); test('isExistAndNotEmpty str', () => { const obj = ''; expect(isExistAndNotEmpty(obj)).toBe(false); }); test('isExistAndNotEmpty str', () => { const obj = '123'; expect(isExistAndNotEmpty(obj)).toBe(true); }); }); describe('hasFunction unit test', () => { test('hasFunction is true', () => { const obj = { fn: () => {}, }; expect(hasFunction(obj, 'fn')).toBe(true); }); test('hasFunction is false', () => { const obj = { fn: 'testFn', }; expect(hasFunction(obj, 'fn')).toBe(false); }); }); describe('isEmpty unit test', () => { test('isEmpty is true', () => { const obj = undefined; expect(isEmpty(obj)).toBe(true); }); test('isEmpty is true', () => { const obj = null; expect(isEmpty(obj)).toBe(true); }); test('isEmpty str', () => { const obj = ''; expect(isEmpty(obj)).toBe(true); }); test('isEmpty number', () => { const obj = 0; expect(isEmpty(obj)).toBe(false); }); test('isEmpty arr', () => { const obj = ['']; expect(isEmpty(obj)).toBe(false); }); test('isEmpty function', () => { const obj = () => {}; expect(isEmpty(obj)).toBe(false); }); }); describe('isUndefined unit test', () => { test('isUndefined is true', () => { const obj = undefined; expect(isUndefined(obj)).toBe(true); }); test('isUndefined is true', () => { const obj = null; expect(isUndefined(obj)).toBe(false); }); test('isUndefined str', () => { const obj = ''; expect(isUndefined(obj)).toBe(false); }); test('isUndefined number', () => { const obj = 0; expect(isUndefined(obj)).toBe(false); }); test('isUndefined arr', () => { const obj = ['']; expect(isUndefined(obj)).toBe(false); }); test('isUndefined function', () => { const obj = () => {}; expect(isUndefined(obj)).toBe(false); }); }); describe('notEmpty unit test', () => { test('notEmpty is false', () => { const obj = undefined; expect(notEmpty(obj)).toBe(false); }); test('notEmpty is false', () => { const obj = null; expect(notEmpty(obj)).toBe(false); }); test('notEmpty is false', () => { const obj = 0; expect(notEmpty(obj)).toBe(false); }); test('notEmpty is false', () => { const obj = ''; expect(notEmpty(obj)).toBe(false); }); test('notEmpty arr', () => { const obj = ['123']; expect(notEmpty(obj)).toBe(true); }); test('notEmpty function', () => { const obj = () => {}; expect(notEmpty(obj)).toBe(true); }); }); describe('typeOf unit test', () => { test('typeOf', () => { const obj = false; expect(typeOf(obj)).toBe('boolean'); }); test('typeOf', () => { const obj = 0; expect(typeOf(obj)).toBe('number'); }); test('typeOf', () => { const obj = ''; expect(typeOf(obj)).toBe('string'); }); test('typeOf', () => { const obj = () => {}; expect(typeOf(obj)).toBe('function'); }); test('typeOf', () => { const obj = [1, 2]; expect(typeOf(obj)).toBe('array'); }); test('typeOf', () => { const obj = new Date(); expect(typeOf(obj)).toBe('date'); }); test('typeOf', () => { const obj = /\a/; expect(typeOf(obj)).toBe('regExp'); }); test('typeOf', () => { const obj = undefined; expect(typeOf(obj)).toBe('undefined'); }); test('typeOf', () => { const obj = null; expect(typeOf(obj)).toBe('null'); }); }); describe('dateFormat unit test', () => { test('dateFormat', () => { const date = new Date('January 12,2022 22:19:35'); expect(dateFormat(date, 'YYYY-MM-DD')).toBe('2022-01-12'); }); }); describe('verifyDynamicLogic unit test', () => { test('verifyDynamicLogic', () => { const data = { orderstate: "10", } const logic = { name: '表单成员[memo][表单项空输入]逻辑', logicType: 'GROUP', logicCat: 'ITEMBLANK', notMode: false, groupOP: 'AND', relatedDetailNames: ["orderstate"], childLogics: [ { name: 'AND[取反]', logicType: 'GROUP', logicCat: '', notMode: true, groupOP: 'AND', relatedDetailNames: [], childLogics: [ { name: 'orderstate 等于(=) (10)', logicType: 'SINGLE', logicCat: '', condOP: "EQ", dEFDName: "orderstate", value: "10" }, ] }, ] } expect(verifyDynamicLogic(data, logic)).toBe(false); }) }); describe('verifyNodeLinkCond unit test', () => { test('verifyNodeLinkCond', () => { const linkCond = { name: '', logicType: 'GROUP', notMode: false, groupOP: 'AND', childLogics: [ { name: 'verifyValue 值不为空(NotNil)', logicType: 'SINGLE', condOP: "ISNOTNULL", verifyValue: "sjdgjdhfdajfjasf", value: "" } ], } expect(verifyNodeLinkCond(linkCond)).toBe(true); }) }); describe('debounce unit test', () => { test('debounce', () => { let number: number = 0; const add = () => { number++; } vi.useFakeTimers(); debounce(add, [], {}, 1000); debounce(add, [], {}, 1000); setTimeout(() => { expect(number).toBe(1); }, 3000); vi.runAllTimers(); }) }); describe('throttle unit test', () => { // TODO 节流测试异常 test('throttle', () => { let number: number = 0; const add = () => { number++; } vi.useFakeTimers(); throttle(add, [], {}, 1000); throttle(add, [], {}, 1000); setTimeout(() => { expect(number).toBe(2); }, 3000); vi.runAllTimers(); }) }); describe('getSessionStorage setSessionStorage unit test', () => { test('getSessionStorage', () => { const str = 'word'; setSessionStorage('1', str); expect(getSessionStorage('1')).toBe(str); }); }); describe('srfpluralize unit test', () => { test('srfpluralize', () => { const str = 'word'; expect(srfpluralize(str)).toBe('words'); }); }); describe('deepObjectMerge unit test', () => { test('deepObjectMerge', () => { const obj1 = { a: 11, b: { c: 22 } }; const obj2 = { b: { c: 33 }, d: 44, e: { f: 55 } }; expect(deepObjectMerge(obj1, obj2)).toStrictEqual({ a: 11, b: { c: 33 }, d: 44, e: { f: 55 }, }); }); }); describe('computedNavData unit test', () => { test('computedNavData', () => { const data = { aaa: 111 }; const parentContext = { bbb: 222 }; const parentParam = { ccc: 333 }; const params = { a: '%aaa%', b: '%bbb%', c: '%ccc%' }; expect( computedNavData(data, parentContext, parentParam, params) ).toStrictEqual({ a: 111, b: 222, c: 333, }); }); }); describe('getRouteParams unit test', () => { test('getRouteParams', () => { const str = 'http://127.0.0.1:8080/?aaa=bbb&ccc=ddd#/index/views/viewtypeindexview'; expect(getRouteParams(str)).toStrictEqual({ aaa: 'bbb', ccc: 'ddd' }); }); }); describe('fillStrData unit test', () => { test('fillStrData', () => { const str = 'http://127.0.0.1:8080/?aaa=${context.aaa}&bbb=${data.bbb}#/index/views/viewtypeindexview'; const context = { aaa: 111 }; const viewparams = { bbb: 222 }; expect(fillStrData(str, context, viewparams)).toBe( 'http://127.0.0.1:8080/?aaa=111&bbb=222#/index/views/viewtypeindexview' ); }); }); describe('formatActionData unit test', () => { test('formatActionData', () => { const actionTarget = 'SINGLEKEY'; const args = [{ aaa: 111 }]; const parentContext = { aaa: 222 }; const parentParams = { bbb: 222 }; const _params = { a: '%aaa%', b: '%bbb%' }; expect( formatActionData(actionTarget, args, parentContext, parentParams, _params) ).toStrictEqual({ a: 111, b: 222, }); }); test('formatActionData', () => { const actionTarget = 'MULTIKEY'; const args = [{ aaa: 111 }, { bbb: 222 }]; const parentContext = { aaa: 333 }; const parentParams = { bbb: 444 }; const _params = { a: '%aaa%', b: '%bbb%' }; expect( formatActionData(actionTarget, args, parentContext, parentParams, _params) ).toStrictEqual({ a: '111,333', b: '444,222', }); }); }); describe('getLocal unit test', () => { test('getLocal', () => { expect(getLocal()).toBe('zh_CN'); }); }); describe('UpperFirstLetter unit test', () => { test('UpperFirstLetter', () => { const str = 'abcdefg'; expect(UpperFirstLetter(str)).toBe('Abcdefg'); }); }); describe('getBoxSize unit test', () => { test('getBoxSize', () => { expect(getBoxSize('WIDTH', 'PERCENTAGE', 10)).toStrictEqual({ width: '10%', }); }); }); describe('transformRawData unit test', () => { test('transformRawData', () => { const data = new ControlVOBase(); expect(transformRawData(data)).toStrictEqual({ srfuf: '0', }) }) }); describe('transformDoData unit test', () => { test('transformDoData', () => { const data = new ControlVOBase(); data.setDo({ test: '1', test2: '2', }) expect(transformDoData(data)).toStrictEqual({ test: '1', test2: '2', }) }) }); describe('formatUrlString unit test', () => { test('formatUrlString', () => { const url = 'http://127.0.0.1:8080/?aaa=${context.aaa}&bbb=${data.bbb}&ccc=${viewparams.ccc}#/index/views/viewtypeindexview'; const context = { aaa: 111 }; const viewparams = { ccc: 333 }; const data = { bbb: 222 }; expect(formatUrlString(url, context, viewparams, data)).toBe( 'http://127.0.0.1:8080/?aaa=111&bbb=222&ccc=333#/index/views/viewtypeindexview' ); }); }); describe('mergeViewParams unit test', () => { test('mergeViewParams', () => { let obj1 = { a: 1 }; let obj2 = { a: 2, b: 3 }; mergeViewParams(obj1, obj2); expect(obj1).toStrictEqual({ a: 1, b: 3, }); }); });