@pie-lib/mask-markup 3.0.4-next.33 → 3.0.4-next.34

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 (107) hide show
  1. package/CHANGELOG.json +17 -0
  2. package/CHANGELOG.md +1256 -0
  3. package/LICENSE.md +5 -0
  4. package/lib/choices/choice.js +116 -0
  5. package/lib/choices/choice.js.map +1 -0
  6. package/lib/choices/index.js +103 -0
  7. package/lib/choices/index.js.map +1 -0
  8. package/lib/componentize.js +21 -0
  9. package/lib/componentize.js.map +1 -0
  10. package/lib/components/blank.js +371 -0
  11. package/lib/components/blank.js.map +1 -0
  12. package/lib/components/correct-input.js +94 -0
  13. package/lib/components/correct-input.js.map +1 -0
  14. package/lib/components/dropdown.js +483 -0
  15. package/lib/components/dropdown.js.map +1 -0
  16. package/lib/components/input.js +50 -0
  17. package/lib/components/input.js.map +1 -0
  18. package/lib/constructed-response.js +101 -0
  19. package/lib/constructed-response.js.map +1 -0
  20. package/lib/customizable.js +42 -0
  21. package/lib/customizable.js.map +1 -0
  22. package/lib/drag-in-the-blank.js +254 -0
  23. package/lib/drag-in-the-blank.js.map +1 -0
  24. package/lib/index.js +55 -0
  25. package/lib/index.js.map +1 -0
  26. package/lib/inline-dropdown.js +40 -0
  27. package/lib/inline-dropdown.js.map +1 -0
  28. package/lib/mask.js +198 -0
  29. package/lib/mask.js.map +1 -0
  30. package/lib/serialization.js +261 -0
  31. package/lib/serialization.js.map +1 -0
  32. package/lib/with-mask.js +97 -0
  33. package/lib/with-mask.js.map +1 -0
  34. package/package.json +20 -39
  35. package/src/__tests__/drag-in-the-blank.test.js +111 -0
  36. package/src/__tests__/index.test.js +38 -0
  37. package/src/__tests__/mask.test.js +381 -0
  38. package/src/__tests__/serialization.test.js +54 -0
  39. package/src/__tests__/utils.js +1 -0
  40. package/src/__tests__/with-mask.test.js +76 -0
  41. package/src/choices/__tests__/index.test.js +75 -0
  42. package/src/choices/choice.jsx +97 -0
  43. package/src/choices/index.jsx +64 -0
  44. package/src/componentize.js +13 -0
  45. package/src/components/__tests__/blank.test.js +199 -0
  46. package/src/components/__tests__/correct-input.test.js +90 -0
  47. package/src/components/__tests__/dropdown.test.js +129 -0
  48. package/src/components/__tests__/input.test.js +102 -0
  49. package/src/components/blank.jsx +386 -0
  50. package/src/components/correct-input.jsx +82 -0
  51. package/src/components/dropdown.jsx +423 -0
  52. package/src/components/input.jsx +48 -0
  53. package/src/constructed-response.jsx +87 -0
  54. package/src/customizable.jsx +34 -0
  55. package/src/drag-in-the-blank.jsx +241 -0
  56. package/src/index.js +16 -0
  57. package/src/inline-dropdown.jsx +29 -0
  58. package/src/mask.jsx +172 -0
  59. package/src/serialization.js +260 -0
  60. package/src/with-mask.jsx +75 -0
  61. package/dist/_virtual/_rolldown/runtime.js +0 -4
  62. package/dist/choices/choice.d.ts +0 -24
  63. package/dist/choices/choice.js +0 -77
  64. package/dist/choices/index.d.ts +0 -25
  65. package/dist/choices/index.js +0 -49
  66. package/dist/componentize.d.ts +0 -12
  67. package/dist/componentize.js +0 -4
  68. package/dist/components/blank.d.ts +0 -39
  69. package/dist/components/blank.js +0 -240
  70. package/dist/components/correct-input.d.ts +0 -11
  71. package/dist/components/dropdown.d.ts +0 -37
  72. package/dist/components/dropdown.js +0 -320
  73. package/dist/components/input.d.ts +0 -37
  74. package/dist/constructed-response.d.ts +0 -44
  75. package/dist/constructed-response.js +0 -55
  76. package/dist/customizable.d.ts +0 -43
  77. package/dist/customizable.js +0 -8
  78. package/dist/drag-in-the-blank.d.ts +0 -37
  79. package/dist/drag-in-the-blank.js +0 -164
  80. package/dist/index.d.ts +0 -15
  81. package/dist/index.js +0 -7
  82. package/dist/inline-dropdown.d.ts +0 -44
  83. package/dist/inline-dropdown.js +0 -24
  84. package/dist/mask.d.ts +0 -30
  85. package/dist/mask.js +0 -99
  86. package/dist/node_modules/.bun/clsx@2.1.1/node_modules/clsx/dist/clsx.js +0 -16
  87. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/index.js +0 -17
  88. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssPrefix.js +0 -9
  89. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/cssUnitless.js +0 -26
  90. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/hasOwn.js +0 -11
  91. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isFunction.js +0 -11
  92. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/isObject.js +0 -11
  93. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixInfo.js +0 -24
  94. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixProperties.js +0 -32
  95. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/prefixer.js +0 -29
  96. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/camelize.js +0 -14
  97. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenRe.js +0 -8
  98. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/hyphenate.js +0 -12
  99. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/separate.js +0 -11
  100. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toLowerFirst.js +0 -10
  101. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/stringUtils/toUpperFirst.js +0 -10
  102. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleObject.js +0 -55
  103. package/dist/node_modules/.bun/to-style@1.3.3/node_modules/to-style/src/toStyleString.js +0 -16
  104. package/dist/serialization.d.ts +0 -34
  105. package/dist/serialization.js +0 -132
  106. package/dist/with-mask.d.ts +0 -55
  107. package/dist/with-mask.js +0 -45
