scinote-web/spec/services/repository_datatable_service_spec.rb

130 lines
4.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
describe RepositoryDatatableService do
let!(:team) { create :team }
let!(:user) { create :user, email: 'user_one@asdf.com' }
let!(:repository) do
2018-03-09 21:43:12 +08:00
create :repository, name: 'my repo',
created_by: user,
team: team
end
let!(:repository_column) do
2018-03-09 21:43:12 +08:00
create :repository_column, name: 'My column',
data_type: :RepositoryListValue
end
let!(:repository_state) do
RepositoryTableStateService.new(user, repository).create_default_state
end
let!(:repository_row) do
create :repository_row, name: 'A row',
repository: repository,
created_by: user,
last_modified_by: user
end
let!(:repository_row_two) do
create :repository_row, name: 'B row',
repository: repository,
created_by: user,
last_modified_by: user
end
let!(:list_item) do
2018-03-09 21:43:12 +08:00
create :repository_list_item,
data: 'bug',
repository: repository,
repository_column: repository_column,
created_by: user,
last_modified_by: user
end
let!(:repository_list_value) do
2018-03-09 21:43:12 +08:00
create :repository_list_value,
repository_list_item: list_item,
created_by: user,
last_modified_by: user,
repository_cell_attributes: {
repository_column: repository_column,
repository_row: repository_row
}
end
context 'object' do
let(:params) do
2018-04-04 20:44:32 +08:00
{ order: { 0 => { column: '3', dir: 'asc' } },
2018-03-09 21:43:12 +08:00
search: { value: 'row' } }
end
let(:subject) do
RepositoryDatatableService.new(repository, params, user)
end
describe '#build_conditions/1' do
it 'parsers the contitions' do
contitions = subject.send(:build_conditions, params)
expect(contitions[:search_value]).to eq 'row'
2018-03-09 21:43:12 +08:00
expect(contitions[:order_by_column]).to eq(
2018-04-04 20:44:32 +08:00
{ column: 3, dir: 'asc' }
2018-03-09 21:43:12 +08:00
)
end
end
describe '#sortable_columns' do
it 'returns an array of all columns that are sortable' do
columns = subject.send(:sortable_columns)
2018-04-04 20:44:32 +08:00
expect(columns.length).to eq 6
end
end
describe '#sort_null_direction' do
it 'returns LAST if value is ascending' do
result = subject.send(:sort_null_direction, 'ASC')
expect(result).to eq 'LAST'
end
it 'returns FIRST if value is not ascending' do
result = subject.send(:sort_null_direction, 'DESC')
expect(result).to eq 'FIRST'
end
end
end
describe 'ordering' do
it 'is ordered by row name asc' do
2018-04-04 20:44:32 +08:00
params = { order: { 0 => { column: '3', dir: 'asc' } },
search: { value: '' } }
2018-03-09 21:43:12 +08:00
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'A row'
expect(subject.repository_rows.last.name).to eq 'B row'
end
it 'is ordered by row name desc' do
2018-04-04 20:44:32 +08:00
params = { order: { 0 => { column: '3', dir: 'desc' } },
search: { value: '' } }
2018-03-09 21:43:12 +08:00
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'B row'
expect(subject.repository_rows.last.name).to eq 'A row'
end
end
describe 'search' do
before do
create :repository_row, name: 'test',
repository: repository,
created_by: user,
last_modified_by: user
end
it 'returns only the searched entity' do
2018-04-04 20:44:32 +08:00
params = { order: { 0 => { column: '4', dir: 'desc' } },
search: { value: 'test' } }
2018-03-09 21:43:12 +08:00
subject = RepositoryDatatableService.new(repository,
params,
user)
expect(subject.repository_rows.first.name).to eq 'test'
expect(subject.repository_rows.count).to eq 1
end
end
end