stat_graph_contributors_util_spec.js 9.6 KB
Newer Older
1
/* eslint-disable quotes, no-var, camelcase, object-curly-spacing, object-property-newline, comma-dangle, comma-spacing, spaced-comment, max-len, key-spacing, vars-on-top, quote-props, no-multi-spaces */
2 3
/* global ContributorsStatGraphUtil */

C
Connor Shea 已提交
4
//= require graphs/stat_graph_contributors_util
5

6 7 8 9
describe("ContributorsStatGraphUtil", function () {
  describe("#parse_log", function () {
    it("returns a correctly parsed log", function () {
      var fake_log = [
10 11 12
            {author_email: "karlo@email.com", author_name: "Karlo Soriano", date: "2013-05-09", additions: 471},
            {author_email: "dzaporozhets@email.com", author_name: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 6, deletions: 1},
            {author_email: "dzaporozhets@email.com", author_name: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 19, deletions: 3},
13
            {author_email: "dzaporozhets@email.com", author_name: "Dmitriy Zaporozhets", date: "2013-05-08", additions: 29, deletions: 3}];
P
Phil Hughes 已提交
14

15 16 17 18 19 20
      var correct_parsed_log = {
        total: [
        {date: "2013-05-09", additions: 471, deletions: 0, commits: 1},
        {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}],
        by_author:
        [
21 22 23 24 25 26 27 28
          {
            author_name: "Karlo Soriano", author_email: "karlo@email.com",
            "2013-05-09": {date: "2013-05-09", additions: 471, deletions: 0, commits: 1}
          },
          {
            author_name: "Dmitriy Zaporozhets",author_email: "dzaporozhets@email.com",
            "2013-05-08": {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}
          }
29
        ]
30 31 32 33
      };
      expect(ContributorsStatGraphUtil.parse_log(fake_log)).toEqual(correct_parsed_log);
    });
  });
34 35

  describe("#store_data", function () {
36 37 38
    var fake_entry = {author: "Karlo Soriano", date: "2013-05-09", additions: 471};
    var fake_total = {};
    var fake_by_author = {};
39 40

    it("calls #store_commits", function () {
41 42 43 44
      spyOn(ContributorsStatGraphUtil, 'store_commits');
      ContributorsStatGraphUtil.store_data(fake_entry, fake_total, fake_by_author);
      expect(ContributorsStatGraphUtil.store_commits).toHaveBeenCalled();
    });
45 46

    it("calls #store_additions", function () {
47 48 49 50
      spyOn(ContributorsStatGraphUtil, 'store_additions');
      ContributorsStatGraphUtil.store_data(fake_entry, fake_total, fake_by_author);
      expect(ContributorsStatGraphUtil.store_additions).toHaveBeenCalled();
    });
51 52

    it("calls #store_deletions", function () {
53 54 55 56 57
      spyOn(ContributorsStatGraphUtil, 'store_deletions');
      ContributorsStatGraphUtil.store_data(fake_entry, fake_total, fake_by_author);
      expect(ContributorsStatGraphUtil.store_deletions).toHaveBeenCalled();
    });
  });
58

59 60
  // TODO: fix or remove
  //describe("#store_commits", function () {
61 62
    //var fake_total = "fake_total";
    //var fake_by_author = "fake_by_author";
63

64
    //it("calls #add twice with arguments fake_total and fake_by_author respectively", function () {
65 66 67 68 69
      //spyOn(ContributorsStatGraphUtil, 'add');
      //ContributorsStatGraphUtil.store_commits(fake_total, fake_by_author);
      //expect(ContributorsStatGraphUtil.add.argsForCall).toEqual([["fake_total", "commits", 1], ["fake_by_author", "commits", 1]]);
    //});
  //});
70 71 72

  describe("#add", function () {
    it("adds 1 to current test_field in collection", function () {
73 74 75 76
      var fake_collection = {test_field: 10};
      ContributorsStatGraphUtil.add(fake_collection, "test_field", 1);
      expect(fake_collection.test_field).toEqual(11);
    });
77 78

    it("inits and adds 1 if test_field in collection is not defined", function () {
79 80 81 82 83
      var fake_collection = {};
      ContributorsStatGraphUtil.add(fake_collection, "test_field", 1);
      expect(fake_collection.test_field).toEqual(1);
    });
  });
84

85 86
  // TODO: fix or remove
  //describe("#store_additions", function () {
87 88 89
    //var fake_entry = {additions: 10};
    //var fake_total= "fake_total";
    //var fake_by_author = "fake_by_author";
90
    //it("calls #add twice with arguments fake_total and fake_by_author respectively", function () {
91 92 93 94 95
      //spyOn(ContributorsStatGraphUtil, 'add');
      //ContributorsStatGraphUtil.store_additions(fake_entry, fake_total, fake_by_author);
      //expect(ContributorsStatGraphUtil.add.argsForCall).toEqual([["fake_total", "additions", 10], ["fake_by_author", "additions", 10]]);
    //});
  //});
96

97 98
  // TODO: fix or remove
  //describe("#store_deletions", function () {
99 100 101
    //var fake_entry = {deletions: 10};
    //var fake_total= "fake_total";
    //var fake_by_author = "fake_by_author";
102
    //it("calls #add twice with arguments fake_total and fake_by_author respectively", function () {
103 104 105 106 107
      //spyOn(ContributorsStatGraphUtil, 'add');
      //ContributorsStatGraphUtil.store_deletions(fake_entry, fake_total, fake_by_author);
      //expect(ContributorsStatGraphUtil.add.argsForCall).toEqual([["fake_total", "deletions", 10], ["fake_by_author", "deletions", 10]]);
    //});
  //});
108 109 110

  describe("#add_date", function () {
    it("adds a date field to the collection", function () {
111 112 113 114 115 116
      var fake_date = "2013-10-02";
      var fake_collection = {};
      ContributorsStatGraphUtil.add_date(fake_date, fake_collection);
      expect(fake_collection[fake_date].date).toEqual("2013-10-02");
    });
  });
117 118 119

  describe("#add_author", function () {
    it("adds an author field to the collection", function () {
120 121 122 123 124 125 126 127
      var fake_author = { author_name: "Author", author_email: 'fake@email.com' };
      var fake_author_collection = {};
      var fake_email_collection = {};
      ContributorsStatGraphUtil.add_author(fake_author, fake_author_collection, fake_email_collection);
      expect(fake_author_collection[fake_author.author_name].author_name).toEqual("Author");
      expect(fake_email_collection[fake_author.author_email].author_name).toEqual("Author");
    });
  });
128 129 130 131

  describe("#get_total_data", function () {
    it("returns the collection sorted via specified field", function () {
      var fake_parsed_log = {
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        total: [
          {date: "2013-05-09", additions: 471, deletions: 0, commits: 1},
          {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}
        ],
        by_author:[
          {
            author: "Karlo Soriano",
            "2013-05-09": {date: "2013-05-09", additions: 471, deletions: 0, commits: 1}
          },
          {
            author: "Dmitriy Zaporozhets",
            "2013-05-08": {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}
          }
        ]
      };
147 148
      var correct_total_data = [{date: "2013-05-08", commits: 3},
      {date: "2013-05-09", commits: 1}];
149 150 151
      expect(ContributorsStatGraphUtil.get_total_data(fake_parsed_log, "commits")).toEqual(correct_total_data);
    });
  });
152 153 154 155 156

  describe("#pick_field", function () {
    it("returns the collection with only the specified field and date", function () {
      var fake_parsed_log_total = [{date: "2013-05-09", additions: 471, deletions: 0, commits: 1},
      {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}];
157
      ContributorsStatGraphUtil.pick_field(fake_parsed_log_total, "commits");
158
      var correct_pick_field_data = [{date: "2013-05-09", commits: 1},{date: "2013-05-08", commits: 3}];
159 160 161
      expect(ContributorsStatGraphUtil.pick_field(fake_parsed_log_total, "commits")).toEqual(correct_pick_field_data);
    });
  });
162 163 164 165

  describe("#get_author_data", function () {
    it("returns the log by author sorted by specified field", function () {
      var fake_parsed_log = {
166
        total: [
P
Phil Hughes 已提交
167
          {date: "2013-05-09", additions: 471, deletions: 0, commits: 1},
168 169 170
          {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}
        ],
        by_author: [
P
Phil Hughes 已提交
171
          {
172 173 174 175 176 177 178 179
            author_name: "Karlo Soriano", author_email: "karlo@email.com",
            "2013-05-09": {date: "2013-05-09", additions: 471, deletions: 0, commits: 1}
          },
          {
            author_name: "Dmitriy Zaporozhets", author_email: "dzaporozhets@email.com",
            "2013-05-08": {date: "2013-05-08", additions: 54, deletions: 7, commits: 3}
          }
        ]
180
      };
181 182 183
      var correct_author_data = [
        {author_name:"Dmitriy Zaporozhets",author_email:"dzaporozhets@email.com",dates:{"2013-05-08":3},deletions:7,additions:54,"commits":3},
        {author_name:"Karlo Soriano",author_email:"karlo@email.com",dates:{"2013-05-09":1},deletions:0,additions:471,commits:1}
184 185 186 187
      ];
      expect(ContributorsStatGraphUtil.get_author_data(fake_parsed_log, "commits")).toEqual(correct_author_data);
    });
  });
188 189 190

  describe("#parse_log_entry", function () {
    it("adds the corresponding info from the log entry to the author", function () {
191
      var fake_log_entry =    { author_name: "Karlo Soriano", author_email: "karlo@email.com",
192
        "2013-05-09": {date: "2013-05-09", additions: 471, deletions: 0, commits: 1}
193 194 195 196 197
      };
      var correct_parsed_log = {author_name:"Karlo Soriano",author_email:"karlo@email.com",dates:{"2013-05-09":1},deletions:0,additions:471,commits:1};
      expect(ContributorsStatGraphUtil.parse_log_entry(fake_log_entry, 'commits', null)).toEqual(correct_parsed_log);
    });
  });
198 199

  describe("#in_range", function () {
200
    var date = "2013-05-09";
201
    it("returns true if date_range is null", function () {
202 203
      expect(ContributorsStatGraphUtil.in_range(date, null)).toEqual(true);
    });
204
    it("returns true if date is in range", function () {
205 206 207
      var date_range = [new Date("2013-01-01"), new Date("2013-12-12")];
      expect(ContributorsStatGraphUtil.in_range(date, date_range)).toEqual(true);
    });
208
    it("returns false if date is not in range", function () {
209 210 211 212 213
      var date_range = [new Date("1999-12-01"), new Date("2000-12-01")];
      expect(ContributorsStatGraphUtil.in_range(date, date_range)).toEqual(false);
    });
  });
});