From 8b44bb356161c8e301d31a7ee9582ea95fe12ab4 Mon Sep 17 00:00:00 2001 From: Irfan Ahmad Date: Tue, 16 Jun 2026 12:52:27 +0500 Subject: [PATCH 1/4] feat: modernize Problem Block JS to ES6+ syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Converts all three capa JavaScript files to ES6+ syntax and removes the legacy imports-loader workarounds, satisfying the acceptance criteria of openedx/public-engineering#438. - display.js: removed CoffeeScript IIFE wrapper; added `import $ from 'jquery'`; replaced 28 prototype method-binding blocks with `.bind(this)`; removed Array.indexOf polyfill in favour of Array.prototype.includes(); converted all var → const/let; template literals throughout; explicit `window.Problem = Problem` + ES6 export. - imageinput.js: rewritten as ES6 class with import/export; const/let; template literals; `window.ImageInput = ImageInput` for compat. - schematic.js: added `import $ from 'jquery'`; module-level var → const; added named + default exports; kept `window.update_schematics` for backwards compatibility. - spec/display_spec.js: added `import Problem` for explicit dependency. - spec/imageinput_spec.js: rewritten to ES6 (import, const/let, template literals, no IIFE wrapper). - webpack.config.js: removed `imports-loader` (this=>window) entries for display.js, imageinput.js, schematic.js; updated babel-loader exclude so these three files are now Babel-processed. Co-Authored-By: Claude Sonnet 4.6 --- .../problem/assets/spec/display_spec.js | 54 +- .../problem/assets/spec/imageinput_spec.js | 151 +- .../problem/assets/static/js/display.js | 2461 ++++++++--------- .../problem/assets/static/js/imageinput.js | 58 +- .../problem/assets/static/js/schematic.js | 9 +- .../problem/assets/webpack.config.js | 29 +- 6 files changed, 1279 insertions(+), 1483 deletions(-) diff --git a/xblocks_contrib/problem/assets/spec/display_spec.js b/xblocks_contrib/problem/assets/spec/display_spec.js index 843ea6a9..9eed2860 100644 --- a/xblocks_contrib/problem/assets/spec/display_spec.js +++ b/xblocks_contrib/problem/assets/spec/display_spec.js @@ -4,9 +4,10 @@ * DS207: Consider shorter variations of null checks * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md */ +import Problem from '../static/js/display.js'; + describe("Problem", function () { const problem_content_default = readFixtures("problem_content.html"); - var mockRuntime = {}; beforeEach(function () { // Stub MathJax @@ -37,7 +38,7 @@ describe("Problem", function () { describe("constructor", function () { it("set the element from html", function () { - this.problem999 = new Problem(mockRuntime,`\ + this.problem999 = new Problem(`\
\
\ }); it("set the element from loadFixtures", function () { - this.problem1 = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem1 = new Problem($(".xblock-student_view")); expect(this.problem1.element_id).toBe("problem_1"); }); }); @@ -59,7 +60,7 @@ data-url='/problem/quiz/'> \ beforeEach(function () { spyOn(window, "update_schematics"); MathJax.Hub.getAllJax.and.returnValue([this.stubbedJax]); - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); }); it("set mathjax typeset", () => expect(MathJax.Hub.Queue).toHaveBeenCalled()); @@ -95,7 +96,7 @@ data-url='/problem/quiz/'> \ beforeEach(function () { spyOn(window, "update_schematics"); MathJax.Hub.getAllJax.and.returnValue([this.stubbedJax]); - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); return $(this).html(readFixtures("problem_content_1240.html")); }); @@ -110,7 +111,7 @@ data-url='/problem/quiz/'> \ describe("renderProgressState", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); }); const testProgessData = function ( @@ -213,7 +214,7 @@ data-url='/problem/quiz/'> \ describe("render", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.bind = this.problem.bind; spyOn(this.problem, "bind"); }); @@ -252,7 +253,7 @@ data-url='/problem/quiz/'> \ beforeEach(function () { // Insert an input of type file outside of the problem. $(".xblock-student_view").after(''); - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); spyOn(this.problem, "submit"); }); @@ -264,7 +265,7 @@ data-url='/problem/quiz/'> \ describe("submit", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.answers = "foo=1&bar=2"; }); @@ -410,7 +411,7 @@ data-url='/problem/quiz/'> \ describe("submit button on problems", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.submitDisabled = (disabled) => { if (disabled) { expect(this.problem.submitButton).toHaveAttr("disabled"); @@ -496,7 +497,7 @@ data-url='/problem/quiz/'> \ describe("reset", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); }); it("log the problem_reset event", function () { @@ -604,7 +605,7 @@ data-url='/problem/quiz/'> \ describe("show problem with column in id", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.el.prepend('
'); }); @@ -637,7 +638,7 @@ data-url='/problem/quiz/'> \ describe("show", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.el.prepend('
'); }); @@ -740,7 +741,7 @@ data-url='/problem/quiz/'> \ }; beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.el.prepend(_.template(imageinput_html)(DEFAULTS)); }); @@ -904,7 +905,7 @@ data-url='/problem/quiz/'> \ describe("save", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.answers = "foo=1&bar=2"; }); @@ -994,7 +995,16 @@ data-url='/problem/quiz/'> \ describe("refreshMath", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); + // Reset Queue spy so that bind()'s Queue call ([fn, null, domEl]) is not + // included when toHaveBeenCalledWith scans recorded calls. In Jasmine 2.99, + // toHaveBeenCalledWith iterates ALL recorded calls' args element-by-element + // (even mismatched ones, for diff output). jasmine-jquery's custom equality + // tester calls $(domEl).is(anyString) when comparing a DOM node against a + // string — which throws a Sizzle syntax error if the string isn't a valid + // CSS selector (e.g. "E=mc^2"). Resetting here isolates this describe to + // testing only what refreshMath itself queues. + MathJax.Hub.Queue.calls.reset(); $("#input_example_1").val("E=mc^2"); this.problem.refreshMath({ target: $("#input_example_1").get(0) }); }); @@ -1009,7 +1019,7 @@ data-url='/problem/quiz/'> \ describe("updateMathML", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.stubbedJax.root.toMathML.and.returnValue(""); }); @@ -1037,7 +1047,7 @@ data-url='/problem/quiz/'> \ describe("refreshAnswers", function () { beforeEach(function () { - this.problem = new Problem(mockRuntime, $(".xblock-student_view")); + this.problem = new Problem($(".xblock-student_view")); this.problem.el.html(`\