license_spec.js 6.67 KB
Newer Older
1
define(["js/views/license", "js/models/license", "common/js/spec_helpers/template_helpers"],
2 3
             function(LicenseView, LicenseModel, TemplateHelpers) {
    describe("License view", function() {
4

5 6 7 8 9
        beforeEach(function() {
            TemplateHelpers.installTemplate("license-selector", true);
            this.model = new LicenseModel();
            this.view = new LicenseView({model: this.model});
        });
10

11 12 13 14 15 16 17 18 19 20 21
        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");
        });
22

23 24 25 26 27 28 29
        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");
        });
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        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");
            // view has re-rendered, so get a new reference to the button
            arrBtn = this.view.$("li[data-license=all-rights-reserved] button");
            expect(arrBtn).toHaveClass("is-selected");
            // now switch to creative commons
            var ccBtn = this.view.$("li[data-license=creative-commons] button");
            ccBtn.click();
            expect(this.model.get("type")).toEqual("creative-commons");
            // update references again
            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");
        });
49

50 51 52 53 54 55 56 57 58 59 60
        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}
            );
            var arrBtn = this.view.$("li[data-license=all-rights-reserved] button");
            arrBtn.click()
            expect(this.model.get("options")).toEqual({});
        });
61

62 63 64 65 66 67 68 69 70 71 72 73
        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);
        });
74

75 76 77 78 79 80 81 82 83 84 85
        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}
            );
            // toggle NC option
            this.view.$("li[data-option=NC]").click();
            expect(this.model.get("options")).toEqual(
                {"ver": "4.0", "BY": true, "NC": false, "ND": true, "SA": false}
            );
        });
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100
        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}
            );
            var BY = this.view.$("li[data-option=BY]");
            expect(BY).toHaveClass("is-disabled");
            // try to toggle BY option
            BY.click()
            // no change
            expect(this.model.get("options")).toEqual(
                {"ver": "4.0", "BY": true, "NC": true, "ND": true, "SA": false}
            );
        });
101

102 103 104 105 106 107 108
        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}
            );
            // SA and ND conflict
            var SA = this.view.$("li[data-option=SA]");
109
            // try to turn on SA option
110
            SA.click()
111
            // ND should no longer be selected
112 113 114
            expect(this.model.get("options")).toEqual(
                {"ver": "4.0", "BY": true, "NC": true, "ND": false, "SA": true}
            );
115 116

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

124 125
        it("has no preview by default", function () {
            this.view.render();
126
            expect(this.view.$(".license-preview").length).toEqual(0)
127
            this.view.$("li[data-license=creative-commons] button").click();
128
            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()
134
            expect(this.view.$(".license-preview").length).toEqual(1)
135 136
	    // Expect default text to be "All Rights Reserved"
            expect(this.view.$(".license-preview")).toContainText("All Rights Reserved");
137
            this.view.$("li[data-license=creative-commons] button").click();
138 139
            expect(this.view.$(".license-preview").length).toEqual(1)
            expect(this.view.$(".license-preview")).toContainText("Some Rights Reserved");
140
        });
141

142
    })
143
})