@pie-lib/mask-markup 2.0.0-beta.2 → 2.0.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/CHANGELOG.json +1 -871
- package/CHANGELOG.md +296 -2
- package/LICENSE.md +5 -0
- package/NEXT.CHANGELOG.json +1 -0
- package/lib/choices/choice.js +99 -118
- package/lib/choices/choice.js.map +1 -1
- package/lib/choices/index.js +23 -19
- package/lib/choices/index.js.map +1 -1
- package/lib/componentize.js +1 -2
- package/lib/componentize.js.map +1 -1
- package/lib/components/blank.js +315 -221
- package/lib/components/blank.js.map +1 -1
- package/lib/components/correct-input.js +39 -42
- package/lib/components/correct-input.js.map +1 -1
- package/lib/components/dropdown.js +393 -124
- package/lib/components/dropdown.js.map +1 -1
- package/lib/components/input.js +1 -2
- package/lib/components/input.js.map +1 -1
- package/lib/constructed-response.js +82 -26
- package/lib/constructed-response.js.map +1 -1
- package/lib/customizable.js +44 -0
- package/lib/customizable.js.map +1 -0
- package/lib/drag-in-the-blank.js +154 -61
- package/lib/drag-in-the-blank.js.map +1 -1
- package/lib/index.js +7 -0
- package/lib/index.js.map +1 -1
- package/lib/inline-dropdown.js +4 -3
- package/lib/inline-dropdown.js.map +1 -1
- package/lib/mask.js +89 -56
- package/lib/mask.js.map +1 -1
- package/lib/serialization.js +30 -42
- package/lib/serialization.js.map +1 -1
- package/lib/with-mask.js +48 -20
- package/lib/with-mask.js.map +1 -1
- package/package.json +26 -15
- package/src/__tests__/drag-in-the-blank.test.js +111 -0
- package/src/__tests__/index.test.js +39 -0
- package/src/__tests__/mask.test.js +187 -0
- package/src/__tests__/serialization.test.js +54 -0
- package/src/__tests__/utils.js +1 -0
- package/src/__tests__/with-mask.test.js +76 -0
- package/src/choices/__tests__/index.test.js +75 -0
- package/src/choices/choice.jsx +83 -96
- package/src/choices/index.jsx +11 -5
- package/src/components/__tests__/blank.test.js +138 -0
- package/src/components/__tests__/correct-input.test.js +90 -0
- package/src/components/__tests__/dropdown.test.js +93 -0
- package/src/components/__tests__/input.test.js +102 -0
- package/src/components/blank.jsx +316 -204
- package/src/components/correct-input.jsx +37 -38
- package/src/components/dropdown.jsx +371 -125
- package/src/constructed-response.jsx +80 -18
- package/src/customizable.jsx +35 -0
- package/src/drag-in-the-blank.jsx +152 -40
- package/src/index.js +10 -1
- package/src/inline-dropdown.jsx +2 -0
- package/src/mask.jsx +71 -25
- package/src/serialization.js +22 -34
- package/src/with-mask.jsx +43 -3
- package/README.md +0 -14
- package/lib/new-serialization.js +0 -267
- package/lib/new-serialization.js.map +0 -1
- package/lib/parse-html.js +0 -17
- package/lib/parse-html.js.map +0 -1
- package/lib/test-serializer.js +0 -164
- package/lib/test-serializer.js.map +0 -1
- package/src/new-serialization.jsx +0 -291
- package/src/parse-html.js +0 -8
- package/src/test-serializer.js +0 -163
package/src/choices/choice.jsx
CHANGED
|
@@ -1,110 +1,97 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useEffect, useRef } from 'react';
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import Chip from '@material
|
|
6
|
-
import classnames from 'classnames';
|
|
7
|
-
import ReactDOM from 'react-dom';
|
|
3
|
+
import { useDraggable } from '@dnd-kit/core';
|
|
4
|
+
import { styled } from '@mui/material/styles';
|
|
5
|
+
import Chip from '@mui/material/Chip';
|
|
8
6
|
import { renderMath } from '@pie-lib/math-rendering';
|
|
9
7
|
import { color } from '@pie-lib/render-ui';
|
|
10
8
|
|
|
11
9
|
export const DRAG_TYPE = 'MaskBlank';
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
11
|
+
const StyledChoice = styled('span')(({ theme, disabled }) => ({
|
|
12
|
+
border: `solid 0px ${theme.palette.primary.main}`,
|
|
13
|
+
borderRadius: theme.spacing(2),
|
|
14
|
+
margin: theme.spacing(0.5),
|
|
15
|
+
transform: 'translate(0, 0)',
|
|
16
|
+
display: 'inline-flex',
|
|
17
|
+
...(disabled && {}),
|
|
18
|
+
}));
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
20
|
+
const StyledChip = styled(Chip)(() => ({
|
|
21
|
+
backgroundColor: color.white(),
|
|
22
|
+
border: `1px solid ${color.text()}`,
|
|
23
|
+
color: color.text(),
|
|
24
|
+
alignItems: 'center',
|
|
25
|
+
display: 'inline-flex',
|
|
26
|
+
height: 'initial',
|
|
27
|
+
minHeight: '32px',
|
|
28
|
+
fontSize: 'inherit',
|
|
29
|
+
whiteSpace: 'pre-wrap',
|
|
30
|
+
maxWidth: '374px',
|
|
31
|
+
// Added for touch devices, for image content.
|
|
32
|
+
// This will prevent the context menu from appearing and not allowing other interactions with the image.
|
|
33
|
+
// If interactions with the image in the token will be requested we should handle only the context Menu.
|
|
34
|
+
pointerEvents: 'none',
|
|
35
|
+
borderRadius: '3px',
|
|
36
|
+
paddingTop: '12px',
|
|
37
|
+
paddingBottom: '12px',
|
|
24
38
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
// TODO the Chip element is causing drag problems on touch devices. Avoid using Chip and consider refactoring the code. Keep in mind that Chip is a span with a button role, which interferes with seamless touch device dragging.
|
|
29
|
-
|
|
30
|
-
return connectDragSource(
|
|
31
|
-
<span className={classnames(classes.choice, disabled && classes.disabled)}>
|
|
32
|
-
<Chip
|
|
33
|
-
clickable={false}
|
|
34
|
-
disabled={true}
|
|
35
|
-
ref={(ref) => {
|
|
36
|
-
//eslint-disable-next-line
|
|
37
|
-
this.rootRef = ReactDOM.findDOMNode(ref);
|
|
38
|
-
}}
|
|
39
|
-
className={classes.chip}
|
|
40
|
-
label={
|
|
41
|
-
<span
|
|
42
|
-
className={classes.chipLabel}
|
|
43
|
-
ref={(ref) => {
|
|
44
|
-
if (ref) {
|
|
45
|
-
ref.innerHTML = choice.value || ' ';
|
|
46
|
-
}
|
|
47
|
-
}}
|
|
48
|
-
>
|
|
49
|
-
{' '}
|
|
50
|
-
</span>
|
|
51
|
-
}
|
|
52
|
-
variant={disabled ? 'outlined' : undefined}
|
|
53
|
-
/>
|
|
54
|
-
</span>,
|
|
55
|
-
{},
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export const BlankContent = withStyles((theme) => ({
|
|
61
|
-
choice: {
|
|
62
|
-
border: `solid 0px ${theme.palette.primary.main}`,
|
|
63
|
-
borderRadius: theme.spacing.unit * 2,
|
|
64
|
-
margin: theme.spacing.unit / 2,
|
|
65
|
-
transform: 'translate(0, 0)',
|
|
66
|
-
},
|
|
67
|
-
chip: {
|
|
68
|
-
backgroundColor: color.background(),
|
|
69
|
-
border: `1px solid ${color.text()}`,
|
|
70
|
-
color: color.text(),
|
|
71
|
-
alignItems: 'center',
|
|
72
|
-
display: 'inline-flex',
|
|
73
|
-
height: 'initial',
|
|
74
|
-
minHeight: '32px',
|
|
75
|
-
fontSize: 'inherit',
|
|
76
|
-
whiteSpace: 'pre-wrap',
|
|
77
|
-
maxWidth: '374px',
|
|
78
|
-
// Added for touch devices, for image content.
|
|
79
|
-
// This will prevent the context menu from appearing and not allowing other interactions with the image.
|
|
80
|
-
// If interactions with the image in the token will be requested we should handle only the context Menu.
|
|
81
|
-
pointerEvents: 'none',
|
|
82
|
-
},
|
|
83
|
-
chipLabel: {
|
|
84
|
-
whiteSpace: 'pre-wrap',
|
|
85
|
-
'& img': {
|
|
86
|
-
display: 'block',
|
|
87
|
-
padding: '2px 0',
|
|
88
|
-
},
|
|
39
|
+
'&.Mui-disabled': {
|
|
40
|
+
opacity: 1,
|
|
89
41
|
},
|
|
90
|
-
|
|
91
|
-
}))(BlankContentComp);
|
|
42
|
+
}));
|
|
92
43
|
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
44
|
+
const StyledChipLabel = styled('span')(() => ({
|
|
45
|
+
whiteSpace: 'normal',
|
|
46
|
+
'& img': {
|
|
47
|
+
display: 'block',
|
|
48
|
+
padding: '2px 0',
|
|
96
49
|
},
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
choice: props.choice,
|
|
100
|
-
instanceId: props.instanceId,
|
|
101
|
-
};
|
|
50
|
+
'& mjx-frac': {
|
|
51
|
+
fontSize: '120% !important',
|
|
102
52
|
},
|
|
103
|
-
};
|
|
53
|
+
}));
|
|
54
|
+
|
|
55
|
+
export default function Choice({ choice, disabled, instanceId }) {
|
|
56
|
+
const rootRef = useRef(null);
|
|
57
|
+
|
|
58
|
+
const { attributes, listeners, setNodeRef, isDragging } = useDraggable({
|
|
59
|
+
id: `choice-${choice.id}`,
|
|
60
|
+
data: { choice, instanceId, fromChoice: true, type: DRAG_TYPE },
|
|
61
|
+
disabled,
|
|
62
|
+
});
|
|
104
63
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}))(BlankContent);
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
renderMath(rootRef.current);
|
|
66
|
+
}, [choice.value]);
|
|
109
67
|
|
|
110
|
-
|
|
68
|
+
return (
|
|
69
|
+
<StyledChoice
|
|
70
|
+
ref={setNodeRef}
|
|
71
|
+
style={
|
|
72
|
+
isDragging
|
|
73
|
+
? {
|
|
74
|
+
width: rootRef.current?.offsetWidth,
|
|
75
|
+
height: rootRef.current?.offsetHeight,
|
|
76
|
+
}
|
|
77
|
+
: {}
|
|
78
|
+
}
|
|
79
|
+
disabled={disabled}
|
|
80
|
+
{...listeners}
|
|
81
|
+
{...attributes}
|
|
82
|
+
>
|
|
83
|
+
<StyledChip
|
|
84
|
+
clickable={false}
|
|
85
|
+
disabled={disabled}
|
|
86
|
+
ref={rootRef}
|
|
87
|
+
label={<StyledChipLabel dangerouslySetInnerHTML={{ __html: choice.value }} />}
|
|
88
|
+
/>
|
|
89
|
+
</StyledChoice>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
Choice.propTypes = {
|
|
94
|
+
choice: PropTypes.object.isRequired,
|
|
95
|
+
disabled: PropTypes.bool,
|
|
96
|
+
instanceId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
97
|
+
};
|
package/src/choices/index.jsx
CHANGED
|
@@ -2,7 +2,7 @@ import React from 'react';
|
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
3
|
import findKey from 'lodash/findKey';
|
|
4
4
|
import Choice from './choice';
|
|
5
|
-
import DragDroppablePlaceholder from '@pie-lib/drag
|
|
5
|
+
import { DragDroppablePlaceholder } from '@pie-lib/drag';
|
|
6
6
|
|
|
7
7
|
export default class Choices extends React.Component {
|
|
8
8
|
static propTypes = {
|
|
@@ -11,6 +11,7 @@ export default class Choices extends React.Component {
|
|
|
11
11
|
choices: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, value: PropTypes.string })),
|
|
12
12
|
value: PropTypes.object,
|
|
13
13
|
choicePosition: PropTypes.string.isRequired,
|
|
14
|
+
instanceId: PropTypes.string, // Added for drag isolation
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
getStyleForWrapper = () => {
|
|
@@ -40,7 +41,7 @@ export default class Choices extends React.Component {
|
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
render() {
|
|
43
|
-
const { disabled, duplicates, choices, value } = this.props;
|
|
44
|
+
const { disabled, duplicates, choices, value, instanceId } = this.props;
|
|
44
45
|
const filteredChoices = choices.filter((c) => {
|
|
45
46
|
if (duplicates === true) {
|
|
46
47
|
return true;
|
|
@@ -48,13 +49,18 @@ export default class Choices extends React.Component {
|
|
|
48
49
|
const foundChoice = findKey(value, (v) => v === c.id);
|
|
49
50
|
return foundChoice === undefined;
|
|
50
51
|
});
|
|
51
|
-
const elementStyle = this.getStyleForWrapper();
|
|
52
|
+
const elementStyle = { ...this.getStyleForWrapper(), minWidth: '100px' };
|
|
52
53
|
|
|
53
54
|
return (
|
|
54
55
|
<div style={elementStyle}>
|
|
55
|
-
<DragDroppablePlaceholder disabled={disabled}>
|
|
56
|
+
<DragDroppablePlaceholder disabled={disabled} instanceId={instanceId}>
|
|
56
57
|
{filteredChoices.map((c, index) => (
|
|
57
|
-
<Choice
|
|
58
|
+
<Choice
|
|
59
|
+
key={`${c.value}-${index}`}
|
|
60
|
+
disabled={disabled}
|
|
61
|
+
choice={c}
|
|
62
|
+
instanceId={instanceId}
|
|
63
|
+
/>
|
|
58
64
|
))}
|
|
59
65
|
</DragDroppablePlaceholder>
|
|
60
66
|
</div>
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
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
|
+
}));
|
|
29
|
+
|
|
30
|
+
describe('Blank', () => {
|
|
31
|
+
const onChange = jest.fn();
|
|
32
|
+
const defaultProps = {
|
|
33
|
+
disabled: false,
|
|
34
|
+
choice: { value: 'Cow' },
|
|
35
|
+
isOver: false,
|
|
36
|
+
dragItem: {},
|
|
37
|
+
correct: false,
|
|
38
|
+
onChange,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
onChange.mockClear();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('rendering', () => {
|
|
46
|
+
it('renders with default props', () => {
|
|
47
|
+
const { container } = render(<Blank {...defaultProps} />);
|
|
48
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('displays the value when provided', () => {
|
|
52
|
+
render(<Blank {...defaultProps} />);
|
|
53
|
+
expect(screen.getByText('Cow')).toBeInTheDocument();
|
|
54
|
+
});
|
|
55
|
+
|
|
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();
|
|
60
|
+
});
|
|
61
|
+
|
|
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();
|
|
66
|
+
});
|
|
67
|
+
|
|
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();
|
|
72
|
+
});
|
|
73
|
+
|
|
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();
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
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();
|
|
85
|
+
});
|
|
86
|
+
|
|
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();
|
|
90
|
+
});
|
|
91
|
+
|
|
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
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
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();
|
|
109
|
+
});
|
|
110
|
+
|
|
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
|
|
116
|
+
});
|
|
117
|
+
|
|
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();
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
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();
|
|
130
|
+
});
|
|
131
|
+
|
|
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
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -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,93 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { render, screen } 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('renders as disabled when disabled prop is true', () => {
|
|
48
|
+
render(<Dropdown {...defaultProps} disabled={true} />);
|
|
49
|
+
const button = screen.getByRole('combobox');
|
|
50
|
+
expect(button).toBeDisabled();
|
|
51
|
+
});
|
|
52
|
+
|
|
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();
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
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');
|
|
75
|
+
});
|
|
76
|
+
|
|
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);
|
|
84
|
+
|
|
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);
|
|
89
|
+
|
|
90
|
+
expect(onChange).toHaveBeenCalledWith('1', 'Smiled');
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
@@ -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
|
+
});
|