adds specs for duplicate class

This commit is contained in:
zmagod 2018-04-12 13:00:21 +02:00
parent 6291857bbf
commit 7f93cf6220

View file

@ -0,0 +1,83 @@
require 'rails_helper'
describe RepositoryActions::Duplicate do
let!(:user) { create :user }
let!(:repository) { create :repository }
let!(:list_column) do
create(:repository_column, name: 'list',
repository: repository,
created_by: user,
data_type: 'RepositoryListValue')
end
let!(:text_column) do
create(:repository_column, name: 'text',
repository: repository,
created_by: user,
data_type: 'RepositoryTextValue')
end
describe '#call' do
before do
@rows_ids = []
3.times do |index|
row = create :repository_row, name: "row (#{index})",
repository: repository
create :repository_text_value, data: "text (#{index})",
repository_cell_attributes: {
repository_row: row,
repository_column: text_column
}
create :repository_list_value,
repository_list_item: create(:repository_list_item,
repository: repository,
repository_column: list_column,
data: "list item (#{index})"),
repository_cell_attributes: {
repository_row: row,
repository_column: list_column
}
@rows_ids << row.id.to_s
end
end
it 'generates a copy of selected items' do
expect(repository.repository_rows.reload.size).to eq 3
described_class.new(user, repository, @rows_ids).call
expect(repository.repository_rows.reload.size).to eq 6
end
it 'generates an exact copy of the row with custom column values' do
described_class.new(user, repository, [@rows_ids.first]).call
duplicated_row = repository.repository_rows.reload.last
expect(duplicated_row.name).to eq 'row (0) (1)'
duplicated_row.repository_cells.each do |cell|
if cell.value_type == 'RepositoryListValue'
expect(cell.value.data).to eq 'list item (0)'
else
expect(cell.value.data).to eq 'text (0)'
end
end
end
it 'prevents to copy items that do not already belong to repository' do
new_repository = create :repository, name: 'new repo'
new_row = create :repository_row, name: 'other row',
repository: new_repository
described_class.new(user, repository, [new_row.id]).call
expect(repository.repository_rows.reload.size).to eq 3
end
it 'returns the number of copied items' do
service_obj = described_class.new(user, repository, @rows_ids)
service_obj.call
expect(service_obj.number_of_duplicated_items).to eq 3
end
it 'returns the number of copied items' do
service_obj = described_class.new(user, repository, [])
service_obj.call
expect(service_obj.number_of_duplicated_items).to eq 0
end
end
end