@pie-lib/mask-markup 3.1.0-beta.7 → 3.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.
Files changed (35) hide show
  1. package/lib/keyboard-coordinates.js +156 -0
  2. package/package.json +2 -2
  3. package/lib/__tests__/drag-in-the-blank.test.js +0 -129
  4. package/lib/__tests__/drag-in-the-blank.test.js.map +0 -1
  5. package/lib/__tests__/index.test.js +0 -39
  6. package/lib/__tests__/index.test.js.map +0 -1
  7. package/lib/__tests__/mask.test.js +0 -344
  8. package/lib/__tests__/mask.test.js.map +0 -1
  9. package/lib/__tests__/serialization.test.js +0 -44
  10. package/lib/__tests__/serialization.test.js.map +0 -1
  11. package/lib/__tests__/utils.js +0 -14
  12. package/lib/__tests__/utils.js.map +0 -1
  13. package/lib/__tests__/with-mask.test.js +0 -110
  14. package/lib/__tests__/with-mask.test.js.map +0 -1
  15. package/lib/choices/__tests__/index.test.js +0 -139
  16. package/lib/choices/__tests__/index.test.js.map +0 -1
  17. package/lib/components/__tests__/blank.test.js +0 -293
  18. package/lib/components/__tests__/blank.test.js.map +0 -1
  19. package/lib/components/__tests__/correct-input.test.js +0 -132
  20. package/lib/components/__tests__/correct-input.test.js.map +0 -1
  21. package/lib/components/__tests__/dropdown.test.js +0 -202
  22. package/lib/components/__tests__/dropdown.test.js.map +0 -1
  23. package/lib/components/__tests__/input.test.js +0 -129
  24. package/lib/components/__tests__/input.test.js.map +0 -1
  25. package/src/__tests__/drag-in-the-blank.test.js +0 -111
  26. package/src/__tests__/index.test.js +0 -38
  27. package/src/__tests__/mask.test.js +0 -381
  28. package/src/__tests__/serialization.test.js +0 -54
  29. package/src/__tests__/utils.js +0 -1
  30. package/src/__tests__/with-mask.test.js +0 -76
  31. package/src/choices/__tests__/index.test.js +0 -109
  32. package/src/components/__tests__/blank.test.js +0 -232
  33. package/src/components/__tests__/correct-input.test.js +0 -90
  34. package/src/components/__tests__/dropdown.test.js +0 -129
  35. package/src/components/__tests__/input.test.js +0 -102
