scinote-web/spec/services/repository_datatable_service_spec.rb

114 lines
3.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2019-03-13 16:06:48 +08:00
require 'rails_helper'
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
2019-03-13 16:06:48 +08:00
create :repository_column,
name: 'My column',
data_type: :RepositoryListValue,
repository: repository
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_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(
column: 3, dir: 'asc'
2018-03-09 21:43:12 +08:00
)
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'
2020-01-14 00:17:07 +08:00
expect(subject.repository_rows.length).to eq 1
end
end
end