2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-07 22:28:28 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe RepositoryListValue, type: :model do
|
2019-05-07 19:47:50 +08:00
|
|
|
let(:repository_list_value) { build :repository_list_value }
|
|
|
|
|
|
|
|
it 'is valid' do
|
|
|
|
expect(repository_list_value).to be_valid
|
|
|
|
end
|
|
|
|
|
2018-02-07 22:28:28 +08:00
|
|
|
it 'should be of class RepositoryListValue' do
|
|
|
|
expect(subject.class).to eq RepositoryListValue
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Database table' do
|
|
|
|
it { should have_db_column :created_by_id }
|
|
|
|
it { should have_db_column :last_modified_by_id }
|
2018-02-09 23:28:21 +08:00
|
|
|
it { should have_db_column :repository_list_item_id }
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Relations' do
|
|
|
|
it { should belong_to(:created_by).class_name('User') }
|
|
|
|
it { should belong_to(:last_modified_by).class_name('User') }
|
2018-02-09 23:28:21 +08:00
|
|
|
it { should belong_to(:repository_list_item) }
|
2018-02-07 22:28:28 +08:00
|
|
|
it { should accept_nested_attributes_for(:repository_cell) }
|
|
|
|
end
|
|
|
|
|
2018-03-06 23:32:39 +08:00
|
|
|
describe '#formatted' do
|
2019-05-07 19:47:50 +08:00
|
|
|
let(:list_item) { create :repository_list_item, data: 'my item' }
|
2018-02-07 22:28:28 +08:00
|
|
|
|
2018-03-06 23:32:39 +08:00
|
|
|
it 'returns the formatted data of a selected item' do
|
2019-05-07 19:47:50 +08:00
|
|
|
repository_list_value = create :repository_list_value, repository_list_item: list_item
|
|
|
|
|
2018-02-09 18:38:52 +08:00
|
|
|
expect(repository_list_value.reload.formatted).to eq 'my item'
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|
|
|
|
end
|
2018-03-06 23:32:39 +08:00
|
|
|
|
|
|
|
describe '#data' do
|
2019-05-07 19:47:50 +08:00
|
|
|
let(:list_item) { create :repository_list_item, data: 'my item' }
|
|
|
|
let(:repository_list_value) { create :repository_list_value, repository_list_item: list_item }
|
2018-03-06 23:32:39 +08:00
|
|
|
|
|
|
|
it 'returns the data of a selected item' do
|
|
|
|
expect(repository_list_value.reload.data).to eq 'my item'
|
|
|
|
end
|
|
|
|
|
2019-05-07 19:47:50 +08:00
|
|
|
# Not sure if this test make sense, because validation will fail before hit this function. Updated in SCI-3183
|
|
|
|
it 'retuns only the item related to the list' do
|
|
|
|
repository_list_value.repository_list_item = nil
|
|
|
|
repository_list_value.save(validate: false)
|
|
|
|
|
|
|
|
expect(repository_list_value.reload.data).to be_nil
|
2018-03-06 23:32:39 +08:00
|
|
|
end
|
|
|
|
end
|
2019-12-08 23:53:59 +08:00
|
|
|
|
|
|
|
describe 'data_changed?' do
|
|
|
|
context 'when has new data' do
|
|
|
|
it do
|
|
|
|
expect(repository_list_value.data_changed?('-1')).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when has same data' do
|
|
|
|
it do
|
|
|
|
repository_list_value.save
|
|
|
|
id = repository_list_value.repository_list_item.id
|
|
|
|
|
|
|
|
expect(repository_list_value.data_changed?(id)).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'update_data!' do
|
|
|
|
let(:user) { create :user }
|
|
|
|
let(:new_list_item) do
|
|
|
|
create :repository_list_item, repository_column: repository_list_value.repository_list_item.repository_column
|
|
|
|
end
|
2019-12-09 15:34:27 +08:00
|
|
|
context 'when update data' do
|
|
|
|
it 'should change last_modified_by and data' do
|
|
|
|
repository_list_value.save
|
2019-12-08 23:53:59 +08:00
|
|
|
|
2019-12-09 15:34:27 +08:00
|
|
|
expect { repository_list_value.update_data!(new_list_item.id, user) }
|
|
|
|
.to(change { repository_list_value.reload.last_modified_by.id }
|
|
|
|
.and(change { repository_list_value.reload.data }))
|
|
|
|
end
|
|
|
|
end
|
2019-12-08 23:53:59 +08:00
|
|
|
end
|
2019-12-09 20:48:24 +08:00
|
|
|
|
|
|
|
describe 'self.new_with_payload' do
|
|
|
|
let(:user) { create :user }
|
|
|
|
let(:column) { create :repository_column }
|
|
|
|
let(:cell) { build :repository_cell, repository_column: column }
|
|
|
|
let(:list_item) { create :repository_list_item, repository: column.repository, repository_column: column }
|
|
|
|
let(:attributes) do
|
|
|
|
{
|
|
|
|
repository_cell: cell,
|
|
|
|
created_by: user,
|
|
|
|
last_modified_by: user
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect(RepositoryListValue.new_with_payload(list_item.id, attributes))
|
|
|
|
.to be_an_instance_of RepositoryListValue
|
|
|
|
end
|
|
|
|
end
|
2018-02-07 22:28:28 +08:00
|
|
|
end
|