@pie-lib/graphing-solution-set 3.1.1-next.0 → 3.2.0-next.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.
- package/lib/axis/arrow.js +0 -3
- package/lib/axis/arrow.js.map +1 -1
- package/lib/axis/axes.js +0 -16
- package/lib/axis/axes.js.map +1 -1
- package/lib/axis/index.js +0 -7
- package/lib/axis/index.js.map +1 -1
- package/lib/bg.js +0 -6
- package/lib/bg.js.map +1 -1
- package/lib/container/actions.js +0 -1
- package/lib/container/actions.js.map +1 -1
- package/lib/container/index.js +4 -10
- package/lib/container/index.js.map +1 -1
- package/lib/container/marks.js +0 -2
- package/lib/container/marks.js.map +1 -1
- package/lib/container/middleware.js +0 -1
- package/lib/container/middleware.js.map +1 -1
- package/lib/container/reducer.js +0 -1
- package/lib/container/reducer.js.map +1 -1
- package/lib/coordinates-label.js +0 -11
- package/lib/coordinates-label.js.map +1 -1
- package/lib/graph-with-controls.js +3 -22
- package/lib/graph-with-controls.js.map +1 -1
- package/lib/graph.js +7 -26
- package/lib/graph.js.map +1 -1
- package/lib/grid-setup.js +0 -11
- package/lib/grid-setup.js.map +1 -1
- package/lib/grid.js +0 -22
- package/lib/grid.js.map +1 -1
- package/lib/index.js +0 -7
- package/lib/index.js.map +1 -1
- package/lib/labels.js +0 -13
- package/lib/labels.js.map +1 -1
- package/lib/mark-label.js +0 -16
- package/lib/mark-label.js.map +1 -1
- package/lib/toggle-bar.js +0 -17
- package/lib/toggle-bar.js.map +1 -1
- package/lib/tool-menu.js +0 -3
- package/lib/tool-menu.js.map +1 -1
- package/lib/tools/index.js +0 -1
- package/lib/tools/index.js.map +1 -1
- package/lib/tools/line/component.js +0 -13
- package/lib/tools/line/component.js.map +1 -1
- package/lib/tools/line/index.js +0 -1
- package/lib/tools/line/index.js.map +1 -1
- package/lib/tools/polygon/component.js +5 -25
- package/lib/tools/polygon/component.js.map +1 -1
- package/lib/tools/polygon/index.js +0 -12
- package/lib/tools/polygon/index.js.map +1 -1
- package/lib/tools/polygon/line.js +0 -16
- package/lib/tools/polygon/line.js.map +1 -1
- package/lib/tools/polygon/polygon.js +0 -19
- package/lib/tools/polygon/polygon.js.map +1 -1
- package/lib/tools/shared/arrow-head.js +0 -3
- package/lib/tools/shared/arrow-head.js.map +1 -1
- package/lib/tools/shared/line/index.js +7 -22
- package/lib/tools/shared/line/index.js.map +1 -1
- package/lib/tools/shared/line/line-path.js +0 -16
- package/lib/tools/shared/line/line-path.js.map +1 -1
- package/lib/tools/shared/line/with-root-edge.js +0 -11
- package/lib/tools/shared/line/with-root-edge.js.map +1 -1
- package/lib/tools/shared/point/arrow-point.js +2 -5
- package/lib/tools/shared/point/arrow-point.js.map +1 -1
- package/lib/tools/shared/point/arrow.js +0 -3
- package/lib/tools/shared/point/arrow.js.map +1 -1
- package/lib/tools/shared/point/base-point.js +0 -11
- package/lib/tools/shared/point/base-point.js.map +1 -1
- package/lib/tools/shared/point/index.js +0 -16
- package/lib/tools/shared/point/index.js.map +1 -1
- package/lib/tools/shared/styles.js +0 -1
- package/lib/tools/shared/styles.js.map +1 -1
- package/lib/tools/shared/types.js +0 -1
- package/lib/tools/shared/types.js.map +1 -1
- package/lib/undo-redo.js +0 -2
- package/lib/undo-redo.js.map +1 -1
- package/lib/use-debounce.js +0 -2
- package/lib/use-debounce.js.map +1 -1
- package/lib/utils.js +8 -26
- package/lib/utils.js.map +1 -1
- package/package.json +14 -11
- package/src/__tests__/bg.test.jsx +250 -0
- package/src/__tests__/coordinates-label.test.jsx +243 -0
- package/src/__tests__/graph-with-controls.test.jsx +9 -10
- package/src/__tests__/graph.test.jsx +0 -2
- package/src/__tests__/grid-setup.test.jsx +645 -0
- package/src/__tests__/mark-label.test.jsx +1 -1
- package/src/__tests__/tool-menu.test.jsx +422 -2
- package/src/__tests__/use-debounce.test.js +1 -1
- package/src/__tests__/utils.test.js +15 -61
- package/src/axis/__tests__/axes.test.jsx +1 -1
- package/src/axis/axes.jsx +7 -21
- package/src/axis/index.js +1 -0
- package/src/bg.jsx +1 -1
- package/src/container/__tests__/actions.test.js +105 -0
- package/src/container/__tests__/index.test.jsx +227 -0
- package/src/container/__tests__/marks.test.js +172 -0
- package/src/container/__tests__/middleware.test.js +235 -0
- package/src/container/__tests__/reducer.test.js +324 -0
- package/src/container/index.jsx +3 -4
- package/src/coordinates-label.jsx +1 -7
- package/src/graph-with-controls.jsx +7 -25
- package/src/graph.jsx +3 -4
- package/src/grid-setup.jsx +1 -1
- package/src/mark-label.jsx +2 -2
- package/src/toggle-bar.jsx +8 -1
- package/src/tool-menu.jsx +1 -1
- package/src/tools/line/__tests__/component.test.jsx +1 -0
- package/src/tools/line/component.jsx +2 -2
- package/src/tools/polygon/__tests__/component.test.jsx +417 -5
- package/src/tools/polygon/__tests__/polygon.test.jsx +1 -1
- package/src/tools/polygon/component.jsx +4 -14
- package/src/tools/polygon/line.jsx +1 -1
- package/src/tools/shared/line/__tests__/index.test.jsx +460 -17
- package/src/tools/shared/line/__tests__/line-path.test.jsx +7 -4
- package/src/tools/shared/line/__tests__/with-root-edge.test.jsx +439 -14
- package/src/tools/shared/line/index.jsx +4 -6
- package/src/tools/shared/line/line-path.jsx +2 -8
- package/src/tools/shared/point/__tests__/arrow.test.jsx +469 -0
- package/src/tools/shared/point/arrow-point.jsx +2 -2
- package/src/tools/shared/point/base-point.jsx +1 -1
- package/src/tools/shared/point/index.jsx +1 -1
- package/src/undo-redo.jsx +1 -3
- package/src/use-debounce.js +1 -1
- package/src/utils.js +1 -5
- package/NEXT.CHANGELOG.json +0 -16
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { addMark, changeMarks } from '../actions';
|
|
2
|
+
|
|
3
|
+
describe('container actions', () => {
|
|
4
|
+
describe('addMark', () => {
|
|
5
|
+
it('creates ADD_MARK action', () => {
|
|
6
|
+
const action = addMark();
|
|
7
|
+
expect(action).toEqual({
|
|
8
|
+
type: 'ADD_MARK',
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('creates action without parameters', () => {
|
|
13
|
+
const action = addMark();
|
|
14
|
+
expect(action.type).toBe('ADD_MARK');
|
|
15
|
+
expect(Object.keys(action)).toEqual(['type']);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('changeMarks', () => {
|
|
20
|
+
it('creates CHANGE_MARKS action with marks', () => {
|
|
21
|
+
const marks = [{ id: 1, type: 'point' }];
|
|
22
|
+
const action = changeMarks(marks);
|
|
23
|
+
|
|
24
|
+
expect(action).toEqual({
|
|
25
|
+
type: 'CHANGE_MARKS',
|
|
26
|
+
marks,
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('creates action with empty marks array', () => {
|
|
31
|
+
const marks = [];
|
|
32
|
+
const action = changeMarks(marks);
|
|
33
|
+
|
|
34
|
+
expect(action).toEqual({
|
|
35
|
+
type: 'CHANGE_MARKS',
|
|
36
|
+
marks: [],
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('creates action with multiple marks', () => {
|
|
41
|
+
const marks = [
|
|
42
|
+
{ id: 1, type: 'point', x: 1, y: 2 },
|
|
43
|
+
{ id: 2, type: 'line', from: { x: 0, y: 0 }, to: { x: 5, y: 5 } },
|
|
44
|
+
];
|
|
45
|
+
const action = changeMarks(marks);
|
|
46
|
+
|
|
47
|
+
expect(action.marks).toEqual(marks);
|
|
48
|
+
expect(action.marks.length).toBe(2);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('creates action with null marks', () => {
|
|
52
|
+
const marks = null;
|
|
53
|
+
const action = changeMarks(marks);
|
|
54
|
+
|
|
55
|
+
expect(action).toEqual({
|
|
56
|
+
type: 'CHANGE_MARKS',
|
|
57
|
+
marks: null,
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('creates action with undefined marks', () => {
|
|
62
|
+
const marks = undefined;
|
|
63
|
+
const action = changeMarks(marks);
|
|
64
|
+
|
|
65
|
+
expect(action).toEqual({
|
|
66
|
+
type: 'CHANGE_MARKS',
|
|
67
|
+
marks: undefined,
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('preserves mark properties', () => {
|
|
72
|
+
const marks = [
|
|
73
|
+
{
|
|
74
|
+
id: 1,
|
|
75
|
+
type: 'point',
|
|
76
|
+
x: 10,
|
|
77
|
+
y: 20,
|
|
78
|
+
label: 'A',
|
|
79
|
+
correctness: { value: 'correct' },
|
|
80
|
+
},
|
|
81
|
+
];
|
|
82
|
+
const action = changeMarks(marks);
|
|
83
|
+
|
|
84
|
+
expect(action.marks[0]).toEqual(marks[0]);
|
|
85
|
+
expect(action.marks[0].correctness).toBeDefined();
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('action types', () => {
|
|
90
|
+
it('has consistent action type strings', () => {
|
|
91
|
+
const addMarkAction = addMark();
|
|
92
|
+
const changeMarksAction = changeMarks([]);
|
|
93
|
+
|
|
94
|
+
expect(typeof addMarkAction.type).toBe('string');
|
|
95
|
+
expect(typeof changeMarksAction.type).toBe('string');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('has unique action types', () => {
|
|
99
|
+
const addMarkAction = addMark();
|
|
100
|
+
const changeMarksAction = changeMarks([]);
|
|
101
|
+
|
|
102
|
+
expect(addMarkAction.type).not.toBe(changeMarksAction.type);
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
});
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@pie-lib/test-utils';
|
|
3
|
+
import Root, { GraphContainer } from '../index';
|
|
4
|
+
|
|
5
|
+
jest.mock('../../graph-with-controls', () => {
|
|
6
|
+
return function GraphWithControls({ marks, onChangeMarks, onUndo, onRedo, onReset, disabled }) {
|
|
7
|
+
return (
|
|
8
|
+
<div data-testid="graph-with-controls">
|
|
9
|
+
<div data-testid="marks-count">{marks?.length || 0}</div>
|
|
10
|
+
{disabled && <div data-testid="disabled-indicator">Disabled</div>}
|
|
11
|
+
<button data-testid="change-marks" onClick={() => onChangeMarks && onChangeMarks([{ id: 1 }])}>
|
|
12
|
+
Change
|
|
13
|
+
</button>
|
|
14
|
+
<button data-testid="undo" onClick={onUndo}>
|
|
15
|
+
Undo
|
|
16
|
+
</button>
|
|
17
|
+
<button data-testid="redo" onClick={onRedo}>
|
|
18
|
+
Redo
|
|
19
|
+
</button>
|
|
20
|
+
<button data-testid="reset" onClick={onReset}>
|
|
21
|
+
Reset
|
|
22
|
+
</button>
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
};
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
jest.mock('@pie-lib/drag', () => ({
|
|
29
|
+
DragProvider: ({ children }) => <div data-testid="drag-provider">{children}</div>,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
describe('Container Root Component', () => {
|
|
33
|
+
const defaultProps = {
|
|
34
|
+
marks: [],
|
|
35
|
+
onChangeMarks: jest.fn(),
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const renderComponent = (props = {}) => {
|
|
39
|
+
return render(<Root {...defaultProps} {...props} />);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
describe('rendering', () => {
|
|
43
|
+
it('renders without crashing', () => {
|
|
44
|
+
const { container } = renderComponent();
|
|
45
|
+
expect(container).toBeInTheDocument();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('renders GraphWithControls', () => {
|
|
49
|
+
const { getByTestId } = renderComponent();
|
|
50
|
+
expect(getByTestId('graph-with-controls')).toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('renders with Provider when no correctness set', () => {
|
|
54
|
+
const { container } = renderComponent({ marks: [{ id: 1, type: 'point' }] });
|
|
55
|
+
expect(container.querySelector('[data-testid="graph-with-controls"]')).toBeInTheDocument();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('renders directly when correctness is set', () => {
|
|
59
|
+
const marks = [{ id: 1, type: 'point', correctness: { value: 'correct' } }];
|
|
60
|
+
const { getByTestId } = renderComponent({ marks });
|
|
61
|
+
|
|
62
|
+
expect(getByTestId('graph-with-controls')).toBeInTheDocument();
|
|
63
|
+
expect(getByTestId('disabled-indicator')).toBeInTheDocument();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('passes marks to GraphWithControls', () => {
|
|
67
|
+
const marks = [{ id: 1 }, { id: 2 }];
|
|
68
|
+
const { getByTestId } = renderComponent({ marks });
|
|
69
|
+
expect(getByTestId('marks-count')).toHaveTextContent('2');
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('componentDidUpdate', () => {
|
|
74
|
+
it('updates store when marks prop changes', () => {
|
|
75
|
+
const onChangeMarks = jest.fn();
|
|
76
|
+
const { rerender } = render(<Root marks={[]} onChangeMarks={onChangeMarks} />);
|
|
77
|
+
|
|
78
|
+
const newMarks = [{ id: 1, type: 'point' }];
|
|
79
|
+
rerender(<Root marks={newMarks} onChangeMarks={onChangeMarks} />);
|
|
80
|
+
|
|
81
|
+
expect(onChangeMarks).not.toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('does not update when marks are equal', () => {
|
|
85
|
+
const marks = [{ id: 1, type: 'point' }];
|
|
86
|
+
const onChangeMarks = jest.fn();
|
|
87
|
+
const { rerender } = render(<Root marks={marks} onChangeMarks={onChangeMarks} />);
|
|
88
|
+
|
|
89
|
+
onChangeMarks.mockClear();
|
|
90
|
+
|
|
91
|
+
rerender(<Root marks={marks} onChangeMarks={onChangeMarks} />);
|
|
92
|
+
|
|
93
|
+
expect(onChangeMarks).not.toHaveBeenCalled();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('handles marks changing from empty to non-empty', () => {
|
|
97
|
+
const onChangeMarks = jest.fn();
|
|
98
|
+
const { rerender } = render(<Root marks={[]} onChangeMarks={onChangeMarks} />);
|
|
99
|
+
|
|
100
|
+
const newMarks = [{ id: 1 }];
|
|
101
|
+
|
|
102
|
+
expect(() => {
|
|
103
|
+
rerender(<Root marks={newMarks} onChangeMarks={onChangeMarks} />);
|
|
104
|
+
}).not.toThrow();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('handles marks changing from non-empty to empty', () => {
|
|
108
|
+
const onChangeMarks = jest.fn();
|
|
109
|
+
const { rerender } = render(<Root marks={[{ id: 1 }]} onChangeMarks={onChangeMarks} />);
|
|
110
|
+
|
|
111
|
+
expect(() => {
|
|
112
|
+
rerender(<Root marks={[]} onChangeMarks={onChangeMarks} />);
|
|
113
|
+
}).not.toThrow();
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('correctness handling', () => {
|
|
118
|
+
it('disables when any mark has correctness', () => {
|
|
119
|
+
const marks = [
|
|
120
|
+
{ id: 1, type: 'point' },
|
|
121
|
+
{ id: 2, type: 'point', correctness: { value: 'correct' } },
|
|
122
|
+
];
|
|
123
|
+
const { getByTestId } = renderComponent({ marks });
|
|
124
|
+
|
|
125
|
+
expect(getByTestId('disabled-indicator')).toBeInTheDocument();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('renders without Provider when correctness is set', () => {
|
|
129
|
+
const marks = [{ id: 1, correctness: { value: 'correct' } }];
|
|
130
|
+
const { container } = renderComponent({ marks });
|
|
131
|
+
|
|
132
|
+
expect(container.querySelector('[data-testid="graph-with-controls"]')).toBeInTheDocument();
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('does not disable when no correctness is set', () => {
|
|
136
|
+
const marks = [
|
|
137
|
+
{ id: 1, type: 'point' },
|
|
138
|
+
{ id: 2, type: 'line' },
|
|
139
|
+
];
|
|
140
|
+
const { queryByTestId } = renderComponent({ marks });
|
|
141
|
+
|
|
142
|
+
expect(queryByTestId('disabled-indicator')).not.toBeInTheDocument();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('handles correctness with different values', () => {
|
|
146
|
+
const marks = [{ id: 1, correctness: { value: 'incorrect' } }];
|
|
147
|
+
const { getByTestId } = renderComponent({ marks });
|
|
148
|
+
|
|
149
|
+
expect(getByTestId('disabled-indicator')).toBeInTheDocument();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('handles empty correctness object', () => {
|
|
153
|
+
const marks = [{ id: 1, correctness: {} }];
|
|
154
|
+
const { getByTestId } = renderComponent({ marks });
|
|
155
|
+
|
|
156
|
+
expect(getByTestId('disabled-indicator')).toBeInTheDocument();
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
describe('props forwarding', () => {
|
|
161
|
+
it('forwards additional props to GraphWithControls', () => {
|
|
162
|
+
const { container } = renderComponent({
|
|
163
|
+
className: 'custom-class',
|
|
164
|
+
size: { width: 500, height: 500 },
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
expect(container.querySelector('[data-testid="graph-with-controls"]')).toBeInTheDocument();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('does not forward onChangeMarks and marks to GraphWithControls', () => {
|
|
171
|
+
const { getByTestId } = renderComponent({
|
|
172
|
+
marks: [{ id: 1 }],
|
|
173
|
+
onChangeMarks: jest.fn(),
|
|
174
|
+
otherProp: 'test',
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
expect(getByTestId('graph-with-controls')).toBeInTheDocument();
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('edge cases', () => {
|
|
182
|
+
it('handles empty marks array', () => {
|
|
183
|
+
const { container } = renderComponent({ marks: [] });
|
|
184
|
+
expect(container).toBeInTheDocument();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('handles marks with complex structure', () => {
|
|
188
|
+
const marks = [
|
|
189
|
+
{
|
|
190
|
+
id: 1,
|
|
191
|
+
type: 'line',
|
|
192
|
+
from: { x: 0, y: 0 },
|
|
193
|
+
to: { x: 10, y: 10 },
|
|
194
|
+
label: 'Line A',
|
|
195
|
+
correctness: { value: 'correct', label: 'Correct!' },
|
|
196
|
+
},
|
|
197
|
+
];
|
|
198
|
+
const { getByTestId } = renderComponent({ marks });
|
|
199
|
+
expect(getByTestId('graph-with-controls')).toBeInTheDocument();
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('handles switching between correctness states', () => {
|
|
203
|
+
const { rerender } = render(<Root marks={[{ id: 1 }]} onChangeMarks={jest.fn()} />);
|
|
204
|
+
|
|
205
|
+
rerender(<Root marks={[{ id: 1, correctness: { value: 'correct' } }]} onChangeMarks={jest.fn()} />);
|
|
206
|
+
|
|
207
|
+
rerender(<Root marks={[{ id: 1 }]} onChangeMarks={jest.fn()} />);
|
|
208
|
+
|
|
209
|
+
expect(document.querySelector('[data-testid="graph-with-controls"]')).toBeInTheDocument();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe('GraphContainer (connected component)', () => {
|
|
215
|
+
it('is a connected component', () => {
|
|
216
|
+
expect(GraphContainer).toBeDefined();
|
|
217
|
+
expect(GraphContainer.displayName).toContain('Connect');
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it('maps state to props', () => {
|
|
221
|
+
expect(GraphContainer).toBeDefined();
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('maps dispatch to props', () => {
|
|
225
|
+
expect(GraphContainer).toBeDefined();
|
|
226
|
+
});
|
|
227
|
+
});
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import marks from '../marks';
|
|
2
|
+
|
|
3
|
+
describe('marks reducer', () => {
|
|
4
|
+
describe('initial state', () => {
|
|
5
|
+
it('returns empty array as initial state', () => {
|
|
6
|
+
const state = marks(undefined, { type: 'INIT' });
|
|
7
|
+
expect(state).toEqual([]);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('handles no state parameter', () => {
|
|
11
|
+
const state = marks(undefined, { type: 'UNKNOWN_ACTION' });
|
|
12
|
+
expect(Array.isArray(state)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('CHANGE_MARKS action', () => {
|
|
17
|
+
it('updates state with new marks', () => {
|
|
18
|
+
const initialState = [];
|
|
19
|
+
const newMarks = [{ id: 1, type: 'point' }];
|
|
20
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
21
|
+
|
|
22
|
+
const state = marks(initialState, action);
|
|
23
|
+
expect(state).toEqual(newMarks);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('replaces existing marks', () => {
|
|
27
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
28
|
+
const newMarks = [{ id: 2, type: 'line' }];
|
|
29
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
30
|
+
|
|
31
|
+
const state = marks(initialState, action);
|
|
32
|
+
expect(state).toEqual(newMarks);
|
|
33
|
+
expect(state.length).toBe(1);
|
|
34
|
+
expect(state[0].id).toBe(2);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('handles empty marks array', () => {
|
|
38
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
39
|
+
const action = { type: 'CHANGE_MARKS', marks: [] };
|
|
40
|
+
|
|
41
|
+
const state = marks(initialState, action);
|
|
42
|
+
expect(state).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('handles multiple marks', () => {
|
|
46
|
+
const initialState = [];
|
|
47
|
+
const newMarks = [
|
|
48
|
+
{ id: 1, type: 'point', x: 1, y: 2 },
|
|
49
|
+
{ id: 2, type: 'line', from: { x: 0, y: 0 }, to: { x: 5, y: 5 } },
|
|
50
|
+
{ id: 3, type: 'circle', center: { x: 3, y: 3 }, radius: 2 },
|
|
51
|
+
];
|
|
52
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
53
|
+
|
|
54
|
+
const state = marks(initialState, action);
|
|
55
|
+
expect(state).toEqual(newMarks);
|
|
56
|
+
expect(state.length).toBe(3);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('throws error when marks is not an array', () => {
|
|
60
|
+
const initialState = [];
|
|
61
|
+
const action = { type: 'CHANGE_MARKS', marks: 'not an array' };
|
|
62
|
+
|
|
63
|
+
expect(() => marks(initialState, action)).toThrow('marks must be an array');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('throws error when marks is null', () => {
|
|
67
|
+
const initialState = [];
|
|
68
|
+
const action = { type: 'CHANGE_MARKS', marks: null };
|
|
69
|
+
|
|
70
|
+
expect(() => marks(initialState, action)).toThrow('marks must be an array');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('throws error when marks is undefined', () => {
|
|
74
|
+
const initialState = [];
|
|
75
|
+
const action = { type: 'CHANGE_MARKS', marks: undefined };
|
|
76
|
+
|
|
77
|
+
expect(() => marks(initialState, action)).toThrow('marks must be an array');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('throws error when marks is an object', () => {
|
|
81
|
+
const initialState = [];
|
|
82
|
+
const action = { type: 'CHANGE_MARKS', marks: { id: 1 } };
|
|
83
|
+
|
|
84
|
+
expect(() => marks(initialState, action)).toThrow('marks must be an array');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('throws error when marks is a number', () => {
|
|
88
|
+
const initialState = [];
|
|
89
|
+
const action = { type: 'CHANGE_MARKS', marks: 123 };
|
|
90
|
+
|
|
91
|
+
expect(() => marks(initialState, action)).toThrow('marks must be an array');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('preserves mark properties', () => {
|
|
95
|
+
const initialState = [];
|
|
96
|
+
const newMarks = [
|
|
97
|
+
{
|
|
98
|
+
id: 1,
|
|
99
|
+
type: 'point',
|
|
100
|
+
x: 10,
|
|
101
|
+
y: 20,
|
|
102
|
+
label: 'A',
|
|
103
|
+
correctness: { value: 'correct' },
|
|
104
|
+
editable: true,
|
|
105
|
+
interactive: true,
|
|
106
|
+
},
|
|
107
|
+
];
|
|
108
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
109
|
+
|
|
110
|
+
const state = marks(initialState, action);
|
|
111
|
+
expect(state[0]).toEqual(newMarks[0]);
|
|
112
|
+
expect(state[0].correctness).toBeDefined();
|
|
113
|
+
expect(state[0].editable).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('unknown actions', () => {
|
|
118
|
+
it('returns current state for unknown action', () => {
|
|
119
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
120
|
+
const action = { type: 'UNKNOWN_ACTION' };
|
|
121
|
+
|
|
122
|
+
const state = marks(initialState, action);
|
|
123
|
+
expect(state).toBe(initialState);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('returns current state for ADD_MARK action', () => {
|
|
127
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
128
|
+
const action = { type: 'ADD_MARK' };
|
|
129
|
+
|
|
130
|
+
const state = marks(initialState, action);
|
|
131
|
+
expect(state).toBe(initialState);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it('does not modify state for unknown actions', () => {
|
|
135
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
136
|
+
const action = { type: 'SOME_OTHER_ACTION', data: 'test' };
|
|
137
|
+
|
|
138
|
+
const state = marks(initialState, action);
|
|
139
|
+
expect(state).toEqual(initialState);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('immutability', () => {
|
|
144
|
+
it('does not mutate original state', () => {
|
|
145
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
146
|
+
const originalState = [...initialState];
|
|
147
|
+
const newMarks = [{ id: 2, type: 'line' }];
|
|
148
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
149
|
+
|
|
150
|
+
marks(initialState, action);
|
|
151
|
+
expect(initialState).toEqual(originalState);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('returns new array reference for CHANGE_MARKS', () => {
|
|
155
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
156
|
+
const newMarks = [{ id: 1, type: 'point' }];
|
|
157
|
+
const action = { type: 'CHANGE_MARKS', marks: newMarks };
|
|
158
|
+
|
|
159
|
+
const state = marks(initialState, action);
|
|
160
|
+
expect(state).not.toBe(initialState);
|
|
161
|
+
expect(state).toBe(newMarks);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('returns same reference for unknown actions', () => {
|
|
165
|
+
const initialState = [{ id: 1, type: 'point' }];
|
|
166
|
+
const action = { type: 'UNKNOWN_ACTION' };
|
|
167
|
+
|
|
168
|
+
const state = marks(initialState, action);
|
|
169
|
+
expect(state).toBe(initialState);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|