@pie-lib/math-input-mathlive 1.1.5-beta.6 → 1.1.5-beta.8
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/lib/__tests__/composites.test.js +255 -0
- package/lib/__tests__/drop-in-compat.test.js +133 -0
- package/lib/__tests__/embedded-fonts.test.js +121 -0
- package/lib/__tests__/index.test.js +118 -0
- package/lib/__tests__/input-imperative.test.js +155 -0
- package/lib/__tests__/input-selection.test.js +103 -0
- package/lib/__tests__/keypad-label.test.js +231 -0
- package/lib/__tests__/keypad-render.test.js +206 -0
- package/lib/__tests__/latex-bridge.test.js +386 -0
- package/lib/__tests__/mathlive-instance.test.js +419 -0
- package/lib/__tests__/mount-race.test.js +218 -0
- package/lib/__tests__/selection.test.js +195 -0
- package/lib/__tests__/static-prompts.test.js +405 -0
- package/lib/__tests__/tmp-markup.test.js +26 -0
- package/lib/latex-bridge.js +17 -3
- package/lib/latex-bridge.js.map +1 -1
- package/lib/mf/input.js +9 -1
- package/lib/mf/input.js.map +1 -1
- package/lib/selection.js +148 -0
- package/lib/selection.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/composites.test.jsx +220 -0
- package/src/__tests__/drop-in-compat.test.jsx +112 -0
- package/src/__tests__/index.test.jsx +109 -0
- package/src/__tests__/input-imperative.test.jsx +144 -0
- package/src/__tests__/input-selection.test.jsx +64 -0
- package/src/__tests__/keypad-label.test.jsx +283 -0
- package/src/__tests__/keypad-render.test.jsx +123 -0
- package/src/__tests__/latex-bridge.test.js +359 -0
- package/src/__tests__/mathlive-instance.test.js +418 -0
- package/src/__tests__/mount-race.test.jsx +142 -0
- package/src/__tests__/selection.test.js +212 -0
- package/src/__tests__/static-prompts.test.jsx +364 -0
- package/src/latex-bridge.js +17 -2
- package/src/mf/input.jsx +10 -1
- package/src/selection.js +146 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
|
|
4
|
+
import MathInput from '../math-input';
|
|
5
|
+
import HorizontalKeypad from '../horizontal-keypad';
|
|
6
|
+
import Input from '../mf/input';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The pieces that sit between the keypad and the field: how a pressed key is
|
|
10
|
+
* routed into the field, and the legacy `{value,type}` shape math-toolbar
|
|
11
|
+
* consumes.
|
|
12
|
+
*/
|
|
13
|
+
describe('MathInput.keypadPress routing', () => {
|
|
14
|
+
const withInput = () => {
|
|
15
|
+
const mi = new MathInput({});
|
|
16
|
+
const calls = { write: [], keystroke: [] };
|
|
17
|
+
|
|
18
|
+
mi.input = {
|
|
19
|
+
write: (v) => calls.write.push(v),
|
|
20
|
+
keystroke: (v) => calls.keystroke.push(v),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return { mi, calls };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
it('routes an insert action to write()', () => {
|
|
27
|
+
const { mi, calls } = withInput();
|
|
28
|
+
|
|
29
|
+
mi.keypadPress({ latex: '\\pi' });
|
|
30
|
+
|
|
31
|
+
expect(calls.write).toEqual(['\\pi']);
|
|
32
|
+
expect(calls.keystroke).toEqual([]);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('expands an argument-taking command before writing', () => {
|
|
36
|
+
const { mi, calls } = withInput();
|
|
37
|
+
|
|
38
|
+
mi.keypadPress({ command: '\\frac' });
|
|
39
|
+
|
|
40
|
+
expect(calls.write).toEqual(['\\frac{#?}{#?}']);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('routes a cursor keystroke to keystroke()', () => {
|
|
44
|
+
const { mi, calls } = withInput();
|
|
45
|
+
|
|
46
|
+
mi.keypadPress({ keystroke: 'Left' });
|
|
47
|
+
|
|
48
|
+
expect(calls.keystroke).toEqual(['moveToPreviousChar']);
|
|
49
|
+
expect(calls.write).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('ignores keys with no action', () => {
|
|
53
|
+
const { mi, calls } = withInput();
|
|
54
|
+
|
|
55
|
+
mi.keypadPress({});
|
|
56
|
+
mi.keypadPress({ keystroke: 'Nonsense' });
|
|
57
|
+
|
|
58
|
+
expect(calls.write).toEqual([]);
|
|
59
|
+
expect(calls.keystroke).toEqual([]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('is a no-op before the field ref exists', () => {
|
|
63
|
+
const mi = new MathInput({});
|
|
64
|
+
|
|
65
|
+
expect(() => mi.keypadPress({ latex: '\\pi' })).not.toThrow();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('changeLatex only fires onChange when the value differs', () => {
|
|
69
|
+
const onChange = jest.fn();
|
|
70
|
+
const mi = new MathInput({ latex: 'x', onChange });
|
|
71
|
+
|
|
72
|
+
mi.changeLatex('x');
|
|
73
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
74
|
+
|
|
75
|
+
mi.changeLatex('y');
|
|
76
|
+
expect(onChange).toHaveBeenCalledWith('y');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('tracks focus state', () => {
|
|
80
|
+
const mi = new MathInput({});
|
|
81
|
+
|
|
82
|
+
mi.setState = (s) => Object.assign(mi.state, s);
|
|
83
|
+
mi.inputFocus();
|
|
84
|
+
expect(mi.state.focused).toBe(true);
|
|
85
|
+
|
|
86
|
+
mi.inputBlur();
|
|
87
|
+
expect(mi.state.focused).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('HorizontalKeypad legacy {value,type} model', () => {
|
|
92
|
+
const pressed = (key) => {
|
|
93
|
+
const onClick = jest.fn();
|
|
94
|
+
const hk = new HorizontalKeypad({ onClick });
|
|
95
|
+
|
|
96
|
+
hk.keypadPress(key);
|
|
97
|
+
|
|
98
|
+
return onClick.mock.calls[0][0];
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
it('maps a command to {value, type: command}', () => {
|
|
102
|
+
expect(pressed({ command: '\\frac' })).toEqual({ value: '\\frac', type: 'command' });
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('maps a write to {value}', () => {
|
|
106
|
+
expect(pressed({ write: 'x' })).toEqual({ value: 'x' });
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('maps a keystroke to {type: cursor}', () => {
|
|
110
|
+
expect(pressed({ keystroke: 'Left' })).toEqual({ type: 'cursor', value: 'Left' });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('renders without throwing', () => {
|
|
114
|
+
expect(() => render(<HorizontalKeypad onClick={() => {}} />)).not.toThrow();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('Input: controlled-value sync and events', () => {
|
|
119
|
+
const withField = () => {
|
|
120
|
+
const input = new Input({});
|
|
121
|
+
|
|
122
|
+
input.mathField = {
|
|
123
|
+
value: '',
|
|
124
|
+
getValue: () => input.mathField.value,
|
|
125
|
+
insert(v) {
|
|
126
|
+
input.mathField.value += v;
|
|
127
|
+
},
|
|
128
|
+
executeCommand() {},
|
|
129
|
+
focus() {},
|
|
130
|
+
blur() {},
|
|
131
|
+
addEventListener() {},
|
|
132
|
+
removeEventListener() {},
|
|
133
|
+
remove() {},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
return input;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
it('emits stored-form latex on edit', () => {
|
|
140
|
+
const onChange = jest.fn();
|
|
141
|
+
const input = withField();
|
|
142
|
+
|
|
143
|
+
input.props = { onChange };
|
|
144
|
+
input.mathField.value = '\\longdiv{\\placeholder{}}';
|
|
145
|
+
input.onInputEdit();
|
|
146
|
+
|
|
147
|
+
// \placeholder is MathLive-internal and must not reach the model
|
|
148
|
+
expect(onChange).toHaveBeenCalledWith('\\longdiv{}');
|
|
149
|
+
expect(input.lastEmitted).toEqual('\\longdiv{}');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('does not push a prop echo back into the field', () => {
|
|
153
|
+
const input = withField();
|
|
154
|
+
|
|
155
|
+
input.mathField.value = 'x';
|
|
156
|
+
input.lastEmitted = 'x';
|
|
157
|
+
input.props = { latex: 'x' };
|
|
158
|
+
|
|
159
|
+
input.componentDidUpdate({ latex: '' });
|
|
160
|
+
|
|
161
|
+
expect(input.mathField.value).toEqual('x');
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('pushes a genuinely new latex prop into the field', () => {
|
|
165
|
+
const input = withField();
|
|
166
|
+
|
|
167
|
+
input.mathField.value = 'x';
|
|
168
|
+
input.lastEmitted = 'x';
|
|
169
|
+
input.props = { latex: 'y' };
|
|
170
|
+
|
|
171
|
+
input.componentDidUpdate({ latex: 'x' });
|
|
172
|
+
|
|
173
|
+
expect(input.mathField.value).toEqual('y');
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('bridges stored latex on the way in', () => {
|
|
177
|
+
const input = withField();
|
|
178
|
+
|
|
179
|
+
input.props = { latex: '\\longdiv{5}' };
|
|
180
|
+
input.componentDidUpdate({ latex: '' });
|
|
181
|
+
|
|
182
|
+
// must become native \enclose so the content stays editable
|
|
183
|
+
expect(input.mathField.value).toEqual('\\enclose{longdiv}{5}');
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it('toggles readOnly', () => {
|
|
187
|
+
const input = withField();
|
|
188
|
+
|
|
189
|
+
input.props = { readOnly: true };
|
|
190
|
+
input.componentDidUpdate({ readOnly: false });
|
|
191
|
+
|
|
192
|
+
expect(input.mathField.readOnly).toBe(true);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('clear() empties the field', () => {
|
|
196
|
+
const input = withField();
|
|
197
|
+
|
|
198
|
+
input.mathField.value = 'abc';
|
|
199
|
+
expect(input.clear()).toEqual('');
|
|
200
|
+
expect(input.mathField.value).toEqual('');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('suppresses Enter so it cannot insert a line break', () => {
|
|
204
|
+
const input = withField();
|
|
205
|
+
const preventDefault = jest.fn();
|
|
206
|
+
|
|
207
|
+
input.onKeyDown({ key: 'Enter', preventDefault });
|
|
208
|
+
expect(preventDefault).toHaveBeenCalled();
|
|
209
|
+
|
|
210
|
+
preventDefault.mockClear();
|
|
211
|
+
input.onKeyDown({ key: 'a', preventDefault });
|
|
212
|
+
expect(preventDefault).not.toHaveBeenCalled();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('componentDidUpdate is safe before the field exists', () => {
|
|
216
|
+
const input = new Input({ latex: 'x' });
|
|
217
|
+
|
|
218
|
+
expect(() => input.componentDidUpdate({ latex: '' })).not.toThrow();
|
|
219
|
+
});
|
|
220
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
import { styled } from '@mui/material/styles';
|
|
4
|
+
|
|
5
|
+
import { mq } from '../index';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Exercises the exact call patterns real consumers use against
|
|
9
|
+
* @pie-lib/math-input, to catch drop-in incompatibilities.
|
|
10
|
+
*
|
|
11
|
+
* Sources:
|
|
12
|
+
* - packages/math-toolbar/src/editor-and-pad.jsx:16,185
|
|
13
|
+
* - packages/math-toolbar/src/math-preview.jsx:8,156
|
|
14
|
+
* - packages/editable-html/src/plugins/respArea/math-templated/index.jsx:92
|
|
15
|
+
* - packages/editable-html-tip-tap/src/components/respArea/MathTemplated.jsx:111
|
|
16
|
+
*/
|
|
17
|
+
describe('drop-in compatibility with @pie-lib/math-input consumers', () => {
|
|
18
|
+
describe('mq.CommonMqStyles destructuring', () => {
|
|
19
|
+
it('math-preview.jsx: { commonMqFontStyles, longdivStyles, supsubStyles }', () => {
|
|
20
|
+
const { commonMqFontStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
|
|
21
|
+
|
|
22
|
+
expect(commonMqFontStyles).toBeDefined();
|
|
23
|
+
expect(longdivStyles).toBeDefined();
|
|
24
|
+
expect(supsubStyles).toBeDefined();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('editor-and-pad.jsx: also { commonMqKeyboardStyles }', () => {
|
|
28
|
+
const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
|
|
29
|
+
|
|
30
|
+
expect(commonMqKeyboardStyles).toBeDefined();
|
|
31
|
+
// All four must be spreadable into an emotion style object.
|
|
32
|
+
expect(() => ({
|
|
33
|
+
...commonMqFontStyles,
|
|
34
|
+
...commonMqKeyboardStyles,
|
|
35
|
+
...longdivStyles,
|
|
36
|
+
...supsubStyles,
|
|
37
|
+
})).not.toThrow();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('the destructured groups survive being spread into a styled component', () => {
|
|
41
|
+
const { commonMqFontStyles, longdivStyles, supsubStyles } = mq.CommonMqStyles;
|
|
42
|
+
const Styled = styled('div')({ ...commonMqFontStyles, ...longdivStyles, ...supsubStyles });
|
|
43
|
+
|
|
44
|
+
expect(() => render(<Styled />)).not.toThrow();
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('mq.Input', () => {
|
|
49
|
+
it('editor-and-pad.jsx: styled(mq.Input) renders', () => {
|
|
50
|
+
const MathEditor = styled(mq.Input)(() => ({ width: '100%' }));
|
|
51
|
+
|
|
52
|
+
expect(() => render(<MathEditor latex="\\frac{1}{2}" onChange={() => {}} />)).not.toThrow();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('accepts the className that styled() injects', () => {
|
|
56
|
+
const Styled = styled(mq.Input)({ padding: 4 });
|
|
57
|
+
const { container } = render(<Styled latex="x" onChange={() => {}} />);
|
|
58
|
+
|
|
59
|
+
expect(container.firstChild).toBeTruthy();
|
|
60
|
+
expect(container.firstChild.className).toBeTruthy();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('mq.Static', () => {
|
|
65
|
+
it('math-preview.jsx: <mq.Static latex onFocus onBlur />', () => {
|
|
66
|
+
expect(() => render(<mq.Static latex={'\\frac{1}{2}'} onFocus={() => {}} onBlur={() => {}} />)).not.toThrow();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('math-templated respArea: <mq.Static latex={value} />', () => {
|
|
70
|
+
expect(() => render(<mq.Static latex={'x=1'} />)).not.toThrow();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('renders answer-block latex (the templated-math case) without throwing', () => {
|
|
74
|
+
expect(() =>
|
|
75
|
+
render(<mq.Static latex={'x=\\MathQuillMathField[r1]{}'} onSubFieldChange={() => {}} />),
|
|
76
|
+
).not.toThrow();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('is styleable, as keypad labels were in math-input', () => {
|
|
80
|
+
const Styled = styled(mq.Static)({ pointerEvents: 'none' });
|
|
81
|
+
|
|
82
|
+
expect(() => render(<Styled latex={'\\pi'} />)).not.toThrow();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('surface parity', () => {
|
|
87
|
+
it('exposes exactly the names math-input exported', () => {
|
|
88
|
+
// eslint-disable-next-line global-require
|
|
89
|
+
const pkg = require('../index');
|
|
90
|
+
|
|
91
|
+
[
|
|
92
|
+
'keysForGrade',
|
|
93
|
+
'addBrackets',
|
|
94
|
+
'removeBrackets',
|
|
95
|
+
'keys',
|
|
96
|
+
'HorizontalKeypad',
|
|
97
|
+
'mq',
|
|
98
|
+
'updateSpans',
|
|
99
|
+
'registerEmbed',
|
|
100
|
+
'applyStaticMath',
|
|
101
|
+
].forEach((name) => {
|
|
102
|
+
expect(pkg[name]).toBeDefined();
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('mq exposes Input, Static and CommonMqStyles', () => {
|
|
107
|
+
['Input', 'Static', 'CommonMqStyles'].forEach((name) => {
|
|
108
|
+
expect(mq[name]).toBeDefined();
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render } from '@testing-library/react';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
keys,
|
|
6
|
+
keysForGrade,
|
|
7
|
+
addBrackets,
|
|
8
|
+
removeBrackets,
|
|
9
|
+
registerEmbed,
|
|
10
|
+
applyStaticMath,
|
|
11
|
+
updateSpans,
|
|
12
|
+
mf,
|
|
13
|
+
mq,
|
|
14
|
+
MathInput,
|
|
15
|
+
HorizontalKeypad,
|
|
16
|
+
KeyPad,
|
|
17
|
+
} from '../index';
|
|
18
|
+
|
|
19
|
+
describe('math-input-mathlive public API', () => {
|
|
20
|
+
it('mirrors the @pie-lib/math-input export surface', () => {
|
|
21
|
+
expect(typeof keysForGrade).toBe('function');
|
|
22
|
+
expect(typeof addBrackets).toBe('function');
|
|
23
|
+
expect(typeof removeBrackets).toBe('function');
|
|
24
|
+
expect(typeof registerEmbed).toBe('function');
|
|
25
|
+
expect(typeof applyStaticMath).toBe('function');
|
|
26
|
+
expect(typeof updateSpans).toBe('function');
|
|
27
|
+
expect(keys).toBeDefined();
|
|
28
|
+
expect(keys.baseSet).toBeDefined();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('exports the field wrappers under both mf and mq', () => {
|
|
32
|
+
expect(mf.Input).toBeDefined();
|
|
33
|
+
expect(mf.Static).toBeDefined();
|
|
34
|
+
// `mq` alias lets a consumer swap packages by changing only the import.
|
|
35
|
+
expect(mq).toBe(mf);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('updateSpans is a safe no-op', () => {
|
|
39
|
+
expect(() => updateSpans()).not.toThrow();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('CommonMqStyles keeps the shape consumers destructure', () => {
|
|
43
|
+
// math-toolbar does:
|
|
44
|
+
// const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles }
|
|
45
|
+
// = mq.CommonMqStyles;
|
|
46
|
+
const { commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles } = mf.CommonMqStyles;
|
|
47
|
+
|
|
48
|
+
[commonMqFontStyles, commonMqKeyboardStyles, longdivStyles, supsubStyles].forEach((s) => {
|
|
49
|
+
expect(s).toBeDefined();
|
|
50
|
+
expect(typeof s).toBe('object');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('bracket helpers round-trip', () => {
|
|
55
|
+
expect(addBrackets('x')).toEqual('\\(x\\)');
|
|
56
|
+
expect(removeBrackets('\\(x\\)')).toEqual('x');
|
|
57
|
+
expect(removeBrackets(addBrackets('\\frac{1}{2}'))).toEqual('\\frac{1}{2}');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('keysForGrade returns a 5-row keyset', () => {
|
|
61
|
+
const set = keysForGrade('geometry');
|
|
62
|
+
|
|
63
|
+
expect(Array.isArray(set)).toBe(true);
|
|
64
|
+
expect(set.length).toBe(5);
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('rendering', () => {
|
|
69
|
+
// MathLive is a browser-only custom element; loadMathLive() resolves async and
|
|
70
|
+
// is a no-op-ish under jsdom. These assert the components mount without
|
|
71
|
+
// throwing, which is what regressed most often in the MathQuill version.
|
|
72
|
+
it('KeyPad renders without a live MathLive instance', () => {
|
|
73
|
+
const { container } = render(<KeyPad onPress={() => {}} />);
|
|
74
|
+
|
|
75
|
+
expect(container.querySelectorAll('button').length).toBeGreaterThan(0);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('KeyPad falls back to raw latex for labels before MathLive loads', () => {
|
|
79
|
+
const { container } = render(
|
|
80
|
+
<KeyPad onPress={() => {}} baseSet={[[{ name: 'pi', latex: '\\pi', category: 'constants' }]]} />,
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(container.textContent).toContain('\\pi');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('KeyPad fires onPress with the key definition', () => {
|
|
87
|
+
const onPress = jest.fn();
|
|
88
|
+
const key = { name: 'x', label: 'x', write: 'x', category: 'vars' };
|
|
89
|
+
const { container } = render(<KeyPad onPress={onPress} baseSet={[[key]]} />);
|
|
90
|
+
|
|
91
|
+
container.querySelector('button').click();
|
|
92
|
+
|
|
93
|
+
expect(onPress).toHaveBeenCalledWith(key);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('MathInput mounts', () => {
|
|
97
|
+
expect(() => render(<MathInput latex="\\frac{1}{2}" onChange={() => {}} />)).not.toThrow();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('HorizontalKeypad mounts and reports the legacy {value,type} model', () => {
|
|
101
|
+
const onClick = jest.fn();
|
|
102
|
+
|
|
103
|
+
expect(() => render(<HorizontalKeypad onClick={onClick} />)).not.toThrow();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('Static renders display-mode latex without a mathfield', () => {
|
|
107
|
+
expect(() => render(<mf.Static latex="\\frac{1}{2}" />)).not.toThrow();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { KEYSTROKES, keyToAction } from '../latex-bridge';
|
|
2
|
+
import Input from '../mf/input';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The imperative surface that @pie-lib/math-toolbar drives
|
|
6
|
+
* (editor-and-pad.jsx: clear/command/keystroke/write/latex).
|
|
7
|
+
*
|
|
8
|
+
* These call the methods against a stub mathfield, because a real
|
|
9
|
+
* <math-field> needs a browser. What matters here is the *translation*:
|
|
10
|
+
* MathQuill-era arguments must become valid MathLive operations.
|
|
11
|
+
*/
|
|
12
|
+
const stubField = () => {
|
|
13
|
+
const calls = { insert: [], executeCommand: [], focus: 0 };
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
calls,
|
|
17
|
+
value: '',
|
|
18
|
+
getValue: () => '',
|
|
19
|
+
insert(v) {
|
|
20
|
+
calls.insert.push(v);
|
|
21
|
+
},
|
|
22
|
+
executeCommand(v) {
|
|
23
|
+
calls.executeCommand.push(v);
|
|
24
|
+
},
|
|
25
|
+
focus() {
|
|
26
|
+
calls.focus++;
|
|
27
|
+
},
|
|
28
|
+
blur() {},
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const withField = () => {
|
|
33
|
+
const input = new Input({});
|
|
34
|
+
const f = stubField();
|
|
35
|
+
|
|
36
|
+
input.mathField = f;
|
|
37
|
+
|
|
38
|
+
return { input, f };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
describe('Input imperative API (drives math-toolbar)', () => {
|
|
42
|
+
describe('keystroke', () => {
|
|
43
|
+
it('maps MathQuill keystroke names to MathLive selectors', () => {
|
|
44
|
+
const { input, f } = withField();
|
|
45
|
+
|
|
46
|
+
input.keystroke('Left');
|
|
47
|
+
input.keystroke('Right');
|
|
48
|
+
input.keystroke('Backspace');
|
|
49
|
+
|
|
50
|
+
expect(f.calls.executeCommand).toEqual(['moveToPreviousChar', 'moveToNextChar', 'deleteBackward']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('passes an unknown value through as a selector', () => {
|
|
54
|
+
const { input, f } = withField();
|
|
55
|
+
|
|
56
|
+
input.keystroke('deleteForward');
|
|
57
|
+
|
|
58
|
+
expect(f.calls.executeCommand).toEqual(['deleteForward']);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('does not throw when the engine rejects the selector', () => {
|
|
62
|
+
const { input } = withField();
|
|
63
|
+
|
|
64
|
+
input.mathField.executeCommand = () => {
|
|
65
|
+
throw new Error('unknown command');
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
expect(() => input.keystroke('Nonsense')).not.toThrow();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('covers every keystroke the keypad emits', () => {
|
|
72
|
+
// navigation.js / edit.js only produce these three
|
|
73
|
+
['Left', 'Right', 'Backspace'].forEach((k) => expect(KEYSTROKES[k]).toBeDefined());
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe('command', () => {
|
|
78
|
+
it('expands argument-taking commands into placeholders', () => {
|
|
79
|
+
const { input, f } = withField();
|
|
80
|
+
|
|
81
|
+
input.command('\\frac');
|
|
82
|
+
|
|
83
|
+
// a bare \frac would give no editable slots
|
|
84
|
+
expect(f.calls.insert).toEqual(['\\frac{#?}{#?}']);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('handles array commands', () => {
|
|
88
|
+
const { input, f } = withField();
|
|
89
|
+
|
|
90
|
+
input.command(['\\log', '_']);
|
|
91
|
+
|
|
92
|
+
expect(f.calls.insert).toEqual(['\\log_{#?}']);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('passes argument-less commands through', () => {
|
|
96
|
+
const { input, f } = withField();
|
|
97
|
+
|
|
98
|
+
input.command('\\pi');
|
|
99
|
+
|
|
100
|
+
expect(f.calls.insert).toEqual(['\\pi']);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('agrees with keyToAction', () => {
|
|
104
|
+
expect(keyToAction({ command: '\\sqrt' }).value).toEqual('\\sqrt{#?}');
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe('safety before MathLive loads', () => {
|
|
109
|
+
it('every method is a no-op rather than a crash', () => {
|
|
110
|
+
const input = new Input({});
|
|
111
|
+
|
|
112
|
+
// mathField is undefined until loadMathLive() resolves
|
|
113
|
+
expect(input.latex()).toEqual('');
|
|
114
|
+
expect(input.clear()).toEqual('');
|
|
115
|
+
expect(input.command('\\frac')).toEqual('');
|
|
116
|
+
expect(input.keystroke('Left')).toEqual('');
|
|
117
|
+
expect(input.write('x')).toEqual('');
|
|
118
|
+
expect(() => input.focus()).not.toThrow();
|
|
119
|
+
expect(() => input.blur()).not.toThrow();
|
|
120
|
+
expect(() => input.setLatex('x')).not.toThrow();
|
|
121
|
+
expect(input.element).toBeUndefined();
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe('mqCompat facade', () => {
|
|
126
|
+
// math-toolbar used to call this.input.mathField.latex(); external
|
|
127
|
+
// consumers may still do the same.
|
|
128
|
+
it('exposes latex() as a getter', () => {
|
|
129
|
+
const { input } = withField();
|
|
130
|
+
|
|
131
|
+
input.mathField.getValue = () => '\\frac{1}{2}';
|
|
132
|
+
|
|
133
|
+
expect(input.mqCompat.latex()).toEqual('\\frac{1}{2}');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('exposes latex(v) as a setter and el()', () => {
|
|
137
|
+
const { input, f } = withField();
|
|
138
|
+
|
|
139
|
+
input.mqCompat.latex('x+1');
|
|
140
|
+
expect(f.value).toEqual('x+1');
|
|
141
|
+
expect(input.mqCompat.el()).toBe(f);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The editable Input keeps its selection snapped to whole atoms (see
|
|
3
|
+
* selection.js). This checks the wiring: attached on mount, removed on unmount.
|
|
4
|
+
*/
|
|
5
|
+
jest.mock('../mathlive-instance', () => {
|
|
6
|
+
class FakeMathfieldElement {
|
|
7
|
+
constructor() {
|
|
8
|
+
const el = global.document.createElement('math-field');
|
|
9
|
+
|
|
10
|
+
el.value = '';
|
|
11
|
+
el.getValue = () => el.value;
|
|
12
|
+
|
|
13
|
+
return el;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
loadMathLive: () => Promise.resolve({ MathfieldElement: FakeMathfieldElement }),
|
|
19
|
+
getMacros: () => ({}),
|
|
20
|
+
MATH_MODE_SPACE: '\\ ',
|
|
21
|
+
applyShadowStyles: () => {},
|
|
22
|
+
trackPlaceholderCaret: () => () => {},
|
|
23
|
+
latexToMarkup: (l) => `<span>${l}</span>`,
|
|
24
|
+
};
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const mockUntrack = jest.fn();
|
|
28
|
+
|
|
29
|
+
jest.mock('../selection', () => ({
|
|
30
|
+
trackWholeAtomSelection: jest.fn(() => mockUntrack),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
// eslint-disable-next-line import/first
|
|
34
|
+
import Input from '../mf/input';
|
|
35
|
+
// eslint-disable-next-line import/first
|
|
36
|
+
import { trackWholeAtomSelection } from '../selection';
|
|
37
|
+
|
|
38
|
+
describe('Input: whole-atom selection', () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
trackWholeAtomSelection.mockClear();
|
|
41
|
+
mockUntrack.mockClear();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('attaches the tracker to the mathfield on mount', async () => {
|
|
45
|
+
const input = new Input({});
|
|
46
|
+
|
|
47
|
+
input.holderRef = { current: document.createElement('span') };
|
|
48
|
+
await input.componentDidMount();
|
|
49
|
+
|
|
50
|
+
expect(trackWholeAtomSelection).toHaveBeenCalledTimes(1);
|
|
51
|
+
expect(trackWholeAtomSelection).toHaveBeenCalledWith(input.mathField);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('removes it on unmount', async () => {
|
|
55
|
+
const input = new Input({});
|
|
56
|
+
|
|
57
|
+
input.holderRef = { current: document.createElement('span') };
|
|
58
|
+
await input.componentDidMount();
|
|
59
|
+
input.componentWillUnmount();
|
|
60
|
+
|
|
61
|
+
expect(mockUntrack).toHaveBeenCalledTimes(1);
|
|
62
|
+
expect(input.untrackSelection).toBeUndefined();
|
|
63
|
+
});
|
|
64
|
+
});
|