BigW Consortium Gitlab

votes_spec.rb 3.4 KB
Newer Older
1 2
require 'spec_helper'

3 4
describe Issue, 'Votes' do
  let(:issue) { create(:issue) }
Riyad Preukschas committed
5

Riyad Preukschas committed
6 7
  describe "#upvotes" do
    it "with no notes has a 0/0 score" do
8
      issue.upvotes.should == 0
Riyad Preukschas committed
9
    end
10

Riyad Preukschas committed
11
    it "should recognize non-+1 notes" do
12 13 14 15
      add_note "No +1 here"
      issue.should have(1).note
      issue.notes.first.upvote?.should be_false
      issue.upvotes.should == 0
Riyad Preukschas committed
16 17 18
    end

    it "should recognize a single +1 note" do
19 20
      add_note "+1 This is awesome"
      issue.upvotes.should == 1
Riyad Preukschas committed
21
    end
22

Riyad Preukschas committed
23
    it "should recognize multiple +1 notes" do
24 25 26
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      issue.upvotes.should == 2
Riyad Preukschas committed
27
    end
28 29
  end

Riyad Preukschas committed
30 31
  describe "#downvotes" do
    it "with no notes has a 0/0 score" do
32
      issue.downvotes.should == 0
Riyad Preukschas committed
33 34 35
    end

    it "should recognize non--1 notes" do
36 37 38 39
      add_note "Almost got a -1"
      issue.should have(1).note
      issue.notes.first.downvote?.should be_false
      issue.downvotes.should == 0
Riyad Preukschas committed
40 41 42
    end

    it "should recognize a single -1 note" do
43 44
      add_note "-1 This is bad"
      issue.downvotes.should == 1
Riyad Preukschas committed
45 46 47
    end

    it "should recognize multiple -1 notes" do
48 49 50
      add_note "-1 This is bad"
      add_note "-1 Away with this"
      issue.downvotes.should == 2
Riyad Preukschas committed
51
    end
52
  end
Riyad Preukschas committed
53 54 55

  describe "#votes_count" do
    it "with no notes has a 0/0 score" do
56
      issue.votes_count.should == 0
Riyad Preukschas committed
57 58 59
    end

    it "should recognize non notes" do
60 61 62
      add_note "No +1 here"
      issue.should have(1).note
      issue.votes_count.should == 0
Riyad Preukschas committed
63 64 65
    end

    it "should recognize a single +1 note" do
66 67
      add_note "+1 This is awesome"
      issue.votes_count.should == 1
Riyad Preukschas committed
68 69 70
    end

    it "should recognize a single -1 note" do
71 72
      add_note "-1 This is bad"
      issue.votes_count.should == 1
Riyad Preukschas committed
73 74 75
    end

    it "should recognize multiple notes" do
76 77 78 79
      add_note "+1 This is awesome"
      add_note "-1 This is bad"
      add_note "+1 I want this"
      issue.votes_count.should == 3
Riyad Preukschas committed
80 81
    end
  end
Riyad Preukschas committed
82 83 84

  describe "#upvotes_in_percent" do
    it "with no notes has a 0% score" do
85
      issue.upvotes_in_percent.should == 0
Riyad Preukschas committed
86 87 88
    end

    it "should count a single 1 note as 100%" do
89 90
      add_note "+1 This is awesome"
      issue.upvotes_in_percent.should == 100
Riyad Preukschas committed
91 92 93
    end

    it "should count multiple +1 notes as 100%" do
94 95 96
      add_note "+1 This is awesome"
      add_note "+1 I want this"
      issue.upvotes_in_percent.should == 100
Riyad Preukschas committed
97 98 99
    end

    it "should count fractions for multiple +1 and -1 notes correctly" do
100 101 102 103 104
      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
Riyad Preukschas committed
105 106 107 108 109
    end
  end

  describe "#downvotes_in_percent" do
    it "with no notes has a 0% score" do
110
      issue.downvotes_in_percent.should == 0
Riyad Preukschas committed
111 112 113
    end

    it "should count a single -1 note as 100%" do
114 115
      add_note "-1 This is bad"
      issue.downvotes_in_percent.should == 100
Riyad Preukschas committed
116 117 118
    end

    it "should count multiple -1 notes as 100%" do
119 120 121
      add_note "-1 This is bad"
      add_note "-1 Away with this"
      issue.downvotes_in_percent.should == 100
Riyad Preukschas committed
122 123 124
    end

    it "should count fractions for multiple +1 and -1 notes correctly" do
125 126 127 128 129
      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
Riyad Preukschas committed
130 131
    end
  end
132 133 134 135

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