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

stream_spec.js « models « app « javascripts « spec - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bea72ea7123e1fd1448a72cb7372e0009bf78854 (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
describe("app.models.Stream", function() {
  var stream,
      expectedPath;

  beforeEach(function(){
    stream = new app.models.Stream();
    expectedPath = document.location.pathname;
  });

  describe("collectionOptions", function() {
    beforeEach(function() {
      this.post1 = new app.models.Post({"id": 1, "created_at": 12, "interacted_at": 123});
      this.post2 = new app.models.Post({"id": 2, "created_at": 13, "interacted_at": 123});
      this.post3 = new app.models.Post({"id": 3, "created_at": 13, "interacted_at": 122});
      this.post4 = new app.models.Post({"id": 4, "created_at": 10, "interacted_at": 100});
    });

    it("returns a comparator for posts that compares created_at and ids by default", function() {
      this.options = stream.collectionOptions();
      expect(this.options.comparator(this.post1, this.post2)).toBe(1);
      expect(this.options.comparator(this.post2, this.post1)).toBe(-1);
      expect(this.options.comparator(this.post2, this.post3)).toBe(1);
      expect(this.options.comparator(this.post3, this.post2)).toBe(-1);
      expect(this.options.comparator(this.post1, this.post4)).toBe(-1);
      expect(this.options.comparator(this.post4, this.post1)).toBe(1);
      expect(this.options.comparator(this.post1, this.post1)).toBe(0);
    });

    it("returns a comparator for posts that compares interacted_at and ids for the activity stream", function() {
      spyOn(stream, "basePath").and.returnValue("/activity");
      this.options = stream.collectionOptions();
      expect(this.options.comparator(this.post1, this.post2)).toBe(1);
      expect(this.options.comparator(this.post2, this.post1)).toBe(-1);
      expect(this.options.comparator(this.post2, this.post3)).toBe(-1);
      expect(this.options.comparator(this.post3, this.post2)).toBe(1);
      expect(this.options.comparator(this.post1, this.post4)).toBe(-1);
      expect(this.options.comparator(this.post4, this.post1)).toBe(1);
      expect(this.options.comparator(this.post1, this.post1)).toBe(0);
    });

    it("returns a comparator for posts that compares created_at and ids for tags including 'activity'", function() {
      spyOn(stream, "basePath").and.returnValue("/tags/foo-activity-bar");
      this.options = stream.collectionOptions();
      expect(this.options.comparator(this.post2, this.post3)).toBe(1);
      expect(this.options.comparator(this.post3, this.post2)).toBe(-1);
    });
  });

  describe("#_fetchOpts", function() {
    it("it fetches posts from the window's url, and ads them to the collection", function() {
      expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath});
    });

    it("returns the json path with max_time if the collection has models", function() {
      var post = new app.models.Post({created_at: 1234000});
      stream.add(post);

      expect( stream._fetchOpts() ).toEqual({ remove: false, url: expectedPath + "?max_time=1234"});
    });
  });

  describe("events", function() {
    var postFetch,
        fetchedSpy;

    beforeEach(function(){
      postFetch = new $.Deferred();
      fetchedSpy = jasmine.createSpy();
      spyOn(stream.items, "fetch").and.callFake(function(){
        return postFetch;
      });
    });

    it("triggers fetched on the stream when it is fetched", function(){
      stream.bind('fetched', fetchedSpy);
      stream.fetch();
      postFetch.resolve([1,2,3]);

      expect(fetchedSpy).toHaveBeenCalled();
    });

    it("triggers allItemsLoaded on the stream when zero posts are returned", function(){
      stream.bind('allItemsLoaded', fetchedSpy);
      stream.fetch();
      postFetch.resolve([]);

      expect(fetchedSpy).toHaveBeenCalled();
    });

    it("triggers allItemsLoaded on the stream when a Post is returned", function(){
      stream.bind('allItemsLoaded', fetchedSpy);
      stream.fetch();
      postFetch.resolve(factory.post().attributes);

      expect(fetchedSpy).toHaveBeenCalled();
    });
  });
});