@pie-lib/mask-markup 2.0.0-beta.2 → 2.1.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/__tests__/drag-in-the-blank.test.js +129 -0
- package/lib/__tests__/index.test.js +42 -0
- package/lib/__tests__/mask.test.js +163 -0
- package/lib/__tests__/serialization.test.js +44 -0
- package/lib/__tests__/utils.js +14 -0
- package/lib/__tests__/with-mask.test.js +110 -0
- package/lib/choices/__tests__/index.test.js +101 -0
- package/lib/choices/choice.js +100 -119
- package/lib/choices/choice.js.map +1 -1
- package/lib/choices/index.js +24 -20
- package/lib/choices/index.js.map +1 -1
- package/lib/componentize.js +2 -3
- package/lib/componentize.js.map +1 -1
- package/lib/components/__tests__/blank.test.js +189 -0
- package/lib/components/__tests__/correct-input.test.js +132 -0
- package/lib/components/__tests__/dropdown.test.js +134 -0
- package/lib/components/__tests__/input.test.js +129 -0
- package/lib/components/blank.js +316 -222
- package/lib/components/blank.js.map +1 -1
- package/lib/components/correct-input.js +40 -43
- package/lib/components/correct-input.js.map +1 -1
- package/lib/components/dropdown.js +394 -125
- package/lib/components/dropdown.js.map +1 -1
- package/lib/components/input.js +2 -3
- package/lib/components/input.js.map +1 -1
- package/lib/constructed-response.js +83 -27
- 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 +155 -62
- package/lib/drag-in-the-blank.js.map +1 -1
- package/lib/index.js +8 -1
- package/lib/index.js.map +1 -1
- package/lib/inline-dropdown.js +5 -4
- package/lib/inline-dropdown.js.map +1 -1
- package/lib/mask.js +90 -57
- package/lib/mask.js.map +1 -1
- package/lib/serialization.js +31 -43
- package/lib/serialization.js.map +1 -1
- package/lib/with-mask.js +49 -21
- package/lib/with-mask.js.map +1 -1
- package/package.json +18 -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 +71 -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
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
4
|
+
import { withMask } from '../with-mask';
|
|
5
|
+
|
|
6
|
+
describe('WithMask', () => {
|
|
7
|
+
const onChange = jest.fn();
|
|
8
|
+
const defaultProps = {
|
|
9
|
+
markup: '<p>Foo bar {{0}} over the moon;</p>',
|
|
10
|
+
value: {
|
|
11
|
+
0: 'blank',
|
|
12
|
+
},
|
|
13
|
+
onChange,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const Masked = withMask('foo', (props) => (node) => {
|
|
17
|
+
const dataset = node.data ? node.data.dataset || {} : {};
|
|
18
|
+
|
|
19
|
+
if (dataset.component === 'foo') {
|
|
20
|
+
return <input type="text" data-testid="masked-input" defaultValue="Foo" onChange={props.onChange} />;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
beforeEach(() => {
|
|
25
|
+
onChange.mockClear();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('rendering', () => {
|
|
29
|
+
it('renders with default props', () => {
|
|
30
|
+
const { container } = render(<Masked {...defaultProps} />);
|
|
31
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('renders markup content', () => {
|
|
35
|
+
render(<Masked {...defaultProps} />);
|
|
36
|
+
expect(screen.getByText(/Foo bar/)).toBeInTheDocument();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('renders paragraph content', () => {
|
|
40
|
+
const { container } = render(<Masked {...defaultProps} />);
|
|
41
|
+
// Paragraph is rendered as a styled div, not a <p> tag
|
|
42
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
43
|
+
expect(screen.getByText(/Foo bar/)).toBeInTheDocument();
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe('onChange handler', () => {
|
|
48
|
+
it('calls onChange when value changes', async () => {
|
|
49
|
+
const user = userEvent.setup();
|
|
50
|
+
render(<Masked {...defaultProps} />);
|
|
51
|
+
|
|
52
|
+
const input = screen.queryByTestId('masked-input');
|
|
53
|
+
if (input) {
|
|
54
|
+
await user.clear(input);
|
|
55
|
+
await user.type(input, 'ceva');
|
|
56
|
+
|
|
57
|
+
expect(onChange).toHaveBeenCalled();
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('passes event to onChange', async () => {
|
|
62
|
+
const user = userEvent.setup();
|
|
63
|
+
render(<Masked {...defaultProps} />);
|
|
64
|
+
|
|
65
|
+
const input = screen.queryByTestId('masked-input');
|
|
66
|
+
if (input) {
|
|
67
|
+
await user.clear(input);
|
|
68
|
+
await user.type(input, 'test');
|
|
69
|
+
|
|
70
|
+
expect(onChange).toHaveBeenCalled();
|
|
71
|
+
const lastCall = onChange.mock.calls[onChange.mock.calls.length - 1][0];
|
|
72
|
+
expect(lastCall).toHaveProperty('target');
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { render, screen } from '@testing-library/react';
|
|
3
|
+
import Choice from '../choice';
|
|
4
|
+
import { choice } from '../../__tests__/utils';
|
|
5
|
+
import Choices from '../index';
|
|
6
|
+
|
|
7
|
+
// Mock @dnd-kit hooks to avoid DndContext requirement
|
|
8
|
+
jest.mock('@dnd-kit/core', () => ({
|
|
9
|
+
useDraggable: jest.fn(() => ({
|
|
10
|
+
attributes: {},
|
|
11
|
+
listeners: {},
|
|
12
|
+
setNodeRef: jest.fn(),
|
|
13
|
+
isDragging: false,
|
|
14
|
+
})),
|
|
15
|
+
useDroppable: jest.fn(() => ({
|
|
16
|
+
setNodeRef: jest.fn(),
|
|
17
|
+
isOver: false,
|
|
18
|
+
active: null,
|
|
19
|
+
})),
|
|
20
|
+
}));
|
|
21
|
+
|
|
22
|
+
describe('index', () => {
|
|
23
|
+
describe('Choices', () => {
|
|
24
|
+
const defaultProps = {
|
|
25
|
+
disabled: false,
|
|
26
|
+
choices: [choice('Jumped', '0'), choice('Laughed', '1'), choice('Spoon', '2')],
|
|
27
|
+
choicePosition: 'below',
|
|
28
|
+
instanceId: 'test-instance',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
it('renders correctly with default props', () => {
|
|
32
|
+
const { container } = render(<Choices {...defaultProps} />);
|
|
33
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
34
|
+
expect(screen.getByText('Jumped')).toBeInTheDocument();
|
|
35
|
+
expect(screen.getByText('Laughed')).toBeInTheDocument();
|
|
36
|
+
expect(screen.getByText('Spoon')).toBeInTheDocument();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('renders correctly with disabled prop as true', () => {
|
|
40
|
+
const { container } = render(<Choices {...defaultProps} disabled={true} />);
|
|
41
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('renders without duplicates', () => {
|
|
45
|
+
const { container } = render(<Choices {...defaultProps} duplicates={undefined} value={{ 0: '0', 1: '1' }} />);
|
|
46
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('renders with duplicates', () => {
|
|
50
|
+
const { container } = render(<Choices {...defaultProps} duplicates={true} value={{ 0: '0', 1: '1' }} />);
|
|
51
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('Choice', () => {
|
|
56
|
+
const defaultProps = {
|
|
57
|
+
disabled: false,
|
|
58
|
+
choice: choice('Label', '1'),
|
|
59
|
+
instanceId: 'test-instance',
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
describe('render', () => {
|
|
63
|
+
it('renders correctly with default props', () => {
|
|
64
|
+
const { container } = render(<Choice {...defaultProps} />);
|
|
65
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
66
|
+
expect(screen.getByText('Label')).toBeInTheDocument();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('renders correctly with disabled prop as true', () => {
|
|
70
|
+
const { container } = render(<Choice {...defaultProps} disabled={true} />);
|
|
71
|
+
expect(container.firstChild).toBeInTheDocument();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
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
|
+
});
|