tab_view_spec.js 4.77 KB
Newer Older
1
define([
2 3 4 5 6 7 8
    'jquery',
    'backbone',
    'edx-ui-toolkit/js/utils/html-utils',
    'common/js/spec_helpers/template_helpers',
    'js/edxnotes/collections/tabs',
    'js/edxnotes/views/tabs_list',
    'js/edxnotes/views/tab_view'
9
], function($, Backbone, HtmlUtils, TemplateHelpers, TabsCollection, TabsListView, TabView) {
10 11 12 13 14 15
    'use strict';
    describe('EdxNotes TabView', function() {
        var TestSubView = Backbone.View.extend({
                id: 'test-subview-panel',
                className: 'tab-panel',
                content: '<p>test view content</p>',
16
                render: function() {
17 18 19 20 21 22 23 24 25 26
                    this.$el.html(this.content);
                    return this;
                }
            }),
            TestView = TabView.extend({
                PanelConstructor: TestSubView,
                tabInfo: {
                    name: 'Test View Tab',
                    is_closable: true
                }
Eric Fischer committed
27 28
            }),
            getView;
29

30
        getView = function(tabsCollection, options) {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
            var view;
            options = _.defaults(options || {}, {
                el: $('.wrapper-student-notes'),
                collection: [],
                tabsCollection: tabsCollection
            });
            view = new TestView(options);

            if (tabsCollection.length) {
                tabsCollection.at(0).activate();
            }

            return view;
        };

46
        beforeEach(function() {
47 48 49 50 51 52 53 54 55
            loadFixtures('js/fixtures/edxnotes/edxnotes.html');
            TemplateHelpers.installTemplates([
                'templates/edxnotes/note-item', 'templates/edxnotes/tab-item'
            ]);
            this.tabsCollection = new TabsCollection();
            this.tabsList = new TabsListView({collection: this.tabsCollection}).render();
            this.tabsList.$el.appendTo($('.tab-list'));
        });

56
        it('can create a tab and content on initialization', function() {
57 58 59 60 61 62
            var view = getView(this.tabsCollection);
            expect(this.tabsCollection).toHaveLength(1);
            expect(view.$('.tab')).toExist();
            expect(view.$('.wrapper-tabs')).toContainHtml('<p>test view content</p>');
        });

63
        it('cannot create a tab on initialization if flag is not set', function() {
64 65 66 67 68 69 70 71
            var view = getView(this.tabsCollection, {
                createTabOnInitialization: false
            });
            expect(this.tabsCollection).toHaveLength(0);
            expect(view.$('.tab')).not.toExist();
            expect(view.$('.wrapper-tabs')).not.toContainHtml('<p>test view content</p>');
        });

72
        it('can remove the content if tab becomes inactive', function() {
73 74 75 76 77 78 79
            var view = getView(this.tabsCollection);
            this.tabsCollection.add({identifier: 'second-tab'});
            view.$('#second-tab').click();
            expect(view.$('.tab')).toHaveLength(2);
            expect(view.$('.wrapper-tabs')).not.toContainHtml('<p>test view content</p>');
        });

80
        it('can remove the content if tab is closed', function() {
81
            var view = getView(this.tabsCollection);
82
            view.onClose = jasmine.createSpy();
83 84 85 86 87 88 89
            view.$('.tab .action-close').click();
            expect(view.$('.tab')).toHaveLength(0);
            expect(view.$('.wrapper-tabs')).not.toContainHtml('<p>test view content</p>');
            expect(view.tabModel).toBeNull();
            expect(view.onClose).toHaveBeenCalled();
        });

90
        it('can correctly update the content of active tab', function() {
91 92 93 94 95 96 97
            var view = getView(this.tabsCollection);
            TestSubView.prototype.content = '<p>New content</p>';
            view.render();
            expect(view.$('.wrapper-tabs')).toContainHtml('<p>New content</p>');
            expect(view.$('.wrapper-tabs')).not.toContainHtml('<p>test view content</p>');
        });

98
        it('can show/hide error messages', function() {
99 100
            var view = getView(this.tabsCollection),
                errorHolder = view.$('.wrapper-msg');
101 102

            view.showErrorMessageHtml(HtmlUtils.HTML('<p>error message is here</p>'));
103 104 105 106 107 108 109 110
            expect(errorHolder).not.toHaveClass('is-hidden');
            expect(errorHolder.find('.copy')).toContainHtml('<p>error message is here</p>');

            view.hideErrorMessage();
            expect(errorHolder).toHaveClass('is-hidden');
            expect(errorHolder.find('.copy')).toBeEmpty();
        });

111
        it('should hide error messages before rendering', function() {
112 113
            var view = getView(this.tabsCollection),
                errorHolder = view.$('.wrapper-msg');
114
            view.showErrorMessageHtml('<p>error message is here</p>');
115 116 117 118 119 120
            view.render();
            expect(errorHolder).toHaveClass('is-hidden');
            expect(errorHolder.find('.copy')).toBeEmpty();
        });
    });
});