@pie-lib/mask-markup 1.33.2 → 1.33.3-next.155
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/CHANGELOG.md +6 -76
- package/esm/package.json +3 -0
- package/lib/choices/choice.js +98 -203
- package/lib/choices/choice.js.map +1 -1
- package/lib/choices/index.js +21 -53
- package/lib/choices/index.js.map +1 -1
- package/lib/componentize.js +1 -5
- package/lib/componentize.js.map +1 -1
- package/lib/components/blank.js +303 -361
- package/lib/components/blank.js.map +1 -1
- package/lib/components/correct-input.js +41 -65
- package/lib/components/correct-input.js.map +1 -1
- package/lib/components/dropdown.js +218 -257
- package/lib/components/dropdown.js.map +1 -1
- package/lib/components/input.js +10 -17
- package/lib/components/input.js.map +1 -1
- package/lib/constructed-response.js +38 -52
- package/lib/constructed-response.js.map +1 -1
- package/lib/customizable.js +5 -9
- package/lib/customizable.js.map +1 -1
- package/lib/drag-in-the-blank.js +140 -105
- package/lib/drag-in-the-blank.js.map +1 -1
- package/lib/index.js +0 -7
- package/lib/index.js.map +1 -1
- package/lib/inline-dropdown.js +4 -12
- package/lib/inline-dropdown.js.map +1 -1
- package/lib/mask.js +60 -118
- package/lib/mask.js.map +1 -1
- package/lib/serialization.js +8 -48
- package/lib/serialization.js.map +1 -1
- package/lib/with-mask.js +30 -58
- package/lib/with-mask.js.map +1 -1
- package/package.json +20 -12
- package/src/__tests__/drag-in-the-blank.test.js +66 -26
- package/src/__tests__/mask.test.js +147 -112
- package/src/__tests__/with-mask.test.js +44 -19
- package/src/choices/__tests__/index.test.js +38 -25
- package/src/choices/choice.jsx +86 -153
- package/src/choices/index.jsx +9 -3
- package/src/components/__tests__/blank.test.js +92 -156
- package/src/components/__tests__/correct-input.test.js +60 -19
- package/src/components/__tests__/dropdown.test.js +61 -19
- package/src/components/__tests__/input.test.js +72 -20
- package/src/components/blank.jsx +273 -272
- package/src/components/correct-input.jsx +33 -39
- package/src/components/dropdown.jsx +173 -161
- package/src/constructed-response.jsx +22 -18
- package/src/drag-in-the-blank.jsx +131 -42
- package/src/mask.jsx +38 -29
- package/src/with-mask.jsx +7 -4
- package/src/__tests__/__snapshots__/drag-in-the-blank.test.js.snap +0 -316
- package/src/__tests__/__snapshots__/mask.test.js.snap +0 -55
- package/src/__tests__/__snapshots__/with-mask.test.js.snap +0 -62
- package/src/choices/__tests__/__snapshots__/index.test.js.snap +0 -209
- package/src/components/__tests__/__snapshots__/blank.test.js.snap +0 -111
- package/src/components/__tests__/__snapshots__/correct-input.test.js.snap +0 -64
- package/src/components/__tests__/__snapshots__/dropdown.test.js.snap +0 -136
- package/src/components/__tests__/__snapshots__/input.test.js.snap +0 -34
|
@@ -1,202 +1,138 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import Blank from '../blank';
|
|
5
|
+
|
|
6
|
+
// Mock @dnd-kit hooks to avoid DndContext requirement
|
|
7
|
+
jest.mock('@dnd-kit/core', () => ({
|
|
8
|
+
useDraggable: jest.fn(() => ({
|
|
9
|
+
attributes: {},
|
|
10
|
+
listeners: {},
|
|
11
|
+
setNodeRef: jest.fn(),
|
|
12
|
+
transform: null,
|
|
13
|
+
isDragging: false,
|
|
14
|
+
})),
|
|
15
|
+
useDroppable: jest.fn(() => ({
|
|
16
|
+
setNodeRef: jest.fn(),
|
|
17
|
+
isOver: false,
|
|
18
|
+
active: null,
|
|
19
|
+
})),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
jest.mock('@dnd-kit/utilities', () => ({
|
|
23
|
+
CSS: {
|
|
24
|
+
Translate: {
|
|
25
|
+
toString: jest.fn(() => 'translate3d(0, 0, 0)'),
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
}));
|
|
4
29
|
|
|
5
30
|
describe('Blank', () => {
|
|
6
31
|
const onChange = jest.fn();
|
|
7
32
|
const defaultProps = {
|
|
8
33
|
disabled: false,
|
|
9
|
-
value: 'Cow',
|
|
10
|
-
classes: {},
|
|
34
|
+
choice: { value: 'Cow' },
|
|
11
35
|
isOver: false,
|
|
12
36
|
dragItem: {},
|
|
13
37
|
correct: false,
|
|
14
38
|
onChange,
|
|
15
39
|
};
|
|
16
|
-
let wrapper;
|
|
17
|
-
let instance;
|
|
18
40
|
|
|
19
41
|
beforeEach(() => {
|
|
20
|
-
|
|
21
|
-
instance = wrapper.instance();
|
|
42
|
+
onChange.mockClear();
|
|
22
43
|
});
|
|
23
44
|
|
|
24
|
-
describe('
|
|
25
|
-
it('renders
|
|
26
|
-
|
|
45
|
+
describe('rendering', () => {
|
|
46
|
+
it('renders with default props', () => {
|
|
47
|
+
const { container } = render(<Blank {...defaultProps} />);
|
|
48
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
27
49
|
});
|
|
28
50
|
|
|
29
|
-
it('
|
|
30
|
-
|
|
31
|
-
expect(
|
|
51
|
+
it('displays the value when provided', () => {
|
|
52
|
+
render(<Blank {...defaultProps} />);
|
|
53
|
+
expect(screen.getByText('Cow')).toBeInTheDocument();
|
|
32
54
|
});
|
|
33
55
|
|
|
34
|
-
it('renders
|
|
35
|
-
|
|
36
|
-
|
|
56
|
+
it('renders as disabled when disabled prop is true', () => {
|
|
57
|
+
render(<Blank {...defaultProps} disabled={true} />);
|
|
58
|
+
// Check that delete button is not present when disabled
|
|
59
|
+
expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
|
|
37
60
|
});
|
|
38
61
|
|
|
39
|
-
it('renders
|
|
40
|
-
|
|
41
|
-
|
|
62
|
+
it('renders with dragged item preview', () => {
|
|
63
|
+
render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} />);
|
|
64
|
+
// Blank component should render
|
|
65
|
+
expect(screen.getByText('Cow')).toBeInTheDocument();
|
|
42
66
|
});
|
|
43
|
-
});
|
|
44
67
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
expect(wrapper.props().onDelete).toEqual(undefined);
|
|
68
|
+
it('shows hover state when isOver is true', () => {
|
|
69
|
+
const { container } = render(<Blank {...defaultProps} dragItem={{ choice: { value: 'Dog' } }} isOver={true} />);
|
|
70
|
+
// Component should have hover styling
|
|
71
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
50
72
|
});
|
|
51
73
|
|
|
52
|
-
it('
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
expect(
|
|
74
|
+
it('shows correct state when correct is true', () => {
|
|
75
|
+
const { container } = render(<Blank {...defaultProps} correct={true} />);
|
|
76
|
+
// Component should indicate correctness
|
|
77
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
56
78
|
});
|
|
57
79
|
});
|
|
58
80
|
|
|
59
|
-
describe('
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
const setSpanDimensions = (height, width) => {
|
|
64
|
-
Object.defineProperty(span, 'offsetHeight', { value: height, configurable: true });
|
|
65
|
-
Object.defineProperty(span, 'offsetWidth', { value: width, configurable: true });
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
beforeEach(() => {
|
|
69
|
-
wrapper = shallow(<Blank {...defaultProps} />);
|
|
70
|
-
instance = wrapper.instance();
|
|
71
|
-
|
|
72
|
-
span = document.createElement('span');
|
|
73
|
-
rootRef = document.createElement('span');
|
|
74
|
-
|
|
75
|
-
instance.spanRef = span;
|
|
76
|
-
instance.rootRef = rootRef;
|
|
77
|
-
|
|
78
|
-
Object.defineProperty(span, 'offsetHeight', { value: 0, configurable: true });
|
|
79
|
-
Object.defineProperty(span, 'offsetWidth', { value: 0, configurable: true });
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should update dimensions if span size exceeds the response area size', () => {
|
|
83
|
-
setSpanDimensions(50, 50);
|
|
84
|
-
|
|
85
|
-
instance.updateDimensions();
|
|
86
|
-
|
|
87
|
-
expect(instance.state).toEqual({
|
|
88
|
-
width: 74,
|
|
89
|
-
height: 74,
|
|
90
|
-
});
|
|
81
|
+
describe('delete functionality', () => {
|
|
82
|
+
it('does not show delete button when disabled', () => {
|
|
83
|
+
render(<Blank {...defaultProps} disabled={true} />);
|
|
84
|
+
expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
|
|
91
85
|
});
|
|
92
86
|
|
|
93
|
-
it('
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
emptyResponseAreaWidth: 50,
|
|
97
|
-
});
|
|
98
|
-
setSpanDimensions(30, 30);
|
|
99
|
-
|
|
100
|
-
instance.updateDimensions();
|
|
101
|
-
|
|
102
|
-
expect(instance.state).toEqual({
|
|
103
|
-
width: 54, // with padding it does exceed (30 + 24 > 50) so it's updating
|
|
104
|
-
height: 54, // with padding it does exceed (30 + 24 > 50) so it's updating
|
|
105
|
-
});
|
|
87
|
+
it('does not show delete button when no value is set', () => {
|
|
88
|
+
render(<Blank {...defaultProps} choice={undefined} />);
|
|
89
|
+
expect(screen.queryByRole('button', { name: /delete/i })).not.toBeInTheDocument();
|
|
106
90
|
});
|
|
107
91
|
|
|
108
|
-
it('
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
instance.updateDimensions();
|
|
116
|
-
|
|
117
|
-
expect(instance.state).toEqual({
|
|
118
|
-
width: 74,
|
|
119
|
-
height: 74,
|
|
120
|
-
});
|
|
92
|
+
it('shows delete button when value is present and not disabled', () => {
|
|
93
|
+
render(<Blank {...defaultProps} />);
|
|
94
|
+
// If delete button is present, it should be clickable
|
|
95
|
+
const deleteButton = screen.queryByRole('button');
|
|
96
|
+
if (deleteButton) {
|
|
97
|
+
expect(deleteButton).toBeInTheDocument();
|
|
98
|
+
}
|
|
121
99
|
});
|
|
122
100
|
});
|
|
123
101
|
|
|
124
|
-
describe('
|
|
125
|
-
it('
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
expect(
|
|
131
|
-
height: 50,
|
|
132
|
-
width: 50,
|
|
133
|
-
minWidth: 90,
|
|
134
|
-
minHeight: 32,
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it('should return state height and props width if state width is not set', () => {
|
|
139
|
-
instance.setState({ height: 50, width: 0 });
|
|
140
|
-
|
|
141
|
-
const dimensions = instance.getRootDimensions();
|
|
142
|
-
|
|
143
|
-
expect(dimensions).toEqual({
|
|
144
|
-
height: 50,
|
|
145
|
-
width: 0,
|
|
146
|
-
minWidth: 90,
|
|
147
|
-
minHeight: 32,
|
|
148
|
-
});
|
|
102
|
+
describe('dimensions', () => {
|
|
103
|
+
it('renders with custom dimensions when provided', () => {
|
|
104
|
+
const { container } = render(
|
|
105
|
+
<Blank {...defaultProps} emptyResponseAreaHeight={100} emptyResponseAreaWidth={200} />,
|
|
106
|
+
);
|
|
107
|
+
const element = container.firstChild;
|
|
108
|
+
expect(element).toBeInTheDocument();
|
|
149
109
|
});
|
|
150
110
|
|
|
151
|
-
it('
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
expect(dimensions).toEqual({
|
|
157
|
-
height: 0,
|
|
158
|
-
width: 50,
|
|
159
|
-
minWidth: 90,
|
|
160
|
-
minHeight: 32,
|
|
161
|
-
});
|
|
111
|
+
it('renders with min dimensions by default', () => {
|
|
112
|
+
const { container } = render(<Blank {...defaultProps} />);
|
|
113
|
+
const element = container.firstChild;
|
|
114
|
+
expect(element).toBeInTheDocument();
|
|
115
|
+
// Component should have minimum dimensions applied
|
|
162
116
|
});
|
|
163
117
|
|
|
164
|
-
it('
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
expect(dimensions).toEqual({
|
|
171
|
-
height: 60,
|
|
172
|
-
width: 60,
|
|
173
|
-
});
|
|
118
|
+
it('handles non-numeric dimension props gracefully', () => {
|
|
119
|
+
const { container } = render(
|
|
120
|
+
<Blank {...defaultProps} emptyResponseAreaHeight="non-numeric" emptyResponseAreaWidth="non-numeric" />,
|
|
121
|
+
);
|
|
122
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
174
123
|
});
|
|
124
|
+
});
|
|
175
125
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
const dimensions = instance.getRootDimensions();
|
|
181
|
-
|
|
182
|
-
expect(dimensions).toEqual({
|
|
183
|
-
height: 50,
|
|
184
|
-
width: 50,
|
|
185
|
-
});
|
|
126
|
+
describe('drag and drop', () => {
|
|
127
|
+
it('accepts drag item when not disabled', () => {
|
|
128
|
+
render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
|
|
129
|
+
expect(screen.getByText('Cow')).toBeInTheDocument();
|
|
186
130
|
});
|
|
187
131
|
|
|
188
|
-
it('
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
const dimensions = instance.getRootDimensions();
|
|
193
|
-
|
|
194
|
-
expect(dimensions).toEqual({
|
|
195
|
-
height: 0,
|
|
196
|
-
width: 0,
|
|
197
|
-
minWidth: 90,
|
|
198
|
-
minHeight: 32,
|
|
199
|
-
});
|
|
132
|
+
it('shows drag preview when dragging over', () => {
|
|
133
|
+
const { container } = render(<Blank {...defaultProps} isOver={true} dragItem={{ choice: { value: 'Dog' } }} />);
|
|
134
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
135
|
+
// Should show visual feedback for drag over
|
|
200
136
|
});
|
|
201
137
|
});
|
|
202
138
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
3
4
|
import CorrectInput from '../correct-input';
|
|
4
5
|
|
|
5
6
|
describe('CorrectInput', () => {
|
|
@@ -11,39 +12,79 @@ describe('CorrectInput', () => {
|
|
|
11
12
|
value: 'Cow',
|
|
12
13
|
onChange,
|
|
13
14
|
};
|
|
14
|
-
let wrapper;
|
|
15
15
|
|
|
16
16
|
beforeEach(() => {
|
|
17
|
-
|
|
17
|
+
onChange.mockClear();
|
|
18
18
|
});
|
|
19
19
|
|
|
20
|
-
describe('
|
|
21
|
-
it('renders
|
|
22
|
-
|
|
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');
|
|
23
26
|
});
|
|
24
27
|
|
|
25
|
-
it('renders
|
|
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();
|
|
28
32
|
});
|
|
29
33
|
|
|
30
|
-
it('renders
|
|
31
|
-
|
|
32
|
-
|
|
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();
|
|
33
51
|
});
|
|
34
52
|
});
|
|
35
53
|
|
|
36
|
-
describe('
|
|
37
|
-
|
|
38
|
-
|
|
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');
|
|
39
77
|
});
|
|
40
78
|
|
|
41
|
-
it('
|
|
42
|
-
const
|
|
79
|
+
it('updates value when user changes input', async () => {
|
|
80
|
+
const user = userEvent.setup();
|
|
81
|
+
render(<CorrectInput {...defaultProps} />);
|
|
43
82
|
|
|
44
|
-
|
|
83
|
+
const input = screen.getByRole('textbox');
|
|
84
|
+
await user.clear(input);
|
|
85
|
+
await user.type(input, 'Dog');
|
|
45
86
|
|
|
46
|
-
expect(onChange).
|
|
87
|
+
expect(onChange).toHaveBeenCalled();
|
|
47
88
|
});
|
|
48
89
|
});
|
|
49
90
|
});
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
3
4
|
import { choice } from '../../__tests__/utils';
|
|
4
5
|
import Dropdown from '../dropdown';
|
|
5
6
|
|
|
@@ -13,39 +14,80 @@ describe('Dropdown', () => {
|
|
|
13
14
|
value: 'Jumped',
|
|
14
15
|
choices: [choice('Jumped'), choice('Laughed'), choice('Smiled')],
|
|
15
16
|
};
|
|
16
|
-
let wrapper;
|
|
17
17
|
|
|
18
18
|
beforeEach(() => {
|
|
19
|
-
|
|
19
|
+
onChange.mockClear();
|
|
20
20
|
});
|
|
21
21
|
|
|
22
|
-
describe('
|
|
23
|
-
it('renders
|
|
24
|
-
|
|
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');
|
|
25
29
|
});
|
|
26
30
|
|
|
27
|
-
it('renders
|
|
28
|
-
|
|
29
|
-
|
|
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('renders as disabled when disabled prop is true', () => {
|
|
48
|
+
render(<Dropdown {...defaultProps} disabled={true} />);
|
|
49
|
+
const button = screen.getByRole('combobox');
|
|
50
|
+
expect(button).toBeDisabled();
|
|
30
51
|
});
|
|
31
52
|
|
|
32
|
-
it('
|
|
33
|
-
|
|
34
|
-
|
|
53
|
+
it('shows correct state when correct is true', () => {
|
|
54
|
+
const { container } = render(<Dropdown {...defaultProps} correct={true} />);
|
|
55
|
+
const button = screen.getByRole('combobox');
|
|
56
|
+
expect(button).toBeInTheDocument();
|
|
35
57
|
});
|
|
36
58
|
});
|
|
37
59
|
|
|
38
|
-
describe('
|
|
39
|
-
|
|
40
|
-
|
|
60
|
+
describe('user interactions', () => {
|
|
61
|
+
it('calls onChange when user selects a different option', async () => {
|
|
62
|
+
const user = userEvent.setup();
|
|
63
|
+
render(<Dropdown {...defaultProps} />);
|
|
64
|
+
|
|
65
|
+
// Click button to open menu
|
|
66
|
+
const button = screen.getByRole('combobox');
|
|
67
|
+
await user.click(button);
|
|
68
|
+
|
|
69
|
+
// Find the option by getting all options and selecting the one with "Laughed" text
|
|
70
|
+
const options = screen.getAllByRole('option');
|
|
71
|
+
const laughedOption = options.find(opt => opt.textContent.includes('Laughed'));
|
|
72
|
+
await user.click(laughedOption);
|
|
73
|
+
|
|
74
|
+
expect(onChange).toHaveBeenCalledWith('1', 'Laughed');
|
|
41
75
|
});
|
|
42
76
|
|
|
43
|
-
it('
|
|
44
|
-
const
|
|
77
|
+
it('calls onChange with correct value', async () => {
|
|
78
|
+
const user = userEvent.setup();
|
|
79
|
+
render(<Dropdown {...defaultProps} />);
|
|
80
|
+
|
|
81
|
+
// Click button to open menu
|
|
82
|
+
const button = screen.getByRole('combobox');
|
|
83
|
+
await user.click(button);
|
|
45
84
|
|
|
46
|
-
|
|
85
|
+
// Find the option by getting all options and selecting the one with "Smiled" text
|
|
86
|
+
const options = screen.getAllByRole('option');
|
|
87
|
+
const smiledOption = options.find(opt => opt.textContent.includes('Smiled'));
|
|
88
|
+
await user.click(smiledOption);
|
|
47
89
|
|
|
48
|
-
expect(onChange).toHaveBeenCalledWith(
|
|
90
|
+
expect(onChange).toHaveBeenCalledWith('1', 'Smiled');
|
|
49
91
|
});
|
|
50
92
|
});
|
|
51
93
|
});
|
|
@@ -1,50 +1,102 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
3
4
|
import Input from '../input';
|
|
4
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
|
+
|
|
5
22
|
describe('Input', () => {
|
|
6
23
|
const onChange = jest.fn();
|
|
7
24
|
const defaultProps = {
|
|
8
25
|
disabled: false,
|
|
9
26
|
correct: false,
|
|
10
|
-
variant: 'outlined',
|
|
11
27
|
value: 'Cow',
|
|
12
28
|
id: '1',
|
|
13
29
|
onChange,
|
|
14
30
|
};
|
|
15
|
-
let wrapper;
|
|
16
31
|
|
|
17
32
|
beforeEach(() => {
|
|
18
|
-
|
|
33
|
+
onChange.mockClear();
|
|
19
34
|
});
|
|
20
35
|
|
|
21
|
-
describe('
|
|
22
|
-
it('renders
|
|
23
|
-
|
|
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');
|
|
24
45
|
});
|
|
25
46
|
|
|
26
|
-
it('renders
|
|
27
|
-
|
|
28
|
-
|
|
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();
|
|
29
52
|
});
|
|
30
53
|
|
|
31
|
-
it('renders
|
|
32
|
-
|
|
33
|
-
|
|
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');
|
|
34
66
|
});
|
|
35
67
|
});
|
|
36
68
|
|
|
37
|
-
describe('
|
|
38
|
-
|
|
39
|
-
|
|
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');
|
|
40
82
|
});
|
|
41
83
|
|
|
42
|
-
it('
|
|
43
|
-
const
|
|
84
|
+
it('calls onChange with updated value', async () => {
|
|
85
|
+
const user = userEvent.setup();
|
|
86
|
+
render(<Input {...defaultProps} />);
|
|
44
87
|
|
|
45
|
-
|
|
88
|
+
const input = screen.getByTestId('correct-input');
|
|
89
|
+
await user.clear(input);
|
|
90
|
+
await user.type(input, 'New Value');
|
|
46
91
|
|
|
47
|
-
|
|
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);
|
|
48
100
|
});
|
|
49
101
|
});
|
|
50
102
|
});
|