@pie-element/image-cloze-association 10.2.0 → 10.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,169 @@
1
+ import React from 'react';
2
+ import { render, fireEvent } from '@testing-library/react';
3
+ import ImageDropTarget from '../image-drop-target';
4
+
5
+ jest.mock('@dnd-kit/core', () => ({
6
+ useDroppable: () => ({
7
+ setNodeRef: jest.fn(),
8
+ isOver: false,
9
+ }),
10
+ }));
11
+
12
+ jest.mock('../possible-response', () => (props) => (
13
+ <div data-testid={`possible-response-${props.data.id}`} onClick={() => props.onSelectClick?.(props.data)}>
14
+ {props.data.value}
15
+ </div>
16
+ ));
17
+
18
+ describe('ImageDropTarget', () => {
19
+ const baseProps = {
20
+ answers: [],
21
+ canDrag: true,
22
+ containerStyle: {},
23
+ draggingElement: { id: '' },
24
+ onDragAnswerBegin: jest.fn(),
25
+ onDragAnswerEnd: jest.fn(),
26
+ onDrop: jest.fn(),
27
+ index: 2,
28
+ };
29
+
30
+ describe('empty container', () => {
31
+ it('is a native tab stop (role=button, tabIndex=0) when canDrag', () => {
32
+ const { container } = render(<ImageDropTarget {...baseProps} />);
33
+ const target = container.firstChild;
34
+
35
+ expect(target.getAttribute('role')).toBe('button');
36
+ expect(target.getAttribute('tabindex')).toBe('0');
37
+ });
38
+
39
+ it('is not a tab stop when canDrag is false, and has no role either (I2)', () => {
40
+ const { container } = render(<ImageDropTarget {...baseProps} canDrag={false} />);
41
+ const target = container.firstChild;
42
+
43
+ expect(target.getAttribute('tabindex')).toBe('-1');
44
+ expect(target.getAttribute('role')).toBeNull();
45
+ });
46
+
47
+ it('places the selection here on click', () => {
48
+ const onPlacementClick = jest.fn();
49
+ const { container } = render(
50
+ <ImageDropTarget
51
+ {...baseProps}
52
+ selectedResponse={{ id: '9', containerIndex: undefined }}
53
+ onPlacementClick={onPlacementClick}
54
+ />,
55
+ );
56
+
57
+ fireEvent.click(container.firstChild);
58
+
59
+ expect(onPlacementClick).toHaveBeenCalledWith(2);
60
+ });
61
+
62
+ it('does nothing on click when nothing is selected', () => {
63
+ const onPlacementClick = jest.fn();
64
+ const { container } = render(<ImageDropTarget {...baseProps} onPlacementClick={onPlacementClick} />);
65
+
66
+ fireEvent.click(container.firstChild);
67
+
68
+ expect(onPlacementClick).not.toHaveBeenCalled();
69
+ });
70
+
71
+ it('places the selection here on Space/Enter', () => {
72
+ const onPlacementClick = jest.fn();
73
+ const { container } = render(
74
+ <ImageDropTarget
75
+ {...baseProps}
76
+ selectedResponse={{ id: '9', containerIndex: undefined }}
77
+ onPlacementClick={onPlacementClick}
78
+ />,
79
+ );
80
+
81
+ fireEvent.keyDown(container.firstChild, { code: 'Space' });
82
+
83
+ expect(onPlacementClick).toHaveBeenCalledWith(2);
84
+ });
85
+ });
86
+
87
+ describe('occupied container', () => {
88
+ const answers = [{ id: '7', value: 'Placed', containerIndex: 2 }];
89
+
90
+ it('is not its own tab stop and has no role either — occupied containers must not become unlabeled buttons for screen readers (I2)', () => {
91
+ const { container } = render(<ImageDropTarget {...baseProps} answers={answers} />);
92
+ const target = container.firstChild;
93
+
94
+ expect(target.getAttribute('tabindex')).toBe('-1');
95
+ expect(target.getAttribute('role')).toBeNull();
96
+ });
97
+
98
+ it('places the selection here when clicking empty background (not a specific tile)', () => {
99
+ const onPlacementClick = jest.fn();
100
+ const { container } = render(
101
+ <ImageDropTarget
102
+ {...baseProps}
103
+ answers={answers}
104
+ selectedResponse={{ id: '9', containerIndex: undefined }}
105
+ onPlacementClick={onPlacementClick}
106
+ />,
107
+ );
108
+
109
+ fireEvent.click(container.firstChild);
110
+
111
+ expect(onPlacementClick).toHaveBeenCalledWith(2);
112
+ });
113
+
114
+ it('forwards selection props to the placed tile, which handles its own click', () => {
115
+ const onSelectClick = jest.fn();
116
+ const { getByTestId } = render(
117
+ <ImageDropTarget {...baseProps} answers={answers} onSelectClick={onSelectClick} />,
118
+ );
119
+
120
+ fireEvent.click(getByTestId('possible-response-7'));
121
+
122
+ expect(onSelectClick).toHaveBeenCalledWith(answers[0]);
123
+ });
124
+ });
125
+
126
+ describe('hover affordance during click-to-select (I3)', () => {
127
+ it('gets the is-over highlight and a pointer cursor on hover when something is selected', () => {
128
+ const { container } = render(
129
+ <ImageDropTarget {...baseProps} selectedResponse={{ id: '9', containerIndex: undefined }} />,
130
+ );
131
+ const target = container.firstChild;
132
+
133
+ expect(target.className).not.toMatch(/\bis-over\b/);
134
+
135
+ fireEvent.mouseEnter(target);
136
+ expect(target.className).toMatch(/\bis-over\b/);
137
+ expect(target.style.cursor).toBe('pointer');
138
+
139
+ fireEvent.mouseLeave(target);
140
+ expect(target.className).not.toMatch(/\bis-over\b/);
141
+ });
142
+
143
+ it('does not show the is-over highlight on hover when nothing is selected', () => {
144
+ const { container } = render(<ImageDropTarget {...baseProps} selectedResponse={null} />);
145
+ const target = container.firstChild;
146
+
147
+ fireEvent.mouseEnter(target);
148
+
149
+ expect(target.className).not.toMatch(/\bis-over\b/);
150
+ expect(target.style.cursor).toBe('');
151
+ });
152
+
153
+ it('does not show the is-over highlight on hover when canDrag is false, even with a selection', () => {
154
+ const { container } = render(
155
+ <ImageDropTarget
156
+ {...baseProps}
157
+ canDrag={false}
158
+ selectedResponse={{ id: '9', containerIndex: undefined }}
159
+ />,
160
+ );
161
+ const target = container.firstChild;
162
+
163
+ fireEvent.mouseEnter(target);
164
+
165
+ expect(target.className).not.toMatch(/\bis-over\b/);
166
+ expect(target.style.cursor).toBe('');
167
+ });
168
+ });
169
+ });
@@ -0,0 +1,127 @@
1
+ import { KeyboardCode } from '@dnd-kit/core';
2
+ import { closestDroppableKeyboardCoordinates } from '../keyboard-coordinates';
3
+
4
+ function rectsToContext(rects) {
5
+ const droppableRects = new Map(Object.entries(rects));
6
+ const droppableContainers = new Map(Object.keys(rects).map((id) => [id, { disabled: false }]));
7
+
8
+ return { droppableRects, droppableContainers };
9
+ }
10
+
11
+ function makeEvent(code, shiftKey = false) {
12
+ return { code, preventDefault: () => {}, shiftKey };
13
+ }
14
+
15
+ // Mirrors dnd-kit's own KeyboardSensor, which always derives collisionRect's top-left
16
+ // from the dragged item's current on-screen position on every keydown — so a test's
17
+ // collisionRect must track currentCoordinates the same way on every simulated press,
18
+ // not stay fixed while currentCoordinates changes across multiple presses.
19
+ function press(rects, currentCoordinates, itemSize, event) {
20
+ const context = {
21
+ ...rectsToContext(rects),
22
+ collisionRect: { left: currentCoordinates.x, top: currentCoordinates.y, ...itemSize },
23
+ };
24
+
25
+ return closestDroppableKeyboardCoordinates(event, { context, currentCoordinates });
26
+ }
27
+
28
+ describe('closestDroppableKeyboardCoordinates', () => {
29
+ describe('arrow keys', () => {
30
+ it("nudges the dragged item by dnd-kit's own default step (25px), unchanged", () => {
31
+ const context = { ...rectsToContext({}), collisionRect: { left: 0, top: 0, width: 100, height: 40 } };
32
+ const currentCoordinates = { x: 10, y: 20 };
33
+
34
+ expect(
35
+ closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Down), { context, currentCoordinates }),
36
+ ).toEqual({ x: 10, y: 45 });
37
+ expect(
38
+ closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Up), { context, currentCoordinates }),
39
+ ).toEqual({ x: 10, y: -5 });
40
+ expect(
41
+ closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Right), { context, currentCoordinates }),
42
+ ).toEqual({ x: 35, y: 20 });
43
+ expect(
44
+ closestDroppableKeyboardCoordinates(makeEvent(KeyboardCode.Left), { context, currentCoordinates }),
45
+ ).toEqual({ x: -15, y: 20 });
46
+ });
47
+ });
48
+
49
+ describe('Tab / Shift+Tab', () => {
50
+ // Two response containers plus the choices pool ("ica-board"), laid out
51
+ // top-to-bottom like they would be over an image.
52
+ const container0Rect = { left: 0, top: 0, width: 200, height: 40, right: 200, bottom: 40 };
53
+ const container1Rect = { left: 0, top: 60, width: 200, height: 40, right: 200, bottom: 100 };
54
+ const poolRect = { left: 0, top: 120, width: 400, height: 300, right: 400, bottom: 420 };
55
+ const baseRects = {
56
+ 'response-container-0': container0Rect,
57
+ 'response-container-1': container1Rect,
58
+ 'ica-board': poolRect,
59
+ };
60
+
61
+ it('jumps to the next droppable in top-to-bottom order, landing at its center-left point', () => {
62
+ const currentCoordinates = { x: container0Rect.left, y: container0Rect.top };
63
+
64
+ const next = press(baseRects, currentCoordinates, { width: 200, height: 40 }, makeEvent('Tab'));
65
+
66
+ expect(next).toEqual({ x: container1Rect.left, y: container1Rect.top + container1Rect.height / 2 });
67
+ });
68
+
69
+ it('cycles backwards with Shift+Tab, wrapping to the pool', () => {
70
+ const currentCoordinates = { x: container0Rect.left, y: container0Rect.top };
71
+
72
+ const next = press(baseRects, currentCoordinates, { width: 200, height: 40 }, makeEvent('Tab', true));
73
+
74
+ expect(next).toEqual({ x: poolRect.left, y: poolRect.top + poolRect.height / 2 });
75
+ });
76
+
77
+ it('keeps advancing on repeated Shift+Tab when a container is much wider than the dragged item', () => {
78
+ // Regression case for the bug already found and fixed in match-list: matching the
79
+ // "current" target by reconstructing a center from the dragged item's own small
80
+ // size (instead of checking which target's rect actually contains it) makes the
81
+ // *next* press re-match a completely different droppable once the item is
82
+ // actually sitting on a wide target, so it looks like the press does nothing.
83
+ // Only two targets here (a wide container and the pool), to isolate this from
84
+ // sort-order effects.
85
+ const wideContainerRect = { left: 0, top: 0, width: 900, height: 40, right: 900, bottom: 40 };
86
+ const rects = { 'response-container-0': wideContainerRect, 'ica-board': poolRect };
87
+ const itemSize = { width: 100, height: 40 };
88
+
89
+ // Start inside the pool, Shift+Tab to the only other target: the wide container.
90
+ const afterFirst = press(rects, { x: poolRect.left, y: poolRect.top }, itemSize, makeEvent('Tab', true));
91
+
92
+ expect(afterFirst).toEqual({ x: 0, y: 20 }); // wide container's center-left point
93
+
94
+ // Now sitting exactly at that dropPosition — Shift+Tab again must advance back
95
+ // to the pool, not re-match some other droppable.
96
+ const afterSecond = press(rects, afterFirst, itemSize, makeEvent('Tab', true));
97
+
98
+ expect(afterSecond).toEqual({ x: poolRect.left, y: poolRect.top + poolRect.height / 2 });
99
+ });
100
+
101
+ it("correctly identifies the pool as the current target when picking up an item positioned near the pool's edge, close to a small neighboring container", () => {
102
+ // Regression case for the OTHER failure mode: matching "current target" purely
103
+ // by nearest-dropPosition (instead of containment first) can misidentify the
104
+ // pool as some small, unrelated nearby container, because a large droppable's
105
+ // dropPosition is anchored at ITS OWN vertical middle — which can be far from an
106
+ // item that's sitting near the droppable's edge, even though the item is clearly
107
+ // still inside it.
108
+ const tallPoolRect = { left: 0, top: 0, width: 400, height: 600, right: 400, bottom: 600 };
109
+ const smallContainerRect = { left: 0, top: -60, width: 100, height: 40, right: 100, bottom: -20 };
110
+ const rects = { 'response-container-0': smallContainerRect, 'ica-board': tallPoolRect };
111
+ const itemSize = { width: 100, height: 40 };
112
+
113
+ // The item sits just inside the pool's top edge (y: 10-50) — nowhere near the
114
+ // pool's own vertical middle (y: 300), but visually still inside it, and closer
115
+ // in raw distance to the small container's anchor point (top: -60, center y:
116
+ // -40) than to the pool's anchor point (center y: 300).
117
+ const currentCoordinates = { x: 0, y: 10 };
118
+
119
+ const next = press(rects, currentCoordinates, itemSize, makeEvent('Tab'));
120
+
121
+ // Tab forward from the pool lands on the only other target: the small
122
+ // container — proving the pool, not the small container, was identified as
123
+ // "current".
124
+ expect(next).toEqual({ x: smallContainerRect.left, y: smallContainerRect.top + smallContainerRect.height / 2 });
125
+ });
126
+ });
127
+ });
@@ -0,0 +1,113 @@
1
+ import React from 'react';
2
+ import { render, fireEvent } from '@testing-library/react';
3
+ import PossibleResponse from '../possible-response';
4
+
5
+ jest.mock('@dnd-kit/core', () => ({
6
+ useDraggable: () => ({
7
+ setNodeRef: jest.fn(),
8
+ attributes: {},
9
+ listeners: {},
10
+ isDragging: false,
11
+ }),
12
+ }));
13
+
14
+ describe('PossibleResponse', () => {
15
+ const baseProps = {
16
+ canDrag: true,
17
+ data: { id: '0', value: 'Choice A' },
18
+ onDragBegin: jest.fn(),
19
+ };
20
+
21
+ it('selects a pool item on click when nothing else is selected', () => {
22
+ const onSelectClick = jest.fn();
23
+ const onPlacementClick = jest.fn();
24
+ const { container } = render(
25
+ <PossibleResponse
26
+ {...baseProps}
27
+ selectedResponse={null}
28
+ onSelectClick={onSelectClick}
29
+ onPlacementClick={onPlacementClick}
30
+ />,
31
+ );
32
+
33
+ fireEvent.click(container.firstChild);
34
+
35
+ expect(onSelectClick).toHaveBeenCalledWith(baseProps.data);
36
+ expect(onPlacementClick).not.toHaveBeenCalled();
37
+ });
38
+
39
+ it('toggles off (calls onSelectClick again) when clicking the already-selected item', () => {
40
+ const onSelectClick = jest.fn();
41
+ const onPlacementClick = jest.fn();
42
+ const { container } = render(
43
+ <PossibleResponse
44
+ {...baseProps}
45
+ selectedResponse={{ id: '0', containerIndex: undefined }}
46
+ onSelectClick={onSelectClick}
47
+ onPlacementClick={onPlacementClick}
48
+ />,
49
+ );
50
+
51
+ fireEvent.click(container.firstChild);
52
+
53
+ expect(onSelectClick).toHaveBeenCalledWith(baseProps.data);
54
+ expect(onPlacementClick).not.toHaveBeenCalled();
55
+ });
56
+
57
+ it('selects a different pool item on click even while something else is selected (pool items are never placement targets)', () => {
58
+ const onSelectClick = jest.fn();
59
+ const onPlacementClick = jest.fn();
60
+ const { container } = render(
61
+ <PossibleResponse
62
+ {...baseProps}
63
+ selectedResponse={{ id: '1', containerIndex: 2 }}
64
+ onSelectClick={onSelectClick}
65
+ onPlacementClick={onPlacementClick}
66
+ />,
67
+ );
68
+
69
+ fireEvent.click(container.firstChild);
70
+
71
+ expect(onSelectClick).toHaveBeenCalledWith(baseProps.data);
72
+ expect(onPlacementClick).not.toHaveBeenCalled();
73
+ });
74
+
75
+ it('places the current selection into this tile\'s container when clicking an already-placed tile that is not the selection', () => {
76
+ const onSelectClick = jest.fn();
77
+ const onPlacementClick = jest.fn();
78
+ const placedData = { id: '5', value: 'Choice B', containerIndex: 3 };
79
+ const { container } = render(
80
+ <PossibleResponse
81
+ {...baseProps}
82
+ data={placedData}
83
+ selectedResponse={{ id: '1', containerIndex: undefined }}
84
+ onSelectClick={onSelectClick}
85
+ onPlacementClick={onPlacementClick}
86
+ />,
87
+ );
88
+
89
+ fireEvent.click(container.firstChild);
90
+
91
+ expect(onPlacementClick).toHaveBeenCalledWith(3);
92
+ expect(onSelectClick).not.toHaveBeenCalled();
93
+ });
94
+
95
+ it('does nothing on click when disabled (canDrag=false)', () => {
96
+ const onSelectClick = jest.fn();
97
+ const onPlacementClick = jest.fn();
98
+ const { container } = render(
99
+ <PossibleResponse
100
+ {...baseProps}
101
+ canDrag={false}
102
+ selectedResponse={null}
103
+ onSelectClick={onSelectClick}
104
+ onPlacementClick={onPlacementClick}
105
+ />,
106
+ );
107
+
108
+ fireEvent.click(container.firstChild);
109
+
110
+ expect(onSelectClick).not.toHaveBeenCalled();
111
+ expect(onPlacementClick).not.toHaveBeenCalled();
112
+ });
113
+ });
@@ -0,0 +1,63 @@
1
+ import React from 'react';
2
+ import { render, fireEvent } from '@testing-library/react';
3
+ import PossibleResponses from '../possible-responses';
4
+
5
+ jest.mock('@pie-lib/drag', () => ({
6
+ ICADroppablePlaceholder: ({ children }) => <div data-testid="ica-board">{children}</div>,
7
+ }));
8
+
9
+ jest.mock('../possible-response', () => (props) => (
10
+ <div data-testid={`possible-response-${props.data.id}`} onClick={(e) => { e.stopPropagation(); props.onSelectClick?.(props.data); }}>
11
+ {props.data.value}
12
+ </div>
13
+ ));
14
+
15
+ describe('PossibleResponses', () => {
16
+ const baseProps = {
17
+ canDrag: true,
18
+ data: [{ id: '0', value: 'Choice A' }],
19
+ onDragBegin: jest.fn(),
20
+ };
21
+
22
+ it('places the selection into the pool when clicking the pool background', () => {
23
+ const onPlacementClick = jest.fn();
24
+ const { container } = render(
25
+ <PossibleResponses
26
+ {...baseProps}
27
+ selectedResponse={{ id: '9', containerIndex: 1 }}
28
+ onPlacementClick={onPlacementClick}
29
+ />,
30
+ );
31
+
32
+ fireEvent.click(container.firstChild);
33
+
34
+ expect(onPlacementClick).toHaveBeenCalledWith(undefined);
35
+ });
36
+
37
+ it('does nothing when clicking the pool background with nothing selected', () => {
38
+ const onPlacementClick = jest.fn();
39
+ const { container } = render(<PossibleResponses {...baseProps} onPlacementClick={onPlacementClick} />);
40
+
41
+ fireEvent.click(container.firstChild);
42
+
43
+ expect(onPlacementClick).not.toHaveBeenCalled();
44
+ });
45
+
46
+ it('clicking a specific pool item selects it, not the pool background handler', () => {
47
+ const onPlacementClick = jest.fn();
48
+ const onSelectClick = jest.fn();
49
+ const { getByTestId } = render(
50
+ <PossibleResponses
51
+ {...baseProps}
52
+ selectedResponse={{ id: '9', containerIndex: 1 }}
53
+ onSelectClick={onSelectClick}
54
+ onPlacementClick={onPlacementClick}
55
+ />,
56
+ );
57
+
58
+ fireEvent.click(getByTestId('possible-response-0'));
59
+
60
+ expect(onSelectClick).toHaveBeenCalledWith(baseProps.data[0]);
61
+ expect(onPlacementClick).not.toHaveBeenCalled();
62
+ });
63
+ });