Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mobile_comments_spec.js « mobile « javascripts « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: af12f644ce1b4678f357f4f1459029dc8a2d7641 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
describe("Diaspora.Mobile.Comments", function(){
  beforeEach(function() {
    spec.loadFixture("aspects_index_mobile_post_with_comments");
    this.bottomBar = $(".bottom-bar").first();
    this.link = $(".stream .show-comments").first();
  });

  describe("initialize", function() {
    it("calls submitComment when the comment form has been submitted", function() {
      spyOn(Diaspora.Mobile.Comments, "submitComment").and.returnValue(false);
      Diaspora.Mobile.Comments.initialize();
      Diaspora.Mobile.Comments.showCommentBox($(".stream .comment-action").first());
      $(".stream .new-comment").first().submit();
      expect(Diaspora.Mobile.Comments.submitComment).toHaveBeenCalled();
    });
  });

  describe("toggleComments", function() {
    beforeEach(function() {
      spyOn(Diaspora.Mobile.Comments, "showComments");
      spyOn(Diaspora.Mobile.Comments, "hideComments");
    });

    it("calls showComments", function() {
      Diaspora.Mobile.Comments.toggleComments(this.link);
      expect(Diaspora.Mobile.Comments.showComments).toHaveBeenCalled();
      expect(Diaspora.Mobile.Comments.hideComments).not.toHaveBeenCalled();
    });

    it("calls hideComments if the link class is 'active'", function() {
      this.link.addClass("active");
      Diaspora.Mobile.Comments.toggleComments(this.link);
      expect(Diaspora.Mobile.Comments.showComments).not.toHaveBeenCalled();
      expect(Diaspora.Mobile.Comments.hideComments).toHaveBeenCalled();
    });

    it("doesn't call any function if the link class is 'loading'", function() {
      this.link.addClass("loading");
      Diaspora.Mobile.Comments.toggleComments(this.link);
      expect(Diaspora.Mobile.Comments.showComments).not.toHaveBeenCalled();
      expect(Diaspora.Mobile.Comments.hideComments).not.toHaveBeenCalled();
    });
  });

  describe("showUnloadedComments", function() {
    beforeEach(function() {
      this.commentActionLink = this.bottomBar.find("a.comment-action");
    });

    it("adds the 'loading' class to the link", function() {
      Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
      expect($(".show-comments").first()).toHaveClass("loading");
    });

    it("removes the 'loading' class if the request failed", function() {
      Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
      jasmine.Ajax.requests.mostRecent().respondWith({status: 400});
      expect($(".show-comments").first()).not.toHaveClass("loading");
    });

    it("adds the 'active' class if the request succeeded", function() {
      Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
      jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
      expect($(".show-comments").first()).toHaveClass("active");
      expect($(".show-comments").first()).not.toHaveClass("loading");
    });

    it("calls showCommentBox", function() {
      spyOn(Diaspora.Mobile.Comments, "showCommentBox");
      Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
      jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
      expect(Diaspora.Mobile.Comments.showCommentBox).toHaveBeenCalledWith(this.commentActionLink);
    });

    it("adds the response text to the comments list", function() {
      Diaspora.Mobile.Comments.showUnloadedComments(this.link, this.bottomBar, this.commentActionLink);
      jasmine.Ajax.requests.mostRecent().respondWith({
        status: 200,
        contentType: "text/plain",
        responseText: "<div class=\"commentContainerForTest\">new comments</div>"
      });
      expect($(".stream .stream-element").first()).toContainElement(".commentContainerForTest");
    });

    it("shows and hides the mobile spinner", function(){
      Diaspora.Mobile.Comments.showComments(this.link);
      expect($(".ajax-loader").first()).toBeVisible();
      jasmine.Ajax.requests.mostRecent().respondWith({status: 200, contentType: "text/plain", responseText: "test"});
      expect($(".ajax-loader").first()).not.toBeVisible();
    });
  });

  describe("submitComment", function() {
    beforeEach(function() {
      Diaspora.Mobile.Comments.initialize();
      Diaspora.Mobile.Comments.showCommentBox($(".stream .comment-action").first());
    });

    it("doesn't submit an empty comment", function() {
      $(".stream .new-comment").first().submit();
      expect(jasmine.Ajax.requests.count()).toBe(0);
    });

    it("submits comments with text", function() {
      $(".stream .new-comment textarea").val("comment text");
      $(".stream .new-comment").first().submit();
      expect(jasmine.Ajax.requests.mostRecent().data().text).toEqual(["comment text"]);
    });

    it("calls updateStream on success", function() {
      spyOn(Diaspora.Mobile.Comments, "updateStream");
      $(".stream .new-comment textarea").val("comment text");
      $(".stream .new-comment").first().submit();
      jasmine.Ajax.requests.mostRecent().respondWith({status: 200, responseText: "foo"});
      expect(Diaspora.Mobile.Comments.updateStream).toHaveBeenCalledWith($(".stream .new-comment").first(), "foo");
    });

    it("lets Diaspora.Mobile.Alert handle AJAX errors", function() {
      spyOn(Diaspora.Mobile.Alert, "handleAjaxError");
      $(".stream .new-comment textarea").val("comment text");
      $(".stream .new-comment").first().submit();
      jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! comment failed!"});
      expect(Diaspora.Mobile.Alert.handleAjaxError).toHaveBeenCalled();
      expect(Diaspora.Mobile.Alert.handleAjaxError.calls.argsFor(0)[0].responseText).toBe("oh noez! comment failed!");
    });

    it("calls resetCommentBox on errors", function() {
      spyOn(Diaspora.Mobile.Comments, "resetCommentBox");
      $(".stream .new-comment textarea").val("comment text");
      $(".stream .new-comment").first().submit();
      jasmine.Ajax.requests.mostRecent().respondWith({status: 400, responseText: "oh noez! comment failed!"});
      expect(Diaspora.Mobile.Comments.resetCommentBox).toHaveBeenCalledWith($(".stream .new-comment").first());
    });
  });

  describe("increaseReactionCount", function(){
    beforeEach(function() {
      this.toggleReactionsLink = this.bottomBar.find(".show-comments").first();
    });

    it("Increase reaction count from 1", function(){
      expect(this.toggleReactionsLink.text().trim()).toBe("5 comments");
      Diaspora.Mobile.Comments.increaseReactionCount(this.bottomBar);
      expect(this.toggleReactionsLink.text().trim()).toBe("6 comments");
    });

    it("Creates the reaction link when there are no reactions", function() {
      var parent = this.toggleReactionsLink.parent();
      var postGuid = this.bottomBar.parents(".stream-element").data("guid");
      this.toggleReactionsLink.remove();
      parent.prepend($("<span/>", {"class": "show-comments"}).text("0 comments"));

      Diaspora.Mobile.Comments.increaseReactionCount(this.bottomBar);
      this.toggleReactionsLink = this.bottomBar.find(".show-comments").first();
      expect(this.toggleReactionsLink.text().trim()).toBe("1 comment");
      expect(this.toggleReactionsLink.attr("href")).toBe("/posts/" + postGuid + "/comments.mobile");
    });

    it("Creates the reaction link when there are no reactions (french locale)", function() {
      var parent = this.toggleReactionsLink.parent();
      var postGuid = this.bottomBar.parents(".stream-element").data("guid");
      this.toggleReactionsLink.remove();
      parent.prepend($("<span/>", {"class": "show-comments"}).text("Aucun commentaire"));

      Diaspora.Mobile.Comments.increaseReactionCount(this.bottomBar);
      this.toggleReactionsLink = this.bottomBar.find(".show-comments").first();
      expect(this.toggleReactionsLink.text().trim()).toBe("1 comment");
      expect(this.toggleReactionsLink.attr("href")).toBe("/posts/" + postGuid + "/comments.mobile");
    });
  });

  describe("bottomBarLazy", function(){
    beforeEach(function() {
      this.bottomBarLazy = Diaspora.Mobile.Comments.bottomBarLazy(this.bottomBar);
    });

    it("shows and hides the loader", function(){
      expect(this.bottomBarLazy.loader()).toHaveClass("hidden");
      this.bottomBarLazy.showLoader();
      expect(this.bottomBarLazy.loader()).not.toHaveClass("hidden");
      this.bottomBarLazy.hideLoader();
      expect(this.bottomBarLazy.loader()).toHaveClass("hidden");
    });

    it("activates the bottom bar", function(){
      expect(this.bottomBar).toHaveClass("inactive");
      expect(this.bottomBar).not.toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink()).not.toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink().find("i")).toHaveClass("entypo-chevron-down");
      this.bottomBarLazy.activate();
      expect(this.bottomBar).not.toHaveClass("inactive");
      expect(this.bottomBar).toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink()).toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink().find("i")).toHaveClass("entypo-chevron-up");
    });

    it("deactivates the bottom bar", function(){
      this.bottomBarLazy.activate();
      expect(this.bottomBar).not.toHaveClass("inactive");
      expect(this.bottomBar).toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink()).toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink().find("i")).toHaveClass("entypo-chevron-up");
      this.bottomBarLazy.deactivate();
      expect(this.bottomBar).toHaveClass("inactive");
      expect(this.bottomBar).not.toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink()).not.toHaveClass("active");
      expect(this.bottomBarLazy.getShowCommentsLink().find("i")).toHaveClass("entypo-chevron-down");
    });
  });
});