@pie-lib/rubric 2.1.0-beta.8 → 2.1.0-beta.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pie-lib/rubric",
3
- "version": "2.1.0-beta.8",
3
+ "version": "2.1.0-beta.9",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -13,7 +13,7 @@
13
13
  "@hello-pangea/dnd": "^18.0.1",
14
14
  "@mui/icons-material": "^7.3.4",
15
15
  "@mui/material": "^7.3.4",
16
- "@pie-lib/editable-html-tip-tap": "^2.2.0-beta.8",
16
+ "@pie-lib/editable-html-tip-tap": "^2.2.0-beta.9",
17
17
  "debug": "^4.1.1",
18
18
  "lodash-es": "^4.17.23",
19
19
  "prop-types": "^15.7.2",
@@ -1,373 +0,0 @@
1
- import { fireEvent, render, screen } from '@testing-library/react';
2
- import React from 'react';
3
- import { RawAuthoring } from '../authoring';
4
- import { takeRight } from 'lodash-es';
5
- import { createTheme, ThemeProvider } from '@mui/material/styles';
6
-
7
- // Mock dependencies
8
- jest.mock('@pie-lib/editable-html-tip-tap', () => {
9
- return function EditableHtml(props) {
10
- return <div data-testid="editable-html" data-markup={props.markup} />;
11
- };
12
- });
13
-
14
- jest.mock('@pie-lib/config-ui', () => ({
15
- FeedbackConfig: ({ feedback }) => <div data-testid="feedback-config">{JSON.stringify(feedback)}</div>,
16
- InputContainer: ({ children, label }) => (
17
- <div data-testid="input-container" data-label={label}>
18
- {children}
19
- </div>
20
- ),
21
- }));
22
-
23
- jest.mock('@hello-pangea/dnd', () => ({
24
- DragDropContext: ({ children }) => <div data-testid="drag-drop-context">{children}</div>,
25
- Droppable: ({ children }) => children({ droppableProps: {}, innerRef: () => {} }, {}),
26
- Draggable: ({ children, index }) =>
27
- children(
28
- {
29
- innerRef: () => {},
30
- draggableProps: { 'data-draggable-index': index },
31
- dragHandleProps: {},
32
- },
33
- {},
34
- ),
35
- }));
36
-
37
- describe('Rubric', () => {
38
- const points = ['nothing right', 'a teeny bit right', 'mostly right', 'bingo'];
39
- const sampleAnswers = [null, 'just right', 'not left', null];
40
- const theme = createTheme();
41
-
42
- const renderComponent = (value = {}, props = {}) => {
43
- const defaultProps = {
44
- classes: {},
45
- onChange: jest.fn(),
46
- className: 'className',
47
- value: {
48
- excludeZero: false,
49
- points,
50
- sampleAnswers,
51
- ...value,
52
- },
53
- ...props,
54
- };
55
-
56
- return {
57
- ...render(
58
- <ThemeProvider theme={theme}>
59
- <RawAuthoring {...defaultProps} />
60
- </ThemeProvider>,
61
- ),
62
- onChange: defaultProps.onChange,
63
- props: defaultProps,
64
- };
65
- };
66
-
67
- describe('render', () => {
68
- it('renders rubric title and main structure', () => {
69
- renderComponent();
70
- expect(screen.getByText('Rubric')).toBeInTheDocument();
71
- expect(screen.getByLabelText('Max Points')).toBeInTheDocument();
72
- expect(screen.getByLabelText('Exclude zeros')).toBeInTheDocument();
73
- });
74
-
75
- it('renders all point configurations', () => {
76
- const { container } = renderComponent();
77
- // Verify DragDropContext is rendered
78
- expect(container.querySelector('[data-testid="drag-drop-context"]')).toBeInTheDocument();
79
-
80
- // Check that point labels are rendered (4 points = "3 pts", "2 pts", "1 pt", "0 pt")
81
- // Note: The PointConfig component uses singular "pt" for 0 and 1, plural "pts" for 2+
82
- expect(screen.getByText('3 pts')).toBeInTheDocument();
83
- expect(screen.getByText('2 pts')).toBeInTheDocument();
84
- expect(screen.getByText('1 pt')).toBeInTheDocument();
85
- expect(screen.getByText('0 pt')).toBeInTheDocument();
86
- });
87
-
88
- describe('draggable', () => {
89
- it('renders 3 draggable items when excludeZero is true', () => {
90
- // When excludeZero is true, the last point (0 pts) should not be rendered
91
- expect(screen.queryByText('0 pt')).not.toBeInTheDocument();
92
- });
93
-
94
- it('renders 4 draggable items when excludeZero is false', () => {
95
- const { container } = renderComponent({ excludeZero: false });
96
-
97
- // When excludeZero is false, all points including 0 should be rendered
98
- const draggableItems = container.querySelectorAll('[data-draggable-index]');
99
- expect(draggableItems.length).toEqual(4);
100
- expect(screen.getByText('0 pt')).toBeInTheDocument();
101
-
102
- // Verify all point labels are rendered
103
- expect(screen.getByText('3 pts')).toBeInTheDocument();
104
- expect(screen.getByText('2 pts')).toBeInTheDocument();
105
- expect(screen.getByText('1 pt')).toBeInTheDocument();
106
- expect(screen.getByText('0 pt')).toBeInTheDocument();
107
- });
108
- });
109
- });
110
-
111
- describe('logic', () => {
112
- describe('changeMaxPoints', () => {
113
- const testChangeMaxPoints = (maxPoints, excludeZero, expectedPoints, expectedSampleAnswers) => {
114
- it(`maxPoints=${maxPoints}, excludeZero=${excludeZero} calls onChange correctly`, () => {
115
- const { onChange, container } = renderComponent({ excludeZero });
116
-
117
- // Get the component instance through the container
118
- // We need to call the method directly since we're testing internal behavior
119
- const instance = container.querySelector('[class*="MuiBox-root"]')?._owner;
120
-
121
- // Since we can't easily access instance methods in RTL, we'll test the behavior
122
- // by verifying the onChange prop receives the correct values
123
- // For now, we'll directly test the logic
124
- const component = new RawAuthoring({
125
- value: { excludeZero, points, sampleAnswers },
126
- onChange,
127
- classes: {},
128
- className: 'className',
129
- });
130
-
131
- component.changeMaxPoints(maxPoints);
132
-
133
- expect(onChange).toHaveBeenCalledWith({
134
- excludeZero,
135
- points: expectedPoints,
136
- sampleAnswers: expectedSampleAnswers,
137
- maxPoints: expectedPoints.length - 1,
138
- });
139
- });
140
- };
141
-
142
- testChangeMaxPoints(1, false, takeRight(points, 2), takeRight(sampleAnswers, 2));
143
- testChangeMaxPoints(1, true, takeRight(points, 2), takeRight(sampleAnswers, 2));
144
- testChangeMaxPoints(2, true, takeRight(points, 3), takeRight(sampleAnswers, 3));
145
- testChangeMaxPoints(2, false, takeRight(points, 3), takeRight(sampleAnswers, 3));
146
- testChangeMaxPoints(5, false, ['', ''].concat(points), [null, null].concat(sampleAnswers));
147
- });
148
-
149
- describe('changeSampleResponse', () => {
150
- const testChangeSampleResponse = (index, clickedItem, excludeZero, expectedPoints, expectedSampleAnswers) => {
151
- it(`Point ${index} with clickedItem="${clickedItem}" calls onChange correctly`, () => {
152
- const { onChange } = renderComponent({ excludeZero });
153
-
154
- // Test the logic by creating a component instance and calling the method
155
- const component = new RawAuthoring({
156
- value: { excludeZero, points, sampleAnswers },
157
- onChange,
158
- classes: {},
159
- className: 'className',
160
- });
161
-
162
- component.onPointMenuChange(index, clickedItem);
163
-
164
- expect(onChange).toHaveBeenCalledWith({
165
- excludeZero,
166
- points: expectedPoints,
167
- sampleAnswers: expectedSampleAnswers,
168
- });
169
- });
170
- };
171
-
172
- testChangeSampleResponse(0, 'sample', false, points, ['', 'just right', 'not left', null]);
173
- testChangeSampleResponse(3, 'sample', false, points, [null, 'just right', 'not left', '']);
174
- testChangeSampleResponse(1, 'sample', true, points, [null, null, 'not left', null]);
175
- testChangeSampleResponse(3, 'sample', true, points, [null, 'just right', 'not left', '']);
176
- });
177
- });
178
-
179
- describe('user interactions', () => {
180
- it('calls onChange when excluding zeros checkbox is toggled', () => {
181
- const { onChange } = renderComponent({ excludeZero: false });
182
-
183
- const checkbox = screen.getByLabelText('Exclude zeros');
184
- fireEvent.click(checkbox);
185
-
186
- expect(onChange).toHaveBeenCalledWith(
187
- expect.objectContaining({
188
- excludeZero: true,
189
- }),
190
- );
191
- });
192
-
193
- it('calls onChange when max points is changed', () => {
194
- const { onChange } = renderComponent();
195
-
196
- const selectInput = screen.getByLabelText('Max Points');
197
- fireEvent.mouseDown(selectInput);
198
-
199
- // The onChange should be called when a new value is selected
200
- // This tests the changeMaxPoints logic indirectly
201
- });
202
-
203
- it('renders correct number of points based on excludeZero', () => {
204
- const { rerender } = renderComponent({ excludeZero: false });
205
- expect(screen.getByText('0 pt')).toBeInTheDocument();
206
-
207
- // Rerender with excludeZero true
208
- rerender(
209
- <ThemeProvider theme={theme}>
210
- <RawAuthoring
211
- classes={{}}
212
- onChange={jest.fn()}
213
- className="className"
214
- value={{ excludeZero: true, points, sampleAnswers }}
215
- />
216
- </ThemeProvider>,
217
- );
218
-
219
- expect(screen.queryByText('0 pt')).not.toBeInTheDocument();
220
- });
221
- });
222
-
223
- describe('drag and drop', () => {
224
- it('calls onChange with reordered points after drag end', () => {
225
- const { onChange } = renderComponent();
226
-
227
- const component = new RawAuthoring({
228
- value: { excludeZero: false, points, sampleAnswers },
229
- onChange,
230
- classes: {},
231
- className: 'className',
232
- });
233
-
234
- // Simulate drag from index 0 to index 2
235
- component.dragEnd({
236
- source: { index: 0 },
237
- destination: { index: 2 },
238
- });
239
-
240
- expect(onChange).toHaveBeenCalled();
241
- const result = onChange.mock.calls[0][0];
242
- expect(result.points).not.toEqual(points);
243
- expect(result.points.length).toBe(points.length);
244
- });
245
-
246
- it('does nothing when drag has no destination', () => {
247
- const { onChange } = renderComponent();
248
-
249
- const component = new RawAuthoring({
250
- value: { excludeZero: false, points, sampleAnswers },
251
- onChange,
252
- classes: {},
253
- className: 'className',
254
- });
255
-
256
- // Simulate drag without destination
257
- component.dragEnd({
258
- source: { index: 0 },
259
- destination: null,
260
- });
261
-
262
- expect(onChange).not.toHaveBeenCalled();
263
- });
264
- });
265
-
266
- describe('content editing', () => {
267
- it('calls onChange when point content is changed', () => {
268
- const { onChange } = renderComponent();
269
-
270
- const component = new RawAuthoring({
271
- value: { excludeZero: false, points, sampleAnswers },
272
- onChange,
273
- classes: {},
274
- className: 'className',
275
- });
276
-
277
- component.changeContent(0, 'new content', 'points');
278
-
279
- expect(onChange).toHaveBeenCalledWith(
280
- expect.objectContaining({
281
- points: expect.arrayContaining(['new content']),
282
- }),
283
- );
284
- });
285
-
286
- it('calls onChange when sample answer is changed', () => {
287
- const { onChange } = renderComponent();
288
-
289
- const component = new RawAuthoring({
290
- value: { excludeZero: false, points, sampleAnswers },
291
- onChange,
292
- classes: {},
293
- className: 'className',
294
- });
295
-
296
- component.changeContent(1, 'new sample answer', 'sampleAnswers');
297
-
298
- expect(onChange).toHaveBeenCalledWith(
299
- expect.objectContaining({
300
- sampleAnswers: expect.arrayContaining(['new sample answer']),
301
- }),
302
- );
303
- });
304
-
305
- it('does not change content for invalid type', () => {
306
- const { onChange } = renderComponent();
307
-
308
- const component = new RawAuthoring({
309
- value: { excludeZero: false, points, sampleAnswers },
310
- onChange,
311
- classes: {},
312
- className: 'className',
313
- });
314
-
315
- component.changeContent(0, 'new content', 'invalidType');
316
-
317
- expect(onChange).not.toHaveBeenCalled();
318
- });
319
- });
320
-
321
- describe('rubricless mode', () => {
322
- it('calls onChange when rubricless instruction is changed', () => {
323
- const { onChange } = renderComponent({ rubriclessInstruction: '' }, { rubricless: true });
324
-
325
- const component = new RawAuthoring({
326
- value: { excludeZero: false, points, sampleAnswers, rubriclessInstruction: '' },
327
- onChange,
328
- classes: {},
329
- className: 'className',
330
- rubricless: true,
331
- });
332
-
333
- component.changeRubriclessInstruction('New instruction');
334
-
335
- expect(onChange).toHaveBeenCalledWith(
336
- expect.objectContaining({
337
- rubriclessInstruction: 'New instruction',
338
- }),
339
- );
340
- });
341
- });
342
-
343
- describe('edge cases', () => {
344
- it('handles empty points array', () => {
345
- const { container } = renderComponent({ points: [], sampleAnswers: [] });
346
- expect(container).toBeInTheDocument();
347
- });
348
-
349
- it('handles maxPoints greater than current max', () => {
350
- const { onChange } = renderComponent();
351
-
352
- const component = new RawAuthoring({
353
- value: { excludeZero: false, points, sampleAnswers },
354
- onChange,
355
- classes: {},
356
- className: 'className',
357
- });
358
-
359
- component.changeMaxPoints(10);
360
-
361
- expect(onChange).toHaveBeenCalled();
362
- const result = onChange.mock.calls[0][0];
363
- expect(result.points.length).toBe(11); // 10 + 1 for 0 points
364
- });
365
-
366
- it('handles config and pluginOpts props', () => {
367
- const config = { someConfig: true };
368
- const pluginOpts = { somePlugin: true };
369
- const { container } = renderComponent({}, { config, pluginOpts });
370
- expect(container).toBeInTheDocument();
371
- });
372
- });
373
- });