2018-10-10 06:09:32 +08:00
|
|
|
# frozen_string_literal: true
|
2018-03-09 21:22:00 +08:00
|
|
|
|
2019-03-13 16:06:48 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2018-03-09 21:22:00 +08:00
|
|
|
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
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
let!(:repository_column) do
|
2019-03-13 16:06:48 +08:00
|
|
|
create :repository_column,
|
|
|
|
name: 'My column',
|
|
|
|
data_type: :RepositoryListValue,
|
|
|
|
repository: repository
|
2018-03-09 21:22:00 +08:00
|
|
|
end
|
|
|
|
let!(:repository_state) do
|
2018-04-20 22:31:28 +08:00
|
|
|
RepositoryTableStateService.new(user, repository).create_default_state
|
2018-03-09 21:22:00 +08:00
|
|
|
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
|
2018-03-09 21:22:00 +08:00
|
|
|
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
|
|
|
|
}
|
2018-03-09 21:22:00 +08:00
|
|
|
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)
|
2018-03-09 21:22:00 +08:00
|
|
|
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(
|
2019-07-26 18:40:36 +08:00
|
|
|
column: 3, dir: 'asc'
|
2018-03-09 21:43:12 +08:00
|
|
|
)
|
2018-03-09 21:22:00 +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
|
2018-03-09 21:22:00 +08:00
|
|
|
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' } },
|
2018-03-09 21:22:00 +08:00
|
|
|
search: { value: '' } }
|
2018-03-09 21:43:12 +08:00
|
|
|
subject = RepositoryDatatableService.new(repository,
|
|
|
|
params,
|
|
|
|
user)
|
2018-03-09 21:22:00 +08:00
|
|
|
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' } },
|
2018-03-09 21:22:00 +08:00
|
|
|
search: { value: '' } }
|
2018-03-09 21:43:12 +08:00
|
|
|
subject = RepositoryDatatableService.new(repository,
|
|
|
|
params,
|
|
|
|
user)
|
2018-03-09 21:22:00 +08:00
|
|
|
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' } },
|
2018-03-09 21:22:00 +08:00
|
|
|
search: { value: 'test' } }
|
2018-03-09 21:43:12 +08:00
|
|
|
subject = RepositoryDatatableService.new(repository,
|
|
|
|
params,
|
|
|
|
user)
|
2018-03-09 21:22:00 +08:00
|
|
|
expect(subject.repository_rows.first.name).to eq 'test'
|
|
|
|
expect(subject.repository_rows.count).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|