Skip to content

Commit

Permalink
test: update unit test of @idraw/util
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshenhai committed Aug 3, 2024
1 parent 1dda8be commit 515ade4
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/util/__tests__/lib/element.test.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
92 changes: 92 additions & 0 deletions packages/util/__tests__/lib/group.test.ts
Original file line number Diff line number Diff line change
@@ -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());
});
});
15 changes: 14 additions & 1 deletion packages/util/__tests__/lib/rotate.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rotatePoint, parseAngleToRadian } from '@idraw/util';
import { rotatePoint, parseAngleToRadian, parseRadianToAngle, calcRadian } from '@idraw/util';

describe('@idraw/util: rotate', () => {
test('rotatePoint', () => {
Expand Down Expand Up @@ -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);
});
});

0 comments on commit 515ade4

Please sign in to comment.