Skip to content

Commit

Permalink
Merge pull request #426 from wayhomeservices/searchable-tests
Browse files Browse the repository at this point in the history
Adding searchable: false test cases
  • Loading branch information
n-rodriguez authored Jul 25, 2024
2 parents ff85109 + 74da2c5 commit 07fa8e1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# CHANGELOG

* Implementing `searchable: false` tests

## 1.5.0 (2024-04-08)

* Drop support of Rails 5.2
Expand Down
8 changes: 8 additions & 0 deletions spec/ajax-datatables-rails/datatable/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@
end
end

describe 'unsearchable column' do
let(:column) { datatable.datatable.columns.find{ |c| c.data == 'email_hash' } }

it 'is not searchable' do
expect(column.searchable?).to eql(false)
end
end

describe '#formatter' do
let(:datatable) { DatatableWithFormater.new(sample_params) }
let(:column) { datatable.datatable.columns.find { |c| c.data == 'last_name' } }
Expand Down
4 changes: 2 additions & 2 deletions spec/ajax-datatables-rails/datatable/datatable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
end

shared_examples 'columns methods' do
it 'has 7 columns' do
expect(datatable.columns.count).to eq(7)
it 'has 8 columns' do
expect(datatable.columns.count).to eq(8)
end

it 'child class' do
Expand Down
35 changes: 23 additions & 12 deletions spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@
expect { datatable.filter_records }.to raise_error(ArgumentError)
end

it 'performs a simple search first' do
datatable.params[:search] = { value: 'msmith' }
expect(datatable).to receive(:build_conditions_for_datatable)
datatable.filter_records(records)
context 'with simple search' do
before do
datatable.params[:search] = { value: 'msmith' }
end

it 'performs a simple search first' do
expect(datatable).to receive(:build_conditions_for_datatable)
datatable.filter_records(records)
end

it 'does not search unsearchable fields' do
criteria = datatable.filter_records(records)
expect(criteria.to_sql).not_to include('email_hash')
end
end


it 'performs a composite search second' do
datatable.params[:search] = { value: '' }
expect(datatable).to receive(:build_conditions_for_selected_columns)
Expand Down Expand Up @@ -246,7 +257,7 @@

context 'when range is empty' do
it 'does not filter records' do
datatable.params[:columns]['6'][:search][:value] = '-'
datatable.params[:columns]['7'][:search][:value] = '-'
expect(datatable.data.size).to eq 2
item = datatable.data.first
expect(item[:last_name]).to eq 'Doe'
Expand All @@ -255,21 +266,21 @@

context 'when start date is filled' do
it 'filters records created after this date' do
datatable.params[:columns]['6'][:search][:value] = '31/12/1999-'
datatable.params[:columns]['7'][:search][:value] = '31/12/1999-'
expect(datatable.data.size).to eq 2
end
end

context 'when end date is filled' do
it 'filters records created before this date' do
datatable.params[:columns]['6'][:search][:value] = '-31/12/1999'
datatable.params[:columns]['7'][:search][:value] = '-31/12/1999'
expect(datatable.data.size).to eq 0
end
end

context 'when both date are filled' do
it 'filters records created between the range' do
datatable.params[:columns]['6'][:search][:value] = '01/12/1999-15/01/2000'
datatable.params[:columns]['7'][:search][:value] = '01/12/1999-15/01/2000'
expect(datatable.data.size).to eq 1
end
end
Expand All @@ -278,7 +289,7 @@
context 'when range is empty' do
it 'filters records' do
datatable.params[:columns]['0'][:search][:value] = 'doe'
datatable.params[:columns]['6'][:search][:value] = '-'
datatable.params[:columns]['7'][:search][:value] = '-'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:last_name]).to eq 'Doe'
Expand All @@ -288,7 +299,7 @@
context 'when start date is filled' do
it 'filters records' do
datatable.params[:columns]['0'][:search][:value] = 'doe'
datatable.params[:columns]['6'][:search][:value] = '01/12/1999-'
datatable.params[:columns]['7'][:search][:value] = '01/12/1999-'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:last_name]).to eq 'Doe'
Expand All @@ -298,7 +309,7 @@
context 'when end date is filled' do
it 'filters records' do
datatable.params[:columns]['0'][:search][:value] = 'doe'
datatable.params[:columns]['6'][:search][:value] = '-15/01/2000'
datatable.params[:columns]['7'][:search][:value] = '-15/01/2000'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:last_name]).to eq 'Doe'
Expand All @@ -308,7 +319,7 @@
context 'when both date are filled' do
it 'filters records' do
datatable.params[:columns]['0'][:search][:value] = 'doe'
datatable.params[:columns]['6'][:search][:value] = '01/12/1999-15/01/2000'
datatable.params[:columns]['7'][:search][:value] = '01/12/1999-15/01/2000'
expect(datatable.data.size).to eq 1
item = datatable.data.first
expect(item[:last_name]).to eq 'Doe'
Expand Down
2 changes: 2 additions & 0 deletions spec/support/datatables/complex_datatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def view_columns
last_name: { source: 'User.last_name' },
full_name: { source: 'full_name' },
post_id: { source: 'User.post_id', orderable: false },
email_hash: { source: 'email_hash', searchable: false },
created_at: { source: 'User.created_at' },
}
end
Expand All @@ -22,6 +23,7 @@ def data
last_name: record.last_name,
full_name: record.full_name,
post_id: record.post_id,
email_hash: record.email_hash,
created_at: record.created_at,
}
end
Expand Down
6 changes: 6 additions & 0 deletions spec/support/helpers/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def sample_params
}
},
'6' => {
'data' => 'email_hash', 'name' => '', 'searchable' => 'false', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
}
},
'7' => {
'data' => 'created_at', 'name' => '', 'searchable' => 'true', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
Expand Down
8 changes: 8 additions & 0 deletions spec/support/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# frozen_string_literal: true

require 'digest'

class User < ActiveRecord::Base
def full_name
"#{first_name} #{last_name}"
end

def email_hash
return nil if email.nil?

Digest::SHA256.hexdigest email
end
end

0 comments on commit 07fa8e1

Please sign in to comment.