@@ -1,232 +0,0 @@
1
- import * as React from 'react';
2
- import { render, screen, act } from '@testing-library/react';
3
- import Blank from '../blank';
4
-
5
- // Mock @dnd-kit hooks to avoid DndContext requirement
6
- jest.mock('@dnd-kit/core', () => ({
7
- useDraggable: jest.fn(() => ({
8
- attributes: {},
9
- listeners: {},
10
- setNodeRef: jest.fn(),
11
- transform: null,
12
- isDragging: false,
13
- })),
14
- useDroppable: jest.fn(() => ({
15
- setNodeRef: jest.fn(),
16
- isOver: false,
17
- active: null,
18
- })),
19
- }));
20
-
21
- jest.mock('@dnd-kit/utilities', () => ({
22
- CSS: {
23
- Translate: {
24
- toString: jest.fn(() => 'translate3d(0, 0, 0)'),
25
- },
26
- },
27
- }));
28
-
29
- jest.mock('@pie-lib/math-rendering', () => ({
30
- renderMath: jest.fn(),
31
- }));
32
-
33
- // Collect the CSS rules emotion/MUI injected into the document (jsdom uses insertRule).
34
- const collectEmotionRules = () => {
35
- const rules = [];
36
- for (const sheet of Array.from(document.styleSheets)) {
37
- try {
38
- for (const rule of Array.from(sheet.cssRules)) {
39
- rules.push(rule.cssText);
40
- }
41
- } catch (e) {
42
- /* inaccessible stylesheet */
43
- }
44
- }
45
- return rules;
46
- };
47
-
48
- describe('Blank', () => {
49
- const { renderMath } = require('@pie-lib/math-rendering');
50
- const onChange = jest.fn();
51
- const defaultProps = {
52
- disabled: false,
53
- choice: { value: 'Cow' },
54
- isOver: false,
55
- dragItem: {},
56
- correct: false,
57
- onChange,
58
- };
59
-
60
- beforeEach(() => {
61
- onChange.mockClear();
62
- renderMath.mockClear();
63
- });
64
-
65
- describe('rendering', () => {
66
- it('renders with default props', () => {
67
- const { container } = render(<Blank {...defaultProps} />);
68
- expect(container.firstChild).toBeInTheDocument();
69
- });
70
-
71
- it('displays the value when provided', () => {
72
- render(<Blank {...defaultProps} />);
73
- expect(screen.getByText('Cow')).toBeInTheDocument();
74
- });
75
-
76
- it('renders as disabled when disabled prop is true', () => {
77
- render(<Blank {...defaultProps} disabled={true} />);
78
- // Check that delete button is not present when disabled
79
- expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
80
- });
81
-
82
- it('renders with dragged item preview', () => {
83
- render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} />);
84
- // Blank component should render
85
- expect(screen.getByText('Cow')).toBeInTheDocument();
86
- });
87
-
88
- it('shows hover state when isOver is true', () => {
89
- const { container } = render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} isOver={true} />);
90
- // Component should have hover styling
91
- expect(container.firstChild).toBeInTheDocument();
92
- });
93
-
94
- it('shows correct state when correct is true', () => {
95
- const { container } = render(<Blank {...defaultProps} correct={true} />);
96
- // Component should indicate correctness
97
- expect(container.firstChild).toBeInTheDocument();
98
- });
99
- });
100
-
101
- describe('delete functionality', () => {
102
- it('does not show delete button when disabled', () => {
103
- render(<Blank {...defaultProps} disabled={true} />);
104
- expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
105
- });
106
-
107
- it('does not show delete button when no value is set', () => {
108
- render(<Blank {...defaultProps} choice={undefined} />);
109
- expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
110
- });
111
-
112
- it('shows delete button when value is present and not disabled', () => {
113
- render(<Blank {...defaultProps} />);
114
- // If delete button is present, it should be clickable
115
- const deleteButton = screen.queryByRole('button');
116
- if (deleteButton) {
117
- expect(deleteButton).toBeInTheDocument();
118
- }
119
- });
120
- });
121
-
122
- describe('dimensions', () => {
123
- it('renders with custom dimensions when provided', () => {
124
- const { container } = render(
125
- <Blank {...defaultProps} emptyResponseAreaHeight={100} emptyResponseAreaWidth={200} />,
126
- );
127
- const element = container.firstChild;
128
- expect(element).toBeInTheDocument();
129
- });
130
-
131
- it('renders with min dimensions by default', () => {
132
- const { container } = render(<Blank {...defaultProps} />);
133
- const element = container.firstChild;
134
- expect(element).toBeInTheDocument();
135
- // Component should have minimum dimensions applied
136
- });
137
-
138
- it('handles non-numeric dimension props gracefully', () => {
139
- const { container } = render(
140
- <Blank {...defaultProps} emptyResponseAreaHeight="non-numeric" emptyResponseAreaWidth="non-numeric" />,
141
- );
142
- expect(container.firstChild).toBeInTheDocument();
143
- });
144
-
145
- it('computes chip dimensions based on content when no emptyResponseArea size is provided', () => {
146
- jest.useFakeTimers();
147
- // Mock getBoundingClientRect to simulate measured content size
148
- const rectSpy = jest
149
- .spyOn(HTMLElement.prototype, 'getBoundingClientRect')
150
- .mockReturnValue({ width: 100, height: 20, top: 0, left: 0, right: 100, bottom: 20 });
151
-
152
- const { container } = render(
153
- <Blank
154
- {...defaultProps}
155
- // Force measurement path that uses getMeasureNode / updateDimensions
156
- emptyResponseAreaHeight={0}
157
- emptyResponseAreaWidth={0}
158
- />,
159
- );
160
-
161
- // Let the internal timeout in handleElements / updateDimensions run
162
- act(() => {
163
- jest.runAllTimers();
164
- });
165
-
166
- const wrapper = container.firstChild; // StyledContent
167
- const chip = wrapper && wrapper.firstChild; // StyledChip (rootRef)
168
-
169
- // Width and height should include padding (24px) around measured content
170
- expect(chip.style.width).toBe('129px');
171
- expect(chip.style.height).toBe('49px');
172
-
173
- rectSpy.mockRestore();
174
- jest.useRealTimers();
175
- });
176
- });
177
-
178
- describe('math rendering', () => {
179
- it('calls renderMath on mount when choice has content', () => {
180
- render(<Blank {...defaultProps} />);
181
- expect(renderMath).toHaveBeenCalled();
182
- });
183
-
184
- it('calls renderMath again when correct changes', () => {
185
- const { rerender } = render(<Blank {...defaultProps} correct={false} />);
186
- const callsAfterMount = renderMath.mock.calls.length;
187
- expect(callsAfterMount).toBeGreaterThan(0);
188
-
189
- rerender(<Blank {...defaultProps} correct={true} />);
190
- expect(renderMath.mock.calls.length).toBeGreaterThan(callsAfterMount);
191
- });
192
-
193
- it('does not call renderMath again when correct is unchanged', () => {
194
- const { rerender } = render(<Blank {...defaultProps} correct={true} />);
195
- const callsAfterMount = renderMath.mock.calls.length;
196
-
197
- rerender(<Blank {...defaultProps} correct={true} />);
198
- expect(renderMath.mock.calls.length).toBe(callsAfterMount);
199
- });
200
- });
201
-
202
- describe('fraction math styling', () => {
203
- it('enlarges numerator/denominator digits adjacent to a fraction to 120%', () => {
204
- render(<Blank {...defaultProps} />);
205
- const rule = collectEmotionRules().find((r) => r.includes('mjx-mn') && r.includes('mjx-mfrac'));
206
- expect(rule).toBeDefined();
207
- expect(rule).toMatch(/mjx-mn:has\(~\s*mjx-mfrac\)/);
208
- expect(rule).toMatch(/mjx-mfrac\s*~\s*mjx-mn/);
209
- expect(rule).toMatch(/font-size:\s*120%\s*!important/i);
210
- });
211
-
212
- it('keeps the existing mjx-frac 120% rule', () => {
213
- render(<Blank {...defaultProps} />);
214
- const rule = collectEmotionRules().find((r) => /(^|[^-])mjx-frac/.test(r) && !r.includes('mjx-mfrac'));
215
- expect(rule).toBeDefined();
216
- expect(rule).toMatch(/font-size:\s*120%\s*!important/i);
217
- });
218
- });
219
-
220
- describe('drag and drop', () => {
221
- it('accepts drag item when not disabled', () => {
222
- render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
223
- expect(screen.getByText('Cow')).toBeInTheDocument();
224
- });
225
-
226
- it('shows drag preview when dragging over', () => {
227
- const { container } = render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
228
- expect(container.firstChild).toBeInTheDocument();
229
- // Should show visual feedback for drag over
230
- });
231
- });
232
- });
@@ -1,90 +0,0 @@
1
- import * as React from 'react';
2
- import { render, screen } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import CorrectInput from '../correct-input';
5
-
6
- describe('CorrectInput', () => {
7
- const onChange = jest.fn();
8
- const defaultProps = {
9
- disabled: false,
10
- correct: false,
11
- variant: 'outlined',
12
- value: 'Cow',
13
- onChange,
14
- };
15
-
16
- beforeEach(() => {
17
- onChange.mockClear();
18
- });
19
-
20
- describe('rendering', () => {
21
- it('renders input with default props', () => {
22
- render(<CorrectInput {...defaultProps} />);
23
- const input = screen.getByRole('textbox');
24
- expect(input).toBeInTheDocument();
25
- expect(input).toHaveValue('Cow');
26
- });
27
-
28
- it('renders as disabled when disabled prop is true', () => {
29
- render(<CorrectInput {...defaultProps} disabled={true} />);
30
- const input = screen.getByRole('textbox');
31
- expect(input).toBeDisabled();
32
- });
33
-
34
- it('renders with correct state as false', () => {
35
- const { container } = render(<CorrectInput {...defaultProps} correct={false} />);
36
- const input = screen.getByRole('textbox');
37
- expect(input).toBeInTheDocument();
38
- });
39
-
40
- it('renders with correct state as true', () => {
41
- const { container } = render(<CorrectInput {...defaultProps} correct={true} />);
42
- const input = screen.getByRole('textbox');
43
- expect(input).toBeInTheDocument();
44
- // Should show visual indication of correctness
45
- });
46
-
47
- it('renders with outlined variant', () => {
48
- render(<CorrectInput {...defaultProps} variant="outlined" />);
49
- const input = screen.getByRole('textbox');
50
- expect(input).toBeInTheDocument();
51
- });
52
- });
53
-
54
- describe('user interactions', () => {
55
- it('calls onChange when user types', async () => {
56
- const user = userEvent.setup();
57
- render(<CorrectInput {...defaultProps} />);
58
-
59
- const input = screen.getByRole('textbox');
60
- await user.clear(input);
61
- await user.type(input, '1');
62
-
63
- expect(onChange).toHaveBeenCalled();
64
- });
65
-
66
- it('calls onChange with event object', async () => {
67
- const user = userEvent.setup();
68
- render(<CorrectInput {...defaultProps} value="" />);
69
-
70
- const input = screen.getByRole('textbox');
71
- await user.type(input, 'test');
72
-
73
- expect(onChange).toHaveBeenCalled();
74
- // Check that onChange receives an event-like object
75
- const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0];
76
- expect(lastCall).toHaveProperty('target');
77
- });
78
-
79
- it('updates value when user changes input', async () => {
80
- const user = userEvent.setup();
81
- render(<CorrectInput {...defaultProps} />);
82
-
83
- const input = screen.getByRole('textbox');
84
- await user.clear(input);
85
- await user.type(input, 'Dog');
86
-
87
- expect(onChange).toHaveBeenCalled();
88
- });
89
- });
90
- });
@@ -1,129 +0,0 @@
1
- import * as React from 'react';
2
- import { render, screen, waitFor } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import { choice } from '../../__tests__/utils';
5
- import Dropdown from '../dropdown';
6
-
7
- describe('Dropdown', () => {
8
- const onChange = jest.fn();
9
- const defaultProps = {
10
- onChange,
11
- id: '1',
12
- correct: false,
13
- disabled: false,
14
- value: 'Jumped',
15
- choices: [choice('Jumped'), choice('Laughed'), choice('Smiled')],
16
- };
17
-
18
- beforeEach(() => {
19
- onChange.mockClear();
20
- });
21
-
22
- describe('rendering', () => {
23
- it('renders dropdown with default props', () => {
24
- render(<Dropdown {...defaultProps} />);
25
- const button = screen.getByRole('combobox');
26
- expect(button).toBeInTheDocument();
27
- // Button displays the selected value
28
- expect(button).toHaveTextContent('Jumped');
29
- });
30
-
31
- it('renders with all choices as options when opened', async () => {
32
- const user = userEvent.setup();
33
- render(<Dropdown {...defaultProps} />);
34
-
35
- const button = screen.getByRole('combobox');
36
- await user.click(button);
37
-
38
- // Options should now be visible - find them by role
39
- const options = screen.getAllByRole('option');
40
- expect(options).toHaveLength(3);
41
- // Verify the text content of options using specific elements
42
- expect(options[0]).toHaveTextContent('Jumped');
43
- expect(options[1]).toHaveTextContent('Laughed');
44
- expect(options[2]).toHaveTextContent('Smiled');
45
- });
46
-
47
- it('focuses the selected option when the menu opens', async () => {
48
- const user = userEvent.setup();
49
- render(<Dropdown {...defaultProps} />);
50
-
51
- const button = screen.getByRole('combobox');
52
- await user.click(button);
53
-
54
- await waitFor(() => {
55
- expect(document.getElementById('dropdown-option-1-0')).toHaveFocus();
56
- });
57
- expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-0');
58
- });
59
-
60
- it('focuses a non-first selected option when the menu opens', async () => {
61
- const user = userEvent.setup();
62
- render(<Dropdown {...defaultProps} value="Laughed" />);
63
-
64
- const button = screen.getByRole('combobox');
65
- await user.click(button);
66
-
67
- await waitFor(() => {
68
- expect(document.getElementById('dropdown-option-1-1')).toHaveFocus();
69
- });
70
- expect(button).toHaveAttribute('aria-activedescendant', 'dropdown-option-1-1');
71
- });
72
-
73
- it('does not highlight an option when no value is selected', async () => {
74
- const user = userEvent.setup();
75
- render(<Dropdown {...defaultProps} value={undefined} />);
76
-
77
- const button = screen.getByRole('combobox');
78
- await user.click(button);
79
-
80
- expect(button).not.toHaveAttribute('aria-activedescendant');
81
- });
82
-
83
- it('renders as disabled when disabled prop is true', () => {
84
- render(<Dropdown {...defaultProps} disabled={true} />);
85
- const button = screen.getByRole('combobox');
86
- expect(button).toBeDisabled();
87
- });
88
-
89
- it('shows correct state when correct is true', () => {
90
- const { container } = render(<Dropdown {...defaultProps} correct={true} />);
91
- const button = screen.getByRole('combobox');
92
- expect(button).toBeInTheDocument();
93
- });
94
- });
95
-
96
- describe('user interactions', () => {
97
- it('calls onChange when user selects a different option', async () => {
98
- const user = userEvent.setup();
99
- render(<Dropdown {...defaultProps} />);
100
-
101
- // Click button to open menu
102
- const button = screen.getByRole('combobox');
103
- await user.click(button);
104
-
105
- // Find the option by getting all options and selecting the one with "Laughed" text
106
- const options = screen.getAllByRole('option');
107
- const laughedOption = options.find((opt) => opt.textContent.includes('Laughed'));
108
- await user.click(laughedOption);
109
-
110
- expect(onChange).toHaveBeenCalledWith('1', 'Laughed');
111
- });
112
-
113
- it('calls onChange with correct value', async () => {
114
- const user = userEvent.setup();
115
- render(<Dropdown {...defaultProps} />);
116
-
117
- // Click button to open menu
118
- const button = screen.getByRole('combobox');
119
- await user.click(button);
120
-
121
- // Find the option by getting all options and selecting the one with "Smiled" text
122
- const options = screen.getAllByRole('option');
123
- const smiledOption = options.find((opt) => opt.textContent.includes('Smiled'));
124
- await user.click(smiledOption);
125
-
126
- expect(onChange).toHaveBeenCalledWith('1', 'Smiled');
127
- });
128
- });
129
- });
@@ -1,102 +0,0 @@
1
- import * as React from 'react';
2
- import { render, screen } from '@testing-library/react';
3
- import userEvent from '@testing-library/user-event';
4
- import Input from '../input';
5
-
6
- // Mock CorrectInput to simplify testing
7
- jest.mock('../correct-input', () => {
8
- return function CorrectInput({ value, onChange, disabled, correct, variant }) {
9
- return (
10
- <input
11
- data-testid="correct-input"
12
- value={value || ''}
13
- onChange={onChange}
14
- disabled={disabled}
15
- data-correct={correct}
16
- data-variant={variant}
17
- />
18
- );
19
- };
20
- });
21
-
22
- describe('Input', () => {
23
- const onChange = jest.fn();
24
- const defaultProps = {
25
- disabled: false,
26
- correct: false,
27
- value: 'Cow',
28
- id: '1',
29
- onChange,
30
- };
31
-
32
- beforeEach(() => {
33
- onChange.mockClear();
34
- });
35
-
36
- describe('rendering', () => {
37
- it('renders with default props', () => {
38
- render(<Input {...defaultProps} />);
39
- const input = screen.getByTestId('correct-input');
40
-
41
- expect(input).toBeInTheDocument();
42
- expect(input).toHaveValue('Cow');
43
- expect(input).not.toBeDisabled();
44
- expect(input).toHaveAttribute('data-correct', 'false');
45
- });
46
-
47
- it('renders as disabled when disabled prop is true', () => {
48
- render(<Input {...defaultProps} disabled={true} />);
49
- const input = screen.getByTestId('correct-input');
50
-
51
- expect(input).toBeDisabled();
52
- });
53
-
54
- it('renders with correct state', () => {
55
- render(<Input {...defaultProps} correct={true} />);
56
- const input = screen.getByTestId('correct-input');
57
-
58
- expect(input).toHaveAttribute('data-correct', 'true');
59
- });
60
-
61
- it('shows correct answer when showCorrectAnswer is true', () => {
62
- render(<Input {...defaultProps} showCorrectAnswer={true} />);
63
- const input = screen.getByTestId('correct-input');
64
-
65
- expect(input).toHaveAttribute('data-correct', 'true');
66
- });
67
- });
68
-
69
- describe('user interactions', () => {
70
- it('calls onChange with id and value when user types', async () => {
71
- const user = userEvent.setup();
72
- render(<Input {...defaultProps} value="" />);
73
-
74
- const input = screen.getByTestId('correct-input');
75
- await user.type(input, '20');
76
-
77
- // userEvent.type types character by character, so onChange is called for each character
78
- expect(onChange).toHaveBeenCalled();
79
- expect(onChange).toHaveBeenCalledTimes(2);
80
- // Check the last call has both characters
81
- expect(onChange).toHaveBeenLastCalledWith('1', '0');
82
- });
83
-
84
- it('calls onChange with updated value', async () => {
85
- const user = userEvent.setup();
86
- render(<Input {...defaultProps} />);
87
-
88
- const input = screen.getByTestId('correct-input');
89
- await user.clear(input);
90
- await user.type(input, 'New Value');
91
-
92
- // userEvent.type types character by character
93
- // After clear, we start with empty string, and each character is typed
94
- // The last call should have the full accumulated value up to the last character
95
- expect(onChange).toHaveBeenCalled();
96
- // With clear + "New Value", onChange is called for clearing ("") and each typed character
97
- // The value accumulated in the input element after typing will be "CowNew Value"
98
- // because the component starts with value="Cow" and we clear then type
99
- expect(onChange.mock.calls.length).toBeGreaterThan(0);
100
- });
101
- });
102
- });