test: update unit test of @idraw/util

This commit is contained in:
chenshenhai 2024-08-03 21:49:28 +08:00
parent 1dda8be77e
commit 515ade4794
3 changed files with 107 additions and 2 deletions

View file

@ -1,4 +1,4 @@
import { createUUID, deepClone, getElementPositionFromList } from '@idraw/util';
import { createUUID, getElementPositionFromList } from '@idraw/util';
import type { Elements } from '@idraw/types';
const getElemBase = () => {
return {

View file

@ -0,0 +1,92 @@
import type { Element } from '@idraw/types';
import { groupElementsByPosition, ungroupElementsByPosition, insertElementToListByPosition } from '@idraw/util';
const createList = () => {
const list: Element[] = [
{ uuid: '0', type: 'rect', x: 20, y: 20, w: 50, h: 50, detail: {} },
{ uuid: '1', type: 'rect', x: 40, y: 40, w: 50, h: 50, detail: {} },
{ uuid: '2', type: 'rect', x: 60, y: 60, w: 50, h: 50, detail: {} },
{ uuid: '3', type: 'rect', x: 80, y: 80, w: 50, h: 50, detail: {} },
{ uuid: '4', type: 'rect', x: 100, y: 100, w: 50, h: 50, detail: {} }
];
return list;
};
const createGroupedList = () => {
const list: Element[] = [
{ uuid: '0', type: 'rect', x: 20, y: 20, w: 50, h: 50, detail: {} },
{
name: 'Group',
uuid: 'group-id',
type: 'group',
x: 40,
y: 40,
w: 90,
h: 90,
detail: {
children: [
{ uuid: '1', type: 'rect', x: 0, y: 0, w: 50, h: 50, detail: {} },
{ uuid: '2', type: 'rect', x: 20, y: 20, w: 50, h: 50, detail: {} },
{ uuid: '3', type: 'rect', x: 40, y: 40, w: 50, h: 50, detail: {} }
]
}
},
{ uuid: '4', type: 'rect', x: 100, y: 100, w: 50, h: 50, detail: {} }
];
return list;
};
describe('@idraw/util: group ', () => {
test('groupElementsByPosition', () => {
const list = createList();
groupElementsByPosition(list, [[1], [2], [3]]);
list[1].uuid = 'group-id';
expect(list).toStrictEqual(createGroupedList());
});
test('groupElementsByPosition with disordered positions', () => {
const list = createList();
groupElementsByPosition(list, [[3], [1], [2]]);
list[1].uuid = 'group-id';
expect(list).toStrictEqual(createGroupedList());
});
test('groupElementsByPosition with deep position', () => {
const list = createList();
list[0].type = 'group';
list[0].detail = { children: createList() };
groupElementsByPosition(list, [
[0, 1],
[0, 2],
[0, 3]
]);
list[0].detail.children[1].uuid = 'group-id';
const expectedList = createList();
expectedList[0].type = 'group';
expectedList[0].detail = { children: createGroupedList() };
expect(list).toStrictEqual(expectedList);
});
test('groupElementsByPosition with deep position and disordered positions', () => {
const list = createList();
list[0].type = 'group';
list[0].detail = { children: createList() };
groupElementsByPosition(list, [
[0, 3],
[0, 1],
[0, 2]
]);
list[0].detail.children[1].uuid = 'group-id';
const expectedList = createList();
expectedList[0].type = 'group';
expectedList[0].detail = { children: createGroupedList() };
expect(list).toStrictEqual(expectedList);
});
test('upgroupElementsByPosition', () => {
const list = createGroupedList();
ungroupElementsByPosition(list, [1]);
expect(list).toStrictEqual(createList());
});
});

View file

@ -1,4 +1,4 @@
import { rotatePoint, parseAngleToRadian } from '@idraw/util';
import { rotatePoint, parseAngleToRadian, parseRadianToAngle, calcRadian } from '@idraw/util';
describe('@idraw/util: rotate', () => {
test('rotatePoint', () => {
@ -211,4 +211,17 @@ describe('@idraw/util: rotate', () => {
expect(start4.x).toBeCloseTo(start0.x);
expect(start4.y).toBeCloseTo(start0.y);
});
test('calcRadian 1', () => {
const start = { x: 402, y: 328 };
const end = { x: 341, y: 414 };
const center = { x: 400, y: 400 };
expect(parseRadianToAngle(calcRadian(center, start, end))).toBeCloseTo(255.060132615518);
});
test('calcRadian 2', () => {
const start = { x: 402, y: 328 };
const end = { x: 340, y: 392 };
const center = { x: 400, y: 400 };
expect(parseRadianToAngle(calcRadian(center, start, end))).toBeCloseTo(276.00350309739684);
});
});