staff_debug_actions_spec.js 10.2 KB
Newer Older
1
define([
2 3 4
    'backbone',
    'jquery',
    'js/staff_debug_actions',
5
    'edx-ui-toolkit/js/utils/spec-helpers/ajax-helpers'
6 7
],
    function(Backbone, $, tmp, AjaxHelpers) {
8 9
        'use strict';
        var StaffDebug = window.StaffDebug;
10

11
        describe('StaffDebugActions', function() {
12 13
            var location = 'i4x://edX/Open_DemoX/edx_demo_course/problem/test_loc';
            var locationName = 'test_loc';
14 15 16 17
            var usernameFixtureID = 'sd_fu_' + locationName;
            var $usernameFixture = $('<input>', {id: usernameFixtureID, placeholder: 'userman'});
            var scoreFixtureID = 'sd_fs_' + locationName;
            var $scoreFixture = $('<input>', {id: scoreFixtureID, placeholder: '0'});
vkaracic committed
18
            var escapableLocationName = 'test\.\*\+\?\^\:\$\{\}\(\)\|\]\[loc';
19 20
            var escapableFixtureID = 'sd_fu_' + escapableLocationName;
            var $escapableFixture = $('<input>', {id: escapableFixtureID, placeholder: 'userman'});
21
            var esclocationName = 'P2:problem_1';
22
            var escapableId = 'result_' + esclocationName;
Eric Fischer committed
23
            var $escapableResultArea = $('<div>', {id: escapableId});
24

25
            describe('getURL ', function() {
26
                it('defines url to courseware ajax entry point', function() {
27
                    spyOn(StaffDebug, 'getCurrentUrl')
28
                      .and.returnValue('/courses/edX/Open_DemoX/edx_demo_course/courseware/stuff');
29
                    expect(StaffDebug.getURL('rescore_problem'))
30
                      .toBe('/courses/edX/Open_DemoX/edx_demo_course/instructor/api/rescore_problem');
31 32
                });
            });
33

34
            describe('sanitizeString', function() {
35
                it('escapes escapable characters in a string', function() {
36 37
                    expect(StaffDebug.sanitizeString('.*+?^:${}()|]['))
                      .toBe('\\.\\*\\+\\?\\^\\:\\$\\{\\}\\(\\)\\|\\]\\[');
38 39 40
                });
            });

41
            describe('getUser', function() {
42
                it('gets the placeholder username if input field is empty', function() {
43
                    $('body').append($usernameFixture);
44
                    expect(StaffDebug.getUser(locationName)).toBe('userman');
45
                    $('#' + usernameFixtureID).remove();
46
                });
47
                it('gets a filled in name if there is one', function() {
48 49
                    $('body').append($usernameFixture);
                    $('#' + usernameFixtureID).val('notuserman');
50
                    expect(StaffDebug.getUser(locationName)).toBe('notuserman');
51

52 53
                    $('#' + usernameFixtureID).val('');
                    $('#' + usernameFixtureID).remove();
54
                });
vkaracic committed
55
                it('gets the placeholder name if the id has escapable characters', function() {
56 57
                    $('body').append($escapableFixture);
                    expect(StaffDebug.getUser('test.*+?^:${}()|][loc')).toBe('userman');
58
                    $("input[id^='sd_fu_']").remove();
vkaracic committed
59
                });
60
            });
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            describe('getScore', function() {
                it('gets the placeholder score if input field is empty', function() {
                    $('body').append($scoreFixture);
                    expect(StaffDebug.getScore(locationName)).toBe('0');
                    $('#' + scoreFixtureID).remove();
                });
                it('gets a filled in score if there is one', function() {
                    $('body').append($scoreFixture);
                    $('#' + scoreFixtureID).val('1');
                    expect(StaffDebug.getScore(locationName)).toBe('1');

                    $('#' + scoreFixtureID).val('');
                    $('#' + scoreFixtureID).remove();
                });
            });
76
            describe('doInstructorDashAction success', function() {
77
                it('adds a success message to the results element after using an action', function() {
Eric Fischer committed
78
                    $('body').append($escapableResultArea);
79 80 81
                    var requests = AjaxHelpers.requests(this);
                    var action = {
                        locationName: esclocationName,
82
                        success_msg: 'Successfully reset the attempts for user userman'
83
                    };
84
                    StaffDebug.doInstructorDashAction(action);
85 86 87 88 89
                    AjaxHelpers.respondWithJson(requests, action);
                    expect($('#idash_msg').text()).toBe('Successfully reset the attempts for user userman');
                    $('#result_' + locationName).remove();
                });
            });
90
            describe('doInstructorDashAction error', function() {
91
                it('adds a failure message to the results element after using an action', function() {
Eric Fischer committed
92
                    $('body').append($escapableResultArea);
93 94 95
                    var requests = AjaxHelpers.requests(this);
                    var action = {
                        locationName: esclocationName,
96
                        error_msg: 'Failed to reset attempts for user.'
97
                    };
98
                    StaffDebug.doInstructorDashAction(action);
99
                    AjaxHelpers.respondWithTextError(requests);
100
                    expect($('#idash_msg').text()).toBe('Failed to reset attempts for user. ');
101 102
                    $('#result_' + locationName).remove();
                });
103 104 105
            });
            describe('reset', function() {
                it('makes an ajax call with the expected parameters', function() {
106
                    $('body').append($usernameFixture);
107

108 109
                    spyOn($, 'ajax');
                    StaffDebug.reset(locationName, location);
110

111
                    expect($.ajax.calls.mostRecent().args[0].type).toEqual('POST');
112
                    expect($.ajax.calls.mostRecent().args[0].data).toEqual({
113 114 115
                        problem_to_reset: location,
                        unique_student_identifier: 'userman',
                        delete_module: false,
116 117
                        only_if_higher: undefined,
                        score: undefined
118
                    });
119
                    expect($.ajax.calls.mostRecent().args[0].url).toEqual(
120 121
                        '/instructor/api/reset_student_attempts'
                    );
122
                    $('#' + usernameFixtureID).remove();
123 124
                });
            });
125
            describe('deleteStudentState', function() {
126
                it('makes an ajax call with the expected parameters', function() {
127
                    $('body').append($usernameFixture);
128

129
                    spyOn($, 'ajax');
130
                    StaffDebug.deleteStudentState(locationName, location);
131

132
                    expect($.ajax.calls.mostRecent().args[0].type).toEqual('POST');
133
                    expect($.ajax.calls.mostRecent().args[0].data).toEqual({
134 135 136
                        problem_to_reset: location,
                        unique_student_identifier: 'userman',
                        delete_module: true,
137 138
                        only_if_higher: undefined,
                        score: undefined
139
                    });
140
                    expect($.ajax.calls.mostRecent().args[0].url).toEqual(
141 142 143
                        '/instructor/api/reset_student_attempts'
                    );

144
                    $('#' + usernameFixtureID).remove();
145 146
                });
            });
147 148
            describe('rescore', function() {
                it('makes an ajax call with the expected parameters', function() {
149
                    $('body').append($usernameFixture);
150

151 152
                    spyOn($, 'ajax');
                    StaffDebug.rescore(locationName, location);
153

154
                    expect($.ajax.calls.mostRecent().args[0].type).toEqual('POST');
155
                    expect($.ajax.calls.mostRecent().args[0].data).toEqual({
156 157 158
                        problem_to_reset: location,
                        unique_student_identifier: 'userman',
                        delete_module: undefined,
159 160
                        only_if_higher: false,
                        score: undefined
161
                    });
162
                    expect($.ajax.calls.mostRecent().args[0].url).toEqual(
163 164
                        '/instructor/api/rescore_problem'
                    );
165
                    $('#' + usernameFixtureID).remove();
166 167 168 169
                });
            });
            describe('rescoreIfHigher', function() {
                it('makes an ajax call with the expected parameters', function() {
170
                    $('body').append($usernameFixture);
171 172 173 174 175 176 177 178 179

                    spyOn($, 'ajax');
                    StaffDebug.rescoreIfHigher(locationName, location);

                    expect($.ajax.calls.mostRecent().args[0].type).toEqual('POST');
                    expect($.ajax.calls.mostRecent().args[0].data).toEqual({
                        problem_to_reset: location,
                        unique_student_identifier: 'userman',
                        delete_module: undefined,
180 181
                        only_if_higher: true,
                        score: undefined
182 183 184 185
                    });
                    expect($.ajax.calls.mostRecent().args[0].url).toEqual(
                        '/instructor/api/rescore_problem'
                    );
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
                    $('#' + usernameFixtureID).remove();
                });
            });
            describe('overrideScore', function() {
                it('makes an ajax call with the expected parameters', function() {
                    $('body').append($usernameFixture);
                    $('body').append($scoreFixture);
                    $('#' + scoreFixtureID).val('1');
                    spyOn($, 'ajax');
                    StaffDebug.overrideScore(locationName, location);

                    expect($.ajax.calls.mostRecent().args[0].type).toEqual('POST');
                    expect($.ajax.calls.mostRecent().args[0].data).toEqual({
                        problem_to_reset: location,
                        unique_student_identifier: 'userman',
                        delete_module: undefined,
                        only_if_higher: undefined,
                        score: '1'
                    });
                    expect($.ajax.calls.mostRecent().args[0].url).toEqual(
                        '/instructor/api/override_problem_score'
                    );
                    $('#' + usernameFixtureID).remove();
209 210 211
                });
            });
        });
212
    });