@@ -0,0 +1,64 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { findKey } from 'lodash-es';
4
+ import Choice from './choice';
5
+ import { DragDroppablePlaceholder } from '@pie-lib/drag';
6
+
7
+ export default class Choices extends React.Component {
8
+ static propTypes = {
9
+ disabled: PropTypes.bool,
10
+ duplicates: PropTypes.bool,
11
+ choices: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, value: PropTypes.string })),
12
+ value: PropTypes.object,
13
+ choicePosition: PropTypes.string.isRequired,
14
+ instanceId: PropTypes.string, // Added for drag isolation
15
+ };
16
+
17
+ getStyleForWrapper = () => {
18
+ const { choicePosition } = this.props;
19
+
20
+ switch (choicePosition) {
21
+ case 'above':
22
+ return {
23
+ margin: '0 0 40px 0',
24
+ };
25
+
26
+ case 'below':
27
+ return {
28
+ margin: '40px 0 0 0',
29
+ };
30
+
31
+ case 'right':
32
+ return {
33
+ margin: '0 0 0 40px',
34
+ };
35
+
36
+ default:
37
+ return {
38
+ margin: '0 40px 0 0',
39
+ };
40
+ }
41
+ };
42
+
43
+ render() {
44
+ const { disabled, duplicates, choices, value, instanceId } = this.props;
45
+ const filteredChoices = choices.filter((c) => {
46
+ if (duplicates === true) {
47
+ return true;
48
+ }
49
+ const foundChoice = findKey(value, (v) => v === c.id);
50
+ return foundChoice === undefined;
51
+ });
52
+ const elementStyle = { ...this.getStyleForWrapper(), minWidth: '100px' };
53
+
54
+ return (
55
+ <div style={elementStyle}>
56
+ <DragDroppablePlaceholder disabled={disabled} instanceId={instanceId}>
57
+ {filteredChoices.map((c, index) => (
58
+ <Choice key={`${c.value}-${index}`} disabled={disabled} choice={c} instanceId={instanceId} />
59
+ ))}
60
+ </DragDroppablePlaceholder>
61
+ </div>
62
+ );
63
+ }
64
+ }
@@ -0,0 +1,13 @@
1
+ const REGEX = /\{\{(\d+)\}\}/g;
2
+
3
+ export default (s, t) => {
4
+ if (!s) {
5
+ return { markup: '' };
6
+ }
7
+
8
+ const markup = s.replace(REGEX, (match, g) => {
9
+ return `<span data-component="${t}" data-id="${g}"></span>`;
10
+ });
11
+
12
+ return { markup };
13
+ };
@@ -0,0 +1,199 @@
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
+ describe('Blank', () => {
34
+ const { renderMath } = require('@pie-lib/math-rendering');
35
+ const onChange = jest.fn();
36
+ const defaultProps = {
37
+ disabled: false,
38
+ choice: { value: 'Cow' },
39
+ isOver: false,
40
+ dragItem: {},
41
+ correct: false,
42
+ onChange,
43
+ };
44
+
45
+ beforeEach(() => {
46
+ onChange.mockClear();
47
+ renderMath.mockClear();
48
+ });
49
+
50
+ describe('rendering', () => {
51
+ it('renders with default props', () => {
52
+ const { container } = render(<Blank {...defaultProps} />);
53
+ expect(container.firstChild).toBeInTheDocument();
54
+ });
55
+
56
+ it('displays the value when provided', () => {
57
+ render(<Blank {...defaultProps} />);
58
+ expect(screen.getByText('Cow')).toBeInTheDocument();
59
+ });
60
+
61
+ it('renders as disabled when disabled prop is true', () => {
62
+ render(<Blank {...defaultProps} disabled={true} />);
63
+ // Check that delete button is not present when disabled
64
+ expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
65
+ });
66
+
67
+ it('renders with dragged item preview', () => {
68
+ render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} />);
69
+ // Blank component should render
70
+ expect(screen.getByText('Cow')).toBeInTheDocument();
71
+ });
72
+
73
+ it('shows hover state when isOver is true', () => {
74
+ const { container } = render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} isOver={true} />);
75
+ // Component should have hover styling
76
+ expect(container.firstChild).toBeInTheDocument();
77
+ });
78
+
79
+ it('shows correct state when correct is true', () => {
80
+ const { container } = render(<Blank {...defaultProps} correct={true} />);
81
+ // Component should indicate correctness
82
+ expect(container.firstChild).toBeInTheDocument();
83
+ });
84
+ });
85
+
86
+ describe('delete functionality', () => {
87
+ it('does not show delete button when disabled', () => {
88
+ render(<Blank {...defaultProps} disabled={true} />);
89
+ expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
90
+ });
91
+
92
+ it('does not show delete button when no value is set', () => {
93
+ render(<Blank {...defaultProps} choice={undefined} />);
94
+ expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
95
+ });
96
+
97
+ it('shows delete button when value is present and not disabled', () => {
98
+ render(<Blank {...defaultProps} />);
99
+ // If delete button is present, it should be clickable
100
+ const deleteButton = screen.queryByRole('button');
101
+ if (deleteButton) {
102
+ expect(deleteButton).toBeInTheDocument();
103
+ }
104
+ });
105
+ });
106
+
107
+ describe('dimensions', () => {
108
+ it('renders with custom dimensions when provided', () => {
109
+ const { container } = render(
110
+ <Blank {...defaultProps} emptyResponseAreaHeight={100} emptyResponseAreaWidth={200} />,
111
+ );
112
+ const element = container.firstChild;
113
+ expect(element).toBeInTheDocument();
114
+ });
115
+
116
+ it('renders with min dimensions by default', () => {
117
+ const { container } = render(<Blank {...defaultProps} />);
118
+ const element = container.firstChild;
119
+ expect(element).toBeInTheDocument();
120
+ // Component should have minimum dimensions applied
121
+ });
122
+
123
+ it('handles non-numeric dimension props gracefully', () => {
124
+ const { container } = render(
125
+ <Blank {...defaultProps} emptyResponseAreaHeight="non-numeric" emptyResponseAreaWidth="non-numeric" />,
126
+ );
127
+ expect(container.firstChild).toBeInTheDocument();
128
+ });
129
+
130
+ it('computes chip dimensions based on content when no emptyResponseArea size is provided', () => {
131
+ jest.useFakeTimers();
132
+ // Mock getBoundingClientRect to simulate measured content size
133
+ const rectSpy = jest
134
+ .spyOn(HTMLElement.prototype, 'getBoundingClientRect')
135
+ .mockReturnValue({ width: 100, height: 20, top: 0, left: 0, right: 100, bottom: 20 });
136
+
137
+ const { container } = render(
138
+ <Blank
139
+ {...defaultProps}
140
+ // Force measurement path that uses getMeasureNode / updateDimensions
141
+ emptyResponseAreaHeight={0}
142
+ emptyResponseAreaWidth={0}
143
+ />,
144
+ );
145
+
146
+ // Let the internal timeout in handleElements / updateDimensions run
147
+ act(() => {
148
+ jest.runAllTimers();
149
+ });
150
+
151
+ const wrapper = container.firstChild; // StyledContent
152
+ const chip = wrapper && wrapper.firstChild; // StyledChip (rootRef)
153
+
154
+ // Width and height should include padding (24px) around measured content
155
+ expect(chip.style.width).toBe('124px');
156
+ expect(chip.style.height).toBe('44px');
157
+
158
+ rectSpy.mockRestore();
159
+ jest.useRealTimers();
160
+ });
161
+ });
162
+
163
+ describe('math rendering', () => {
164
+ it('calls renderMath on mount when choice has content', () => {
165
+ render(<Blank {...defaultProps} />);
166
+ expect(renderMath).toHaveBeenCalled();
167
+ });
168
+
169
+ it('calls renderMath again when correct changes', () => {
170
+ const { rerender } = render(<Blank {...defaultProps} correct={false} />);
171
+ const callsAfterMount = renderMath.mock.calls.length;
172
+ expect(callsAfterMount).toBeGreaterThan(0);
173
+
174
+ rerender(<Blank {...defaultProps} correct={true} />);
175
+ expect(renderMath.mock.calls.length).toBeGreaterThan(callsAfterMount);
176
+ });
177
+
178
+ it('does not call renderMath again when correct is unchanged', () => {
179
+ const { rerender } = render(<Blank {...defaultProps} correct={true} />);
180
+ const callsAfterMount = renderMath.mock.calls.length;
181
+
182
+ rerender(<Blank {...defaultProps} correct={true} />);
183
+ expect(renderMath.mock.calls.length).toBe(callsAfterMount);
184
+ });
185
+ });
186
+
187
+ describe('drag and drop', () => {
188
+ it('accepts drag item when not disabled', () => {
189
+ render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
190
+ expect(screen.getByText('Cow')).toBeInTheDocument();
191
+ });
192
+
193
+ it('shows drag preview when dragging over', () => {
194
+ const { container } = render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
195
+ expect(container.firstChild).toBeInTheDocument();
196
+ // Should show visual feedback for drag over
197
+ });
198
+ });
199
+ });
@@ -0,0 +1,90 @@
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
+ });
@@ -0,0 +1,129 @@
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
+ });
@@ -0,0 +1,102 @@
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
+ });