summaryrefslogtreecommitdiff
path: root/test/models/interaction_test.rb
blob: 6600b4ad1f64b0d588e95165a7b13cda5bf06756 (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
# frozen_string_literal: true

require 'test_helper'

class InteractionTest < ActiveSupport::TestCase
  setup do
    @subject = create(:post)
    @user = create(:user)
  end

  test 'voting without a user fails' do
    assert_raises ActiveRecord::RecordInvalid do
      @subject.upvotes.create!(user: nil, up: true)
    end
  end

  test 'voting up given no existing vote should upvote the subject' do
    @subject.votes.create(user: @user, up: true)
    assert_equal 1, @subject.upvotes.count
    assert_equal 1, @subject.score
  end

  test 'voting down given no existing vote should downvote the subject' do
    @subject.votes.create(user: @user, up: false)
    assert_equal 1, @subject.downvotes.count
    assert_equal(-1, @subject.score)
  end

  test 'voting false given no existing vote should do nothing' do
    @subject.votes.where(user: @user).destroy_all
    assert_equal 0, @subject.votes.count
    assert_equal 0, @subject.score
  end

  test 'voting up given an upvote should do nothing' do
    @subject.votes.create(user: @user, up: true)
    @subject.votes.create(user: @user, up: true)
    assert_equal 1, @subject.upvotes.count
    assert_equal 1, @subject.score
  end

  test 'voting down given an upvote should downvote the subject' do
    @subject.votes.create(user: @user, up: true)
    @subject.votes.create(user: @user, up: false)
    assert_equal 0, @subject.upvotes.count
    assert_equal 1, @subject.downvotes.count
    assert_equal(-1, @subject.score)
  end

  test 'voting false given an upvote should redact the upvote' do
    @subject.votes.create(user: @user, up: true)
    @subject.votes.where(user: @user).destroy_all
    assert_equal 0, @subject.votes.count
    assert_equal 0, @subject.score
  end

  test 'voting up given a downvote should upvote the subject' do
    @subject.votes.create(user: @user, up: false)
    @subject.votes.create(user: @user, up: true)
    assert_equal 1, @subject.upvotes.count
    assert_equal 0, @subject.downvotes.count
    assert_equal 1, @subject.score
  end

  test 'voting down given a downvote should do nothing' do
    @subject.votes.create(user: @user, up: false)
    @subject.votes.create(user: @user, up: false)
    assert_equal 1, @subject.downvotes.count
    assert_equal(-1, @subject.score)
  end

  test 'voting false given a downvote should redact the downvote' do
    @subject.votes.create(user: @user, up: false)
    @subject.votes.where(user: @user).destroy_all
    assert_equal 0, @subject.votes.count
    assert_equal 0, @subject.score
  end

  test 'faving without a user fails' do
    assert_raises ActiveRecord::RecordInvalid do
      @subject.faves.create!(user: nil)
    end
  end

  test 'faving true given no existing fave should fave the subject' do
    @subject.faves.create(user: @user)
    assert_equal 1, @subject.faves.count
    assert_equal 1, @subject.faves_count
  end

  test 'faving false given no existing fave should do nothing' do
    @subject.faves.where(user: @user).destroy_all
    assert_equal 0, @subject.faves.count
    assert_equal 0, @subject.faves_count
  end

  test 'faving true given an existing fave should do nothing' do
    @subject.faves.create(user: @user)
    @subject.faves.create(user: @user)
    assert_equal 1, @subject.faves.count
    assert_equal 1, @subject.faves_count
  end

  test 'faving false given an existing fave should redact the fave' do
    @subject.faves.create(user: @user)
    @subject.faves.where(user: @user).destroy_all
    assert_equal 0, @subject.faves.count
    assert_equal 0, @subject.faves_count
  end
end