xblock_spec.js 5.23 KB
Newer Older
1
define([ "jquery", "js/common_helpers/ajax_helpers", "URI", "js/views/xblock", "js/models/xblock_info",
2
    "xmodule", "coffee/src/main", "xblock/cms.runtime.v1"],
3
    function ($, AjaxHelpers, URI, XBlockView, XBlockInfo) {
4 5 6 7 8 9

        describe("XBlockView", function() {
            var model, xblockView, mockXBlockHtml, respondWithMockXBlockFragment;

            beforeEach(function () {
                model = new XBlockInfo({
10
                    id: 'testCourse/branch/draft/block/verticalFFF',
11 12 13 14 15 16 17 18 19 20 21 22
                    display_name: 'Test Unit',
                    category: 'vertical'
                });
                xblockView = new XBlockView({
                    model: model
                });
            });

            mockXBlockHtml = readFixtures('mock/mock-xblock.underscore');

            respondWithMockXBlockFragment = function(requests, response) {
                var requestIndex = requests.length - 1;
23
                AjaxHelpers.respondWithJson(requests, response, requestIndex);
24 25 26
            };

            it('can render a nested xblock', function() {
27
                var requests = AjaxHelpers.requests(this);
28 29 30
                xblockView.render();
                respondWithMockXBlockFragment(requests, {
                    html: mockXBlockHtml,
31
                    resources: []
32 33 34 35 36 37 38 39 40
                });

                expect(xblockView.$el.select('.xblock-header')).toBeTruthy();
            });

            describe("XBlock rendering", function() {
                var postXBlockRequest;

                postXBlockRequest = function(requests, resources) {
41
                    var promise;
42 43 44 45
                    $.ajax({
                        url: "test_url",
                        type: 'GET',
                        success: function(fragment) {
46
                            promise = xblockView.renderXBlockFragment(fragment, this.$el);
47 48
                        }
                    });
49 50
                    // Note: this mock response will call the AJAX success function synchronously
                    // so the promise variable defined above will be available.
51 52 53 54 55
                    respondWithMockXBlockFragment(requests, {
                        html: mockXBlockHtml,
                        resources: resources
                    });
                    expect(xblockView.$el.select('.xblock-header')).toBeTruthy();
56
                    return promise;
57 58 59
                };

                it('can render an xblock with no CSS or JavaScript', function() {
60
                    var requests = AjaxHelpers.requests(this);
61 62 63 64
                    postXBlockRequest(requests, []);
                });

                it('can render an xblock with required CSS', function() {
65
                    var requests = AjaxHelpers.requests(this),
66 67 68 69 70 71 72 73 74 75 76 77 78
                        mockCssText = "// Just a comment",
                        mockCssUrl = "mock.css",
                        headHtml;
                    postXBlockRequest(requests, [
                        ["hash1", { mimetype: "text/css", kind: "text", data: mockCssText }],
                        ["hash2", { mimetype: "text/css", kind: "url", data: mockCssUrl }]
                    ]);
                    headHtml = $('head').html();
                    expect(headHtml).toContain(mockCssText);
                    expect(headHtml).toContain(mockCssUrl);
                });

                it('can render an xblock with required JavaScript', function() {
79
                    var requests = AjaxHelpers.requests(this);
80 81 82 83 84 85 86
                    postXBlockRequest(requests, [
                        ["hash3", { mimetype: "application/javascript", kind: "text", data: "window.test = 100;" }]
                    ]);
                    expect(window.test).toBe(100);
                });

                it('can render an xblock with required HTML', function() {
87
                    var requests = AjaxHelpers.requests(this),
88 89 90 91 92 93
                        mockHeadTag = "<title>Test Title</title>";
                    postXBlockRequest(requests, [
                        ["hash4", { mimetype: "text/html", placement: "head", data: mockHeadTag }]
                    ]);
                    expect($('head').html()).toContain(mockHeadTag);
                });
94 95

                it('aborts rendering when a dependent script fails to load', function() {
96
                    var requests = AjaxHelpers.requests(this),
97 98 99 100 101 102 103 104
                        mockJavaScriptUrl = "mock.js",
                        promise;
                    spyOn($, 'getScript').andReturn($.Deferred().reject().promise());
                    promise = postXBlockRequest(requests, [
                        ["hash5", { mimetype: "application/javascript", kind: "url", data: mockJavaScriptUrl }]
                    ]);
                    expect(promise.isRejected()).toBe(true);
                });
105 106 107 108 109 110 111 112

                it('Triggers an event to the runtime when a notification-action-button is clicked', function () {
                    var notifySpy = spyOn(xblockView, "notifyRuntime").andCallThrough();

                    postXBlockRequest(AjaxHelpers.requests(this), []);
                    xblockView.$el.find(".notification-action-button").click();
                    expect(notifySpy).toHaveBeenCalledWith("add-missing-groups", model.get("id"));
                })
113 114 115
            });
        });
    });