license_spec.js 7.84 KB
Newer Older
1
define(['js/views/license', 'js/models/license', 'common/js/spec_helpers/template_helpers'],
2
             function(LicenseView, LicenseModel, TemplateHelpers) {
3 4 5 6 7 8
                 describe('License view', function() {
                     beforeEach(function() {
                         TemplateHelpers.installTemplate('license-selector', true);
                         this.model = new LicenseModel();
                         this.view = new LicenseView({model: this.model});
                     });
9

10 11 12 13 14 15 16 17 18 19 20
                     it('renders with no license', function() {
                         this.view.render();
                         expect(this.view.$('li[data-license=all-rights-reserved] button'))
                .toHaveText('All Rights Reserved');
                         expect(this.view.$('li[data-license=all-rights-reserved] button'))
                .not.toHaveClass('is-selected');
                         expect(this.view.$('li[data-license=creative-commons] button'))
                .toHaveText('Creative Commons');
                         expect(this.view.$('li[data-license=creative-commons] button'))
                .not.toHaveClass('is-selected');
                     });
21

22 23 24 25 26 27 28
                     it('renders with the right license selected', function() {
                         this.model.set('type', 'all-rights-reserved');
                         expect(this.view.$('li[data-license=all-rights-reserved] button'))
                .toHaveClass('is-selected');
                         expect(this.view.$('li[data-license=creative-commons] button'))
                .not.toHaveClass('is-selected');
                     });
29

30 31 32 33 34
                     it('switches license type on click', function() {
                         var arrBtn = this.view.$('li[data-license=all-rights-reserved] button');
                         expect(this.model.get('type')).toBeNull();
                         arrBtn.click();
                         expect(this.model.get('type')).toEqual('all-rights-reserved');
35
            // view has re-rendered, so get a new reference to the button
36 37
                         arrBtn = this.view.$('li[data-license=all-rights-reserved] button');
                         expect(arrBtn).toHaveClass('is-selected');
38
            // now switch to creative commons
39 40 41
                         var ccBtn = this.view.$('li[data-license=creative-commons] button');
                         ccBtn.click();
                         expect(this.model.get('type')).toEqual('creative-commons');
42
            // update references again
43 44 45 46 47
                         arrBtn = this.view.$('li[data-license=all-rights-reserved] button');
                         ccBtn = this.view.$('li[data-license=creative-commons] button');
                         expect(arrBtn).not.toHaveClass('is-selected');
                         expect(ccBtn).toHaveClass('is-selected');
                     });
48

49 50 51 52 53 54
                     it('sets default license options when switching license types', function() {
                         expect(this.model.get('options')).toEqual({});
                         var ccBtn = this.view.$('li[data-license=creative-commons] button');
                         ccBtn.click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
55
            );
56 57 58 59
                         var arrBtn = this.view.$('li[data-license=all-rights-reserved] button');
                         arrBtn.click();
                         expect(this.model.get('options')).toEqual({});
                     });
60

61 62 63 64 65 66 67 68 69 70 71 72
                     it('renders license options', function() {
                         this.model.set({'type': 'creative-commons'});
                         expect(this.view.$('ul.license-options li[data-option=BY]'))
                .toContainText('Attribution');
                         expect(this.view.$('ul.license-options li[data-option=NC]'))
                .toContainText('Noncommercial');
                         expect(this.view.$('ul.license-options li[data-option=ND]'))
                .toContainText('No Derivatives');
                         expect(this.view.$('ul.license-options li[data-option=SA]'))
                .toContainText('Share Alike');
                         expect(this.view.$('ul.license-options li').length).toEqual(4);
                     });
73

74 75 76 77
                     it('toggles boolean options on click', function() {
                         this.view.$('li[data-license=creative-commons] button').click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
78 79
            );
            // toggle NC option
80 81 82
                         this.view.$('li[data-option=NC]').click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': false, 'ND': true, 'SA': false}
83
            );
84
                     });
85

86 87 88 89
                     it("doesn't toggle disabled options", function() {
                         this.view.$('li[data-license=creative-commons] button').click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
90
            );
91 92
                         var BY = this.view.$('li[data-option=BY]');
                         expect(BY).toHaveClass('is-disabled');
93
            // try to toggle BY option
94
                         BY.click();
95
            // no change
96 97
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
98
            );
99
                     });
100

101 102 103 104
                     it("doesn't allow simultaneous conflicting options", function() {
                         this.view.$('li[data-license=creative-commons] button').click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
105 106
            );
            // SA and ND conflict
107
                         var SA = this.view.$('li[data-option=SA]');
108
            // try to turn on SA option
109
                         SA.click();
110
            // ND should no longer be selected
111 112
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': false, 'SA': true}
113
            );
114 115

            // try to turn on ND option
116 117 118 119
                         ND = this.view.$('li[data-option=ND]');
                         ND.click();
                         expect(this.model.get('options')).toEqual(
                {'ver': '4.0', 'BY': true, 'NC': true, 'ND': true, 'SA': false}
120
            );
121
                     });
122

123 124 125 126 127 128
                     it('has no preview by default', function() {
                         this.view.render();
                         expect(this.view.$('.license-preview').length).toEqual(0);
                         this.view.$('li[data-license=creative-commons] button').click();
                         expect(this.view.$('.license-preview').length).toEqual(0);
                     });
129

130 131 132 133
                     it('displays a preview if showPreview is true', function() {
                         this.view = new LicenseView({model: this.model, showPreview: true});
                         this.view.render();
                         expect(this.view.$('.license-preview').length).toEqual(1);
134
	    // Expect default text to be "All Rights Reserved"
135 136 137 138 139 140 141
                         expect(this.view.$('.license-preview')).toContainText('All Rights Reserved');
                         this.view.$('li[data-license=creative-commons] button').click();
                         expect(this.view.$('.license-preview').length).toEqual(1);
                         expect(this.view.$('.license-preview')).toContainText('Some Rights Reserved');
                     });
                 });
             });