From 68bb45ab67dc3dee4215e2c166ff898544099cd9 Mon Sep 17 00:00:00 2001 From: Felix Sun Date: Tue, 20 Aug 2013 14:44:12 -0400 Subject: [PATCH] Added jasmine tests to crowdsource_hinter. UI is not covered; only event-inteception is covered. --- .../js/fixtures/crowdsource_hinter.html | 52 ++++++++++++++++++ .../xmodule/js/spec/capa/display_spec.coffee | 12 ++++- .../crowdsource_hinter/display_spec.coffee | 54 +++++++++++++++++++ .../xmodule/js/src/capa/display.coffee | 1 - .../js/src/crowdsource_hinter/display.coffee | 2 +- 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 common/lib/xmodule/xmodule/js/fixtures/crowdsource_hinter.html create mode 100644 common/lib/xmodule/xmodule/js/spec/crowdsource_hinter/display_spec.coffee diff --git a/common/lib/xmodule/xmodule/js/fixtures/crowdsource_hinter.html b/common/lib/xmodule/xmodule/js/fixtures/crowdsource_hinter.html new file mode 100644 index 0000000000..02122017bd --- /dev/null +++ b/common/lib/xmodule/xmodule/js/fixtures/crowdsource_hinter.html @@ -0,0 +1,52 @@ +
  • + + +
    + + +
    +
    + + +

    + Numerical Input +

    + +
    (1/1 points)
    + +
    +

    The answer is 2*x^2*y + 5 +


    Answer = +
    +
    + + +
    + + + +
    + +
    + + + + +
    +
    +
    + +
    + + + +
    + + + + +
    + + + +
  • \ No newline at end of file diff --git a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee index 33d74e2335..111174a0b4 100644 --- a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee @@ -125,9 +125,10 @@ describe 'Problem', -> expect(@problem.bind).toHaveBeenCalled() describe 'check_fd', -> - xit 'should have specs written for this functionality', -> + xit 'should have more specs written for this functionality', -> expect(false) + describe 'check', -> beforeEach -> @problem = new Problem($('.xmodule_display')) @@ -137,6 +138,15 @@ describe 'Problem', -> @problem.check() expect(Logger.log).toHaveBeenCalledWith 'problem_check', 'foo=1&bar=2' + it 'log the problem_graded event, after the problem is done grading.', -> + spyOn($, 'postWithPrefix').andCallFake (url, answers, callback) -> + response = + success: 'correct' + contents: 'mock grader response' + callback(response) + @problem.check() + expect(Logger.log).toHaveBeenCalledWith 'problem_graded', ['foo=1&bar=2', 'mock grader response'], @problem.url + it 'submit the answer for check', -> spyOn $, 'postWithPrefix' @problem.check() diff --git a/common/lib/xmodule/xmodule/js/spec/crowdsource_hinter/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/crowdsource_hinter/display_spec.coffee new file mode 100644 index 0000000000..917741f8af --- /dev/null +++ b/common/lib/xmodule/xmodule/js/spec/crowdsource_hinter/display_spec.coffee @@ -0,0 +1,54 @@ +describe 'Crowdsourced hinter', -> + beforeEach -> + window.update_schematics = -> + jasmine.stubRequests() + # note that the fixturesPath is set in spec/helper.coffee + loadFixtures 'crowdsource_hinter.html' + @hinter = new Hinter($('#hinter-root')) + + describe 'high-level integration tests', -> + # High-level, happy-path tests for integration with capa problems. + beforeEach -> + # Make a more thorough $.postWithPrefix mock. + spyOn($, 'postWithPrefix').andCallFake( -> + last_argument = arguments[arguments.length - 1] + if typeof last_argument == 'function' + response = + success: 'incorrect' + contents: 'mock grader response' + last_argument(response) + ) + @problem = new Problem($('#problem')) + @problem.bind() + + it 'knows when a capa problem is graded, using check.', -> + @problem.answers = 'test answer' + @problem.check() + expect($.postWithPrefix).toHaveBeenCalledWith("#{@hinter.url}/get_hint", 'test answer', jasmine.any(Function)) + + it 'knows when a capa problem is graded usig check_fd.', -> + spyOn($, 'ajaxWithPrefix').andCallFake((url, settings) -> + response = + success: 'incorrect' + contents: 'mock grader response' + settings.success(response) + ) + @problem.answers = 'test answer' + @problem.check_fd() + expect($.postWithPrefix).toHaveBeenCalledWith("#{@hinter.url}/get_hint", 'test answer', jasmine.any(Function)) + + describe 'capture_problem', -> + beforeEach -> + spyOn($, 'postWithPrefix').andReturn(null) + + it 'gets hints for an incorrect answer', -> + data = ['some answers', ''] + @hinter.capture_problem('problem_graded', data, 'fake element') + expect($.postWithPrefix).toHaveBeenCalledWith("#{@hinter.url}/get_hint", 'some answers', jasmine.any(Function)) + + it 'gets feedback for a correct answer', -> + data = ['some answers', ''] + @hinter.capture_problem('problem_graded', data, 'fake element') + expect($.postWithPrefix).toHaveBeenCalledWith("#{@hinter.url}/get_feedback", 'some answers', jasmine.any(Function)) + + diff --git a/common/lib/xmodule/xmodule/js/src/capa/display.coffee b/common/lib/xmodule/xmodule/js/src/capa/display.coffee index ab64617644..61df101d08 100644 --- a/common/lib/xmodule/xmodule/js/src/capa/display.coffee +++ b/common/lib/xmodule/xmodule/js/src/capa/display.coffee @@ -19,7 +19,6 @@ class @Problem problem_prefix = @element_id.replace(/problem_/,'') @inputs = @$("[id^=input_#{problem_prefix}_]") - @$('section.action input:button').click @refreshAnswers @$('section.action input.check').click @check_fd @$('section.action input.reset').click @reset diff --git a/common/lib/xmodule/xmodule/js/src/crowdsource_hinter/display.coffee b/common/lib/xmodule/xmodule/js/src/crowdsource_hinter/display.coffee index 2e99b107f9..c53a2e2066 100644 --- a/common/lib/xmodule/xmodule/js/src/crowdsource_hinter/display.coffee +++ b/common/lib/xmodule/xmodule/js/src/crowdsource_hinter/display.coffee @@ -96,7 +96,7 @@ class @Hinter $(obj).css('margin-top', (viewbox_height - view_height) + 'px') ) - render: (content) => + render: (content) -> if content # Trim leading and trailing whitespace content = content.replace /^\s+|\s+$/g, ""