2019-02-25 22:21:13 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-30 00:49:07 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Activity, type: :model do
|
2019-05-06 21:33:33 +08:00
|
|
|
let(:activity) { build :activity }
|
|
|
|
let(:old_activity) { build :activity, :old }
|
2019-02-25 22:21:13 +08:00
|
|
|
|
2017-08-30 00:49:07 +08:00
|
|
|
it 'should be of class Activity' do
|
|
|
|
expect(subject.class).to eq Activity
|
|
|
|
end
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
it 'is valid' do
|
|
|
|
expect(activity).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is valid (old)' do
|
|
|
|
expect(old_activity).to be_valid
|
|
|
|
end
|
|
|
|
|
2017-08-30 00:49:07 +08:00
|
|
|
describe 'Database table' do
|
|
|
|
it { should have_db_column :id }
|
|
|
|
it { should have_db_column :my_module_id }
|
2019-02-13 16:31:10 +08:00
|
|
|
it { should have_db_column :owner_id }
|
2017-08-30 00:49:07 +08:00
|
|
|
it { should have_db_column :type_of }
|
|
|
|
it { should have_db_column :message }
|
|
|
|
it { should have_db_column :created_at }
|
|
|
|
it { should have_db_column :updated_at }
|
|
|
|
it { should have_db_column :project_id }
|
|
|
|
it { should have_db_column :experiment_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Relations' do
|
2019-07-26 18:40:36 +08:00
|
|
|
it { should belong_to(:project).optional }
|
|
|
|
it { should belong_to(:experiment).optional }
|
|
|
|
it { should belong_to(:my_module).optional }
|
2019-02-13 16:31:10 +08:00
|
|
|
it { should belong_to :owner }
|
2019-07-26 18:40:36 +08:00
|
|
|
it { should belong_to(:subject).optional }
|
2017-08-30 00:49:07 +08:00
|
|
|
end
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
describe 'Validations' do
|
2019-05-06 21:33:33 +08:00
|
|
|
describe '#type_of' do
|
|
|
|
it { is_expected.to validate_presence_of :type_of }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#owner' do
|
|
|
|
it { is_expected.to validate_presence_of :owner }
|
|
|
|
end
|
2019-02-25 22:21:13 +08:00
|
|
|
|
2019-05-06 21:33:33 +08:00
|
|
|
describe '#subject_type' do
|
|
|
|
it { is_expected.to validate_inclusion_of(:subject_type).in_array(Extends::ACTIVITY_SUBJECT_TYPES) }
|
2019-02-25 22:21:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.old_activity?' do
|
|
|
|
it 'returns true for old activity' do
|
|
|
|
expect(old_activity.old_activity?).to be_truthy
|
|
|
|
end
|
|
|
|
it 'returns false for activity' do
|
|
|
|
expect(activity.old_activity?).to be_falsey
|
|
|
|
end
|
2017-08-30 00:49:07 +08:00
|
|
|
end
|
2019-02-28 21:01:58 +08:00
|
|
|
|
|
|
|
describe '.save' do
|
|
|
|
it 'adds user to message items' do
|
2019-05-06 21:33:33 +08:00
|
|
|
activity.save
|
2019-02-28 21:01:58 +08:00
|
|
|
|
2019-09-05 14:33:53 +08:00
|
|
|
expect(activity.message_items).to include('user' => be_an(Hash))
|
2019-02-28 21:01:58 +08:00
|
|
|
end
|
|
|
|
end
|
2019-03-19 20:33:32 +08:00
|
|
|
|
|
|
|
describe '.generate_breadcrumbs' do
|
|
|
|
context 'when do not have subject' do
|
|
|
|
it 'does not add breadcrumbs to activity' do
|
|
|
|
expect { old_activity.generate_breadcrumbs }
|
2019-09-05 14:33:53 +08:00
|
|
|
.not_to(change { activity.values['breadcrumbs'] })
|
2019-03-19 20:33:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when have subject' do
|
|
|
|
it 'adds breadcrumbs to activity' do
|
|
|
|
expect { activity.generate_breadcrumbs }
|
2019-09-05 14:33:53 +08:00
|
|
|
.to(change { activity.values['breadcrumbs'] })
|
2019-03-19 20:33:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when subject is a my_module' do
|
|
|
|
let(:activity) { create :activity, subject: (create :my_module) }
|
|
|
|
|
|
|
|
it 'has keys my_module, experiment, project and team' do
|
|
|
|
activity.generate_breadcrumbs
|
|
|
|
expect(activity.breadcrumbs)
|
|
|
|
.to include(:my_module, :experiment, :project, :team)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when subject is a team' do
|
|
|
|
let(:activity) { create :activity, subject: (create :team) }
|
|
|
|
|
|
|
|
it 'has key team' do
|
|
|
|
activity.generate_breadcrumbs
|
|
|
|
expect(activity.breadcrumbs).to include(:team)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-08-30 00:49:07 +08:00
|
|
|
end
|