Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/xblock_problem/assets/spec/display_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,58 @@ data-url='/problem/quiz/'> \
});
});

// Ported from openedx-platform xmodule/js/spec/capa/display_spec.js (added in the MathJax v4
// upgrade, openedx/openedx-platform#33555), which openedx/openedx-platform#38751 removes along with
// the built-in Problem block. Names, bodies and assertions are unchanged except for the
// Problem(runtime, element) constructor used by the extracted block.
describe("refreshMath", function () {
beforeEach(function () {
this.problem = new Problem(mockRuntime, $(".xblock-student_view"));
MathJax.typesetClear.calls.reset();
MathJax.typesetPromise.calls.reset();
MathJax.startup.document.getMathItemsWithin.calls.reset();
MathJax.startup.document.getMathItemsWithin.and.returnValue([this.stubbedJax]);
$("#input_example_1").val("E=mc^2");
this.problem.refreshMath({ target: $("#input_example_1").get(0) });
});

it("should clear and typeset the math element", function () {
var math = document.querySelector("#display_example_1");
expect(MathJax.typesetClear).toHaveBeenCalledWith([math]);
expect(MathJax.typesetPromise).toHaveBeenCalledWith([math]);
});

it("wraps calculator input in backticks for input.math", function () {
var math = document.querySelector("#display_example_1");
expect(math.textContent).toBe("`E=mc^2`");
});
});

describe("refreshMath formula and delimiter handling", function () {
var formulaHTML = '' +
'<div id="formulaequationinput_test" class="inputtype formulaequationinput">' +
' <input type="text" id="input_test_fe" data-input-id="test_fe" value="">' +
' <div id="input_test_fe_preview" class="equation"></div>' +
'</div>';

beforeEach(function () {
this.problem = new Problem(mockRuntime, $(".xblock-student_view"));
MathJax.typesetClear.calls.reset();
MathJax.typesetPromise.calls.reset();
MathJax.startup.document.getMathItemsWithin.calls.reset();
MathJax.startup.document.getMathItemsWithin.and.returnValue([this.stubbedJax]);
this.problem.el.find(".problem").prepend(formulaHTML);
});

it("clears preview and skips typesetPromise on empty input", function () {
$("#input_test_fe").val("");
this.problem.refreshMath({ target: $("#input_test_fe").get(0) });
var math = document.querySelector("#input_test_fe_preview");
expect(math.textContent).toBe("");
expect(MathJax.typesetPromise).not.toHaveBeenCalled();
});
});

describe("updateMathML", function () {
beforeEach(function () {
this.problem = new Problem(mockRuntime, $(".xblock-student_view"));
Expand Down
43 changes: 43 additions & 0 deletions src/xblock_problem/capa/tests/test_inputtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,49 @@ def test_ajax_other_err(self, mock_log):
assert "error" in response
assert response["error"] == "Error while rendering preview"

# The tests below replace tests that openedx/openedx-platform#33555 (MathJax v4) added to
# xmodule/js/spec/capa/display_spec.js for the built-in block's client-side ``refreshMath``
# formula-equation preview. openedx/openedx-platform#38751 removes that code. The extracted block
# renders formula-equation previews only through the server (``preview_formcalc``, used by
# formula_equation_preview.js; see 989ae32), so these assert that path's behavior instead.

def _preview(self, formula):
"""Request a server-rendered preview for ``formula``."""
return self.the_input.handle_ajax("preview_formcalc", {"formula": formula, "request_start": 1})

def test_formcalc_ajax_calculator_syntax_renders_as_latex(self):
"""
Calculator syntax is rendered as LaTeX by the server.

Replaces platform display_spec "wraps calculator syntax in backticks for formulaequationinput".
"""
response = self._preview("R_1*R_2/R_3")
assert response["error"] == ""
assert response["preview"] == "\\frac{R_{1}\\cdot R_{2}}{R_{3}}"

def _assert_delimited_input_rejected(self, formula):
"""TeX-delimited input is not valid calculator syntax, so the server reports a parse error."""
response = self._preview(formula)
assert response["preview"] == ""
assert response["error"] == "Sorry, couldn't parse formula"
assert response["formula"] == formula

def test_formcalc_ajax_inline_tex_delimiters_rejected(self):
"""Replaces platform display_spec "preserves explicit TeX delimiters \\(...\\)"."""
self._assert_delimited_input_rejected("\\(E=mc^2\\)")

def test_formcalc_ajax_display_tex_delimiters_rejected(self):
"""Replaces platform display_spec "preserves display math TeX delimiters \\[\\]"."""
self._assert_delimited_input_rejected("\\[E=mc^2\\]")

def test_formcalc_ajax_double_dollar_delimiters_rejected(self):
"""Replaces platform display_spec "preserves double-dollar delimiters"."""
self._assert_delimited_input_rejected("$$E=mc^2$$")

def test_formcalc_ajax_single_dollar_delimiters_rejected(self):
"""Replaces platform display_spec "preserves single-dollar delimiters"."""
self._assert_delimited_input_rejected("$E=mc^2$")


class DragAndDropTest(unittest.TestCase):
"""
Expand Down
Loading