Skip to content

Commit a02589a

Browse files
committed
feat: unit tests for reparenting (#148)
1 parent 7e4e397 commit a02589a

File tree

3 files changed

+91
-5
lines changed

3 files changed

+91
-5
lines changed

packages/core/src/controlledEnvironment/DraggingPositionEvaluation.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ export class DraggingPositionEvaluation {
7878
private maybeReparentUpwards(): DraggingPosition | undefined {
7979
const treeLinearItems = this.env.linearItems[this.treeId];
8080
const deepestDepth = treeLinearItems[this.linearIndex].depth;
81+
82+
// Default to zero on last position to allow dropping on root when
83+
// dropping at bottom
8184
const legalDropDepthCount = // itemDepthDifferenceToNextItem/isLastInGroup
8285
deepestDepth - (treeLinearItems[this.linearIndex + 1]?.depth ?? 0);
86+
8387
const canReparentUpwards =
8488
this.offset === 'bottom' && legalDropDepthCount > 0;
85-
// Default to zero on last position to allow dropping on root when
86-
// dropping at bottom
8789

8890
if (!canReparentUpwards) {
8991
return undefined;

packages/core/test/dnd-basics.spec.tsx

+80
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,84 @@ describe('dnd basics', () => {
371371
await test.expectItemContentsVisibleAndUnchanged('a', 'b', 'bb');
372372
});
373373
});
374+
375+
describe('special drop positions', () => {
376+
describe('reparent upwards', () => {
377+
it('doesnt reparent in the on normal item', async () => {
378+
const test = await new TestUtil().renderOpenTree();
379+
await test.startDrag('bbb');
380+
await test.dragOver('aac', 'bottom', 0);
381+
await test.drop();
382+
await test.expectItemContentsUnchanged('a', 'b');
383+
await test.expectItemContents('aa', [
384+
'aaa',
385+
'aab',
386+
'aac',
387+
'bbb',
388+
'aad',
389+
]);
390+
});
391+
392+
it('doesnt reparent in the middle of a subtree', async () => {
393+
const test = await new TestUtil().renderTree();
394+
await test.clickItem('a');
395+
await test.clickItem('target-parent');
396+
await test.startDrag('target');
397+
await test.dragOver('ab', 'bottom', 0);
398+
await test.drop();
399+
await test.expectItemContentsUnchanged('ab', 'ac', 'root');
400+
await test.expectItemContents('a', ['aa', 'ab', 'target', 'ac', 'ad']);
401+
});
402+
403+
it('doesnt reparent at the top of a subtree', async () => {
404+
const test = await new TestUtil().renderOpenTree();
405+
await test.startDrag('bbb');
406+
await test.dragOver('aaa', 'top', 0);
407+
await test.drop();
408+
await test.expectItemContentsUnchanged('a', 'b');
409+
await test.expectItemContents('aa', [
410+
'bbb',
411+
'aaa',
412+
'aab',
413+
'aac',
414+
'aad',
415+
]);
416+
});
417+
418+
it('reparents inner level', async () => {
419+
const test = await new TestUtil().renderOpenTree();
420+
await test.startDrag('bbb');
421+
await test.dragOver('add', 'bottom', 1);
422+
await test.drop();
423+
await test.expectItemContentsUnchanged('ad', 'b');
424+
await test.expectItemContents('a', ['aa', 'ab', 'ac', 'ad', 'bbb']);
425+
});
426+
427+
it('reparents mid level', async () => {
428+
const test = await new TestUtil().renderOpenTree();
429+
await test.startDrag('bbb');
430+
await test.dragOver('add', 'bottom', 1);
431+
await test.drop();
432+
await test.expectItemContentsUnchanged('ad', 'b', 'root');
433+
await test.expectItemContents('a', ['aa', 'ab', 'ac', 'ad', 'bbb']);
434+
});
435+
436+
it('reparents outer level', async () => {
437+
const test = await new TestUtil().renderOpenTree();
438+
await test.startDrag('bbb');
439+
await test.dragOver('add', 'bottom', 0);
440+
await test.drop();
441+
await test.expectItemContentsUnchanged('ad', 'a', 'b');
442+
await test.expectItemContents('root', [
443+
'target-parent',
444+
'a',
445+
'bbb',
446+
'b',
447+
'c',
448+
'deep1',
449+
'special',
450+
]);
451+
});
452+
});
453+
});
374454
});

packages/core/test/helpers/TestUtil.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ export class TestUtil {
146146
});
147147
}
148148

149-
public async dragOver(title: string, position?: 'top' | 'bottom') {
149+
public async dragOver(
150+
title: string,
151+
position?: 'top' | 'bottom',
152+
indent?: number
153+
) {
150154
await act(async () => {
151155
const items = await this.renderProps!.findAllByTestId('title');
152156
const itemIndex = items.findIndex(item => item.innerHTML === title);
153157

154-
// jsom doesnt support drag events :( let's mock it directly on the dnd context
158+
// jsdom doesnt support drag events :( let's mock it directly on the dnd context
155159
this.environmentRef?.dragAndDropContext.onDragOverTreeHandler(
156160
{
157-
clientX: 0,
161+
clientX: indent !== undefined ? indent * 10 : 9999,
158162
clientY:
159163
itemIndex * 10 +
160164
(position === 'top' ? 1 : position === 'bottom' ? 9 : 5),

0 commit comments

Comments
 (0)