|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +describe("Story", function(){ |
| 4 | + it("should exist", function(){ |
| 5 | + Story.should.exist; |
| 6 | + }); |
| 7 | + |
| 8 | + it("uses defaults", function(){ |
| 9 | + var story = new Story(); |
| 10 | + story.get('open').should.be.false; |
| 11 | + story.get('selected').should.be.false; |
| 12 | + }); |
| 13 | + |
| 14 | + describe("open", function(){ |
| 15 | + it("sets open and selected to true", function(){ |
| 16 | + var story = new Story(); |
| 17 | + story.open(); |
| 18 | + story.get("open").should.be.true; |
| 19 | + story.get("selected").should.be.true; |
| 20 | + }); |
| 21 | + |
| 22 | + it("sets is_read to true if keep_unread isn't true", function(){ |
| 23 | + var story = new Story(); |
| 24 | + (!! story.get("keep_unread")).should.be.false; |
| 25 | + story.open(); |
| 26 | + story.get("is_read").should.be.true; |
| 27 | + }); |
| 28 | + |
| 29 | + it("doesn't sets is_read to true if keep_unread is true", function(){ |
| 30 | + var story = new Story(); |
| 31 | + story.set("keep_unread", true); |
| 32 | + story.open(); |
| 33 | + (!! story.get("is_read")).should.be.false; |
| 34 | + }); |
| 35 | + |
| 36 | + it("calls closeOthers, unselectAll, setSelection on collection", function(){ |
| 37 | + var story = new Story(), |
| 38 | + spy = story.collection = { |
| 39 | + closeOthers: sinon.spy(), |
| 40 | + unselectAll: sinon.spy(), |
| 41 | + setSelection: sinon.spy() |
| 42 | + }; |
| 43 | + story.open(); |
| 44 | + spy.closeOthers.should.have.been.calledWith(story); |
| 45 | + spy.unselectAll.should.have.been.calledOnce; |
| 46 | + spy.setSelection.should.have.been.calledWith(story); |
| 47 | + }); |
| 48 | + |
| 49 | + it("calls save if it should save", function(){ |
| 50 | + var story = new Story(); |
| 51 | + |
| 52 | + sinon.stub(story, "save"); |
| 53 | + sinon.stub(story, "shouldSave", function(){ |
| 54 | + return true; |
| 55 | + }); |
| 56 | + |
| 57 | + story.open(); |
| 58 | + story.shouldSave.should.have.been.calledOnce; |
| 59 | + story.save.should.have.been.calledOnce; |
| 60 | + }) |
| 61 | + }); |
| 62 | + |
| 63 | + describe("close", function(){ |
| 64 | + it("sets open to false", function(){ |
| 65 | + var story = new Story(); |
| 66 | + story.set("open", true); |
| 67 | + story.get("open").should.be.true; |
| 68 | + story.close(); |
| 69 | + story.get("open").should.be.false; |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("select", function(){ |
| 74 | + it("sets selected to true", function(){ |
| 75 | + var story = new Story(); |
| 76 | + story.select(); |
| 77 | + story.get("selected").should.be.true; |
| 78 | + }); |
| 79 | + |
| 80 | + it("calls unselectAll on collection", function(){ |
| 81 | + var story = new Story(), |
| 82 | + spy = story.collection = { |
| 83 | + unselectAll: sinon.spy() |
| 84 | + }; |
| 85 | + story.select(); |
| 86 | + spy.unselectAll.should.have.been.calledOnce; |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("toggle", function(){ |
| 91 | + it("calles open/close based on state", function(){ |
| 92 | + var story = new Story(); |
| 93 | + sinon.stub(story, "open"); |
| 94 | + sinon.stub(story, "close"); |
| 95 | + |
| 96 | + story.toggle(); |
| 97 | + story.open.should.have.been.calledOnce; |
| 98 | + story.close.should.not.have.been.called; |
| 99 | + |
| 100 | + story.set("open", true); |
| 101 | + story.toggle(); |
| 102 | + story.open.should.have.been.calledOnce; |
| 103 | + story.close.should.have.been.calledOnce; |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("shouldSave", function(){ |
| 108 | + it("returns false in there are no changed Attributes", function(){ |
| 109 | + var story = new Story(); |
| 110 | + sinon.stub(story, "changedAttributes", function(){ |
| 111 | + return false; |
| 112 | + }); |
| 113 | + story.shouldSave().should.be.false; |
| 114 | + story.changedAttributes.should.have.been.calledOnce; |
| 115 | + }); |
| 116 | + |
| 117 | + it("returns false if it has changedAttributes but no id", function(){ |
| 118 | + var story = new Story(); |
| 119 | + sinon.stub(story, "changedAttributes", function(){ |
| 120 | + return {is_read: true}; |
| 121 | + }); |
| 122 | + story.shouldSave().should.be.false; |
| 123 | + story.changedAttributes.should.have.been.calledOnce; |
| 124 | + }); |
| 125 | + |
| 126 | + it("returns true if it has changedAttributes and an id", function(){ |
| 127 | + var story = new Story({id: 1}); |
| 128 | + sinon.stub(story, "changedAttributes", function(){ |
| 129 | + return {is_read: true}; |
| 130 | + }); |
| 131 | + story.shouldSave().should.be.true; |
| 132 | + story.changedAttributes.should.have.been.calledOnce; |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe("openInTab", function(){ |
| 137 | + it("opens a new window", function(){ |
| 138 | + var story = new Story({permalink: "http://localhost"}); |
| 139 | + sinon.stub(window, "open"); |
| 140 | + |
| 141 | + story.openInTab(); |
| 142 | + window.open.should.have.been.calledWith("http://localhost", "_blank"); |
| 143 | + window.open.restore(); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments