-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrender-math.test.js
More file actions
166 lines (138 loc) · 4.27 KB
/
Copy pathrender-math.test.js
File metadata and controls
166 lines (138 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React from 'react';
import { render } from '@testing-library/react';
import { TeX } from 'mathjax-full/js/input/tex';
import renderMath, { fixMathElement } from '../render-math';
import { times } from 'lodash-es';
jest.mock(
'mathjax-full/js/mathjax',
() => ({
mathjax: {
document: jest.fn().mockReturnThis(),
findMath: jest.fn().mockReturnThis(),
compile: jest.fn().mockReturnThis(),
getMetrics: jest.fn().mockReturnThis(),
typeset: jest.fn().mockReturnThis(),
updateDocument: jest.fn().mockReturnThis(),
clear: jest.fn().mockReturnThis(),
handlers: {
handlesDocument: jest.fn().mockReturnThis(),
},
handleRetriesFor: jest.fn().mockImplementation((callback) => callback()),
},
}),
{
virtual: false,
},
);
jest.mock('mathjax-full/js/input/mathml', () => {
const mock = jest.fn().mockReturnThis();
mock.setMmlFactory = jest.fn();
return {
MathML: () => mock,
};
});
jest.mock('mathjax-full/js/input/tex', () => ({
TeX: jest.fn(),
}));
jest.mock('mathjax-full/js/core/MmlTree/MmlFactory', () => {
const instance = {
setMmlFactory: jest.fn(),
defaultNodes: {},
};
return {
MmlFactory: () => instance,
};
});
const mockMathInstance = {
document: jest.fn().mockReturnThis(),
findMath: jest.fn().mockReturnThis(),
compile: jest.fn().mockReturnThis(),
enrich: jest.fn().mockReturnThis(),
addMenu: jest.fn().mockReturnThis(),
attachSpeech: jest.fn().mockReturnThis(),
assistiveMml: jest.fn().mockReturnThis(),
getMetrics: jest.fn().mockReturnThis(),
typeset: jest.fn().mockReturnThis(),
updateDocument: jest.fn().mockReturnValue({
math: {
list: undefined,
},
clear: jest.fn().mockReturnThis(),
}),
clear: jest.fn().mockReturnThis(),
handlers: {
handlesDocument: jest.fn().mockReturnThis(),
},
handleRetriesFor: jest.fn().mockImplementation((callback) => callback()),
};
const mockHtml = {
findMath: jest.fn().mockReturnValue(mockMathInstance),
};
const mockEnrichHandlerInstance = {
create: jest.fn().mockImplementation(() => mockHtml),
};
jest.mock('mathjax-full/js/a11y/semantic-enrich', () => {
return {
EnrichHandler: () => mockEnrichHandlerInstance,
};
});
jest.mock('mathjax-full/js/a11y/assistive-mml', () => {
return {
AssistiveMmlHandler: () => {},
};
});
jest.mock('mathjax-full/js/ui/menu/MenuHandler', () => {
return {
MenuHandler: () => {},
};
});
jest.mock('mathjax-full/js/output/chtml', () => ({
CHTML: jest.fn(),
}));
jest.mock('mathjax-full/js/adaptors/browserAdaptor', () => ({
browserAdaptor: jest.fn(),
}));
jest.mock('mathjax-full/js/handlers/html', () => ({
RegisterHTMLHandler: jest.fn(),
}));
jest.mock('mathjax-full/js/core/MmlTree/SerializedMmlVisitor', () => ({
SerializedMmlVisitor: jest.fn(),
}));
describe('render-math', () => {
it('calls classFactory.create once', () => {
const div = document.createElement('div');
times(10).forEach((i) => renderMath(div));
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
});
it('calls MathJax render', () => {
const div = document.createElement('div');
renderMath(div);
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
expect(mockHtml.findMath).toHaveBeenCalledWith({ elements: [div] });
});
it('call render math for an array of elements', () => {
const divOne = document.createElement('div');
const divTwo = document.createElement('div');
renderMath([divOne, divTwo]);
expect(mockEnrichHandlerInstance.create).toHaveBeenCalledTimes(1);
expect(mockHtml.findMath).toHaveBeenCalledWith({
elements: [divOne, divTwo],
});
});
it('registers the abs macro in the TeX config', () => {
const div = document.createElement('div');
renderMath(div);
const texConfig = TeX.mock.calls[0][0];
expect(texConfig.macros.abs).toEqual(['\\left|#1\\right|', 1]);
});
it('wraps the math containing element the right way', () => {
const { container } = render(
<div>
<span data-latex="">{'420\\text{ cm}=4.2\\text{ meters}'}</span>
</div>,
);
const spanElem = container.querySelector('span[data-latex]');
fixMathElement(spanElem);
expect(spanElem.textContent).toEqual('\\(420\\text{ cm}=4.2\\text{ meters}\\)');
});
});