summaryrefslogtreecommitdiff
path: root/spec/models/board_project_recent_visit_spec.rb
blob: 145a4f5b1a751fcac01dc73344f98eb459fd49a4 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BoardProjectRecentVisit do
  let(:user)    { create(:user) }
  let(:project) { create(:project) }
  let(:board)   { create(:board, project: project) }

  describe 'relationships' do
    it { is_expected.to belong_to(:user) }
    it { is_expected.to belong_to(:project) }
    it { is_expected.to belong_to(:board) }
  end

  describe 'validations' do
    it { is_expected.to validate_presence_of(:user) }
    it { is_expected.to validate_presence_of(:project) }
    it { is_expected.to validate_presence_of(:board) }
  end

  describe '#visited' do
    it 'creates a visit if one does not exists' do
      expect { described_class.visited!(user, board) }.to change(described_class, :count).by(1)
    end

    shared_examples 'was visited previously' do
      let!(:visit) { create :board_project_recent_visit, project: board.project, board: board, user: user, updated_at: 7.days.ago }

      it 'updates the timestamp' do
        freeze_time do
          described_class.visited!(user, board)

          expect(described_class.count).to eq 1
          expect(described_class.first.updated_at).to be_like_time(Time.zone.now)
        end
      end
    end

    it_behaves_like 'was visited previously'

    context 'when we try to create a visit that is not unique' do
      before do
        expect(described_class).to receive(:find_or_create_by).and_raise(ActiveRecord::RecordNotUnique, 'record not unique')
        expect(described_class).to receive(:find_or_create_by).and_return(visit)
      end

      it_behaves_like 'was visited previously'
    end
  end

  describe '#latest' do
    def create_visit(time)
      create :board_project_recent_visit, project: project, user: user, updated_at: time
    end

    it 'returns the most recent visited' do
      create_visit(7.days.ago)
      create_visit(5.days.ago)
      recent = create_visit(1.day.ago)

      expect(described_class.latest(user, project)).to eq recent
    end

    it 'returns last 3 visited boards' do
      create_visit(7.days.ago)
      visit1 = create_visit(3.days.ago)
      visit2 = create_visit(2.days.ago)
      visit3 = create_visit(5.days.ago)

      expect(described_class.latest(user, project, count: 3)).to eq([visit2, visit1, visit3])
    end
  end
end