require 'rails_helper' describe RepositoryDatatableService do let!(:team) { create :team } let!(:user) { create :user, email: 'user_one@asdf.com' } let!(:repository) do create :repository, name: 'my repo', created_by: user, team: team end let!(:repository_column) do create :repository_column, name: 'My column', data_type: :RepositoryListValue end let!(:repository_state) do RepositoryTableState.create( repository: repository, user: user, state: Constants::REPOSITORY_TABLE_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 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 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 { order: { 0 => { column: '3', dir: 'asc' } }, 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' expect(contitions[:order_by_column]).to eq( { column: 3, dir: 'asc' } ) end end describe '#sortable_columns' do it 'returns an array of all columns that are sortable' do columns = subject.send(:sortable_columns) 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 params = { order: { 0 => { column: '3', dir: 'asc' } }, search: { value: '' } } 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 params = { order: { 0 => { column: '3', dir: 'desc' } }, search: { value: '' } } 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 params = { order: { 0 => { column: '4', dir: 'desc' } }, search: { value: 'test' } } 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