user_interacted_project_spec.rb 1.9 KB
Newer Older
1 2
require 'spec_helper'

Andreas Brandl's avatar
Andreas Brandl committed
3
describe UserInteractedProject do
4 5 6 7 8 9 10 11
  describe '.track' do
    subject { described_class.track(event) }
    let(:event) { build(:event) }

    Event::ACTIONS.each do |action|
      context "for all actions (event types)" do
        let(:event) { build(:event, action: action) }
        it 'creates a record' do
Andreas Brandl's avatar
Andreas Brandl committed
12
          expect { subject }.to change { described_class.count }.from(0).to(1)
13 14 15 16 17
        end
      end
    end

    it 'sets project accordingly' do
18
      subject
Andreas Brandl's avatar
Andreas Brandl committed
19
      expect(described_class.first.project).to eq(event.project)
20 21 22
    end

    it 'sets user accordingly' do
23
      subject
Andreas Brandl's avatar
Andreas Brandl committed
24
      expect(described_class.first.user).to eq(event.author)
25 26 27 28 29 30
    end

    it 'only creates a record once per user/project' do
      expect do
        subject
        described_class.track(event)
Andreas Brandl's avatar
Andreas Brandl committed
31
      end.to change { described_class.count }.from(0).to(1)
32
    end
33 34 35 36 37

    describe 'with an event without a project' do
      let(:event) { build(:event, project: nil) }

      it 'ignores the event' do
Andreas Brandl's avatar
Andreas Brandl committed
38
        expect { subject }.not_to change { described_class.count }
39 40
      end
    end
41 42
  end

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  describe '.available?' do
    before do
      described_class.instance_variable_set('@available_flag', nil)
    end

    it 'checks schema version and properly caches positive result' do
      expect(ActiveRecord::Migrator).to receive(:current_version).and_return(described_class::REQUIRED_SCHEMA_VERSION - 1 - rand(1000))
      expect(described_class.available?).to be_falsey
      expect(ActiveRecord::Migrator).to receive(:current_version).and_return(described_class::REQUIRED_SCHEMA_VERSION + rand(1000))
      expect(described_class.available?).to be_truthy
      expect(ActiveRecord::Migrator).not_to receive(:current_version)
      expect(described_class.available?).to be_truthy # cached response
    end
  end

58 59
  it { is_expected.to validate_presence_of(:project_id) }
  it { is_expected.to validate_presence_of(:user_id) }
60
end