summaryrefslogtreecommitdiff
path: root/spec/lib/votes_spec.rb
blob: a3c353d5eab64c2fb8e2190adff9382bee5d78db (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
require 'spec_helper'

describe Issue, 'Votes' do
  let(:issue) { create(:issue) }

  describe "#upvotes" do
    it "with no notes has a 0/0 score" do
      issue.upvotes.should == 0
    end

    it "should recognize non-+1 notes" do
      add_note "No +1 here"
      issue.should have(1).note
      issue.notes.first.upvote?.should be_false
      issue.upvotes.should == 0
    end

    it "should recognize a single +1 note" do
      add_note "+1 This is awesome"
      issue.upvotes.should == 1
    end

    it "should recognize multiple +1 notes" do
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      issue.upvotes.should == 2
    end
  end

  describe "#downvotes" do
    it "with no notes has a 0/0 score" do
      issue.downvotes.should == 0
    end

    it "should recognize non--1 notes" do
      add_note "Almost got a -1"
      issue.should have(1).note
      issue.notes.first.downvote?.should be_false
      issue.downvotes.should == 0
    end

    it "should recognize a single -1 note" do
      add_note "-1 This is bad"
      issue.downvotes.should == 1
    end

    it "should recognize multiple -1 notes" do
      add_note "-1 This is bad"
      add_note "-1 Away with this"
      issue.downvotes.should == 2
    end
  end

  describe "#votes_count" do
    it "with no notes has a 0/0 score" do
      issue.votes_count.should == 0
    end

    it "should recognize non notes" do
      add_note "No +1 here"
      issue.should have(1).note
      issue.votes_count.should == 0
    end

    it "should recognize a single +1 note" do
      add_note "+1 This is awesome"
      issue.votes_count.should == 1
    end

    it "should recognize a single -1 note" do
      add_note "-1 This is bad"
      issue.votes_count.should == 1
    end

    it "should recognize multiple notes" do
      add_note "+1 This is awesome"
      add_note "-1 This is bad"
      add_note "+1 I want this"
      issue.votes_count.should == 3
    end
  end

  describe "#upvotes_in_percent" do
    it "with no notes has a 0% score" do
      issue.upvotes_in_percent.should == 0
    end

    it "should count a single 1 note as 100%" do
      add_note "+1 This is awesome"
      issue.upvotes_in_percent.should == 100
    end

    it "should count multiple +1 notes as 100%" do
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      issue.upvotes_in_percent.should == 100
    end

    it "should count fractions for multiple +1 and -1 notes correctly" do
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      add_note "-1 This is bad"
      add_note "+1 me too"
      issue.upvotes_in_percent.should == 75
    end
  end

  describe "#downvotes_in_percent" do
    it "with no notes has a 0% score" do
      issue.downvotes_in_percent.should == 0
    end

    it "should count a single -1 note as 100%" do
      add_note "-1 This is bad"
      issue.downvotes_in_percent.should == 100
    end

    it "should count multiple -1 notes as 100%" do
      add_note "-1 This is bad"
      add_note "-1 Away with this"
      issue.downvotes_in_percent.should == 100
    end

    it "should count fractions for multiple +1 and -1 notes correctly" do
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      add_note "-1 This is bad"
      add_note "+1 me too"
      issue.downvotes_in_percent.should == 25
    end
  end

  def add_note(text)
    issue.notes << create(:note, note: text, project: issue.project)
  end
end