Skip to content

Commit 1f62151

Browse files
author
Nicolas Rodriguez
committed
Improve tests
1 parent 5149b5b commit 1f62151

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

spec/ajax-datatables-rails/orm/active_record_filter_records_spec.rb

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,55 +43,68 @@
4343
end
4444
end
4545

46-
context 'none of columns are connected' do
46+
context 'when none of columns are connected' do
4747
before(:each) do
4848
allow(datatable).to receive(:searchable_columns) { [] }
4949
end
5050

51-
it 'returns empty query result' do
52-
datatable.params[:search] = { value: 'msmith' }
53-
expect(datatable.send(:build_conditions_for_datatable)).to be_blank
54-
end
55-
end
56-
57-
context 'with search query' do
5851
context 'when search value is a string' do
5952
before(:each) do
60-
datatable.params[:search] = { value: 'john', regex: 'false' }
53+
datatable.params[:search] = { value: 'msmith' }
6154
end
6255

63-
it 'returns a filtering query' do
56+
it 'returns empty query result' do
57+
expect(datatable.send(:build_conditions_for_datatable)).to be_blank
58+
end
59+
60+
it 'returns filtered results' do
6461
query = datatable.send(:build_conditions_for_datatable)
6562
results = records.where(query).map(&:username)
66-
expect(results).to include('johndoe')
67-
expect(results).not_to include('msmith')
63+
expect(results).to eq ['johndoe', 'msmith']
6864
end
6965
end
7066

7167
context 'when search value is space-separated string' do
7268
before(:each) do
73-
datatable.instance_variable_set :@searchable_columns, []
74-
datatable.params[:search] = { value: 'foo bar', regex: 'false' }
69+
datatable.params[:search] = { value: 'foo bar' }
7570
end
7671

77-
it 'returns a filtering query' do
72+
it 'returns empty query result' do
73+
expect(datatable.send(:build_conditions_for_datatable)).to be_blank
74+
end
75+
76+
it 'returns filtered results' do
7877
query = datatable.send(:build_conditions_for_datatable)
7978
results = records.where(query).map(&:username)
8079
expect(results).to eq ['johndoe', 'msmith']
8180
end
8281
end
82+
end
8383

84-
context 'when search value is comma-separated string' do
84+
context 'with search query' do
85+
context 'when search value is a string' do
8586
before(:each) do
86-
datatable.instance_variable_set :@searchable_columns, []
87-
datatable.params[:search] = { value: 'foo, bar', regex: 'false' }
88-
expect(datatable).to receive(:global_search_delimiter).and_return(', ')
87+
datatable.params[:search] = { value: 'john', regex: 'false' }
8988
end
9089

9190
it 'returns a filtering query' do
9291
query = datatable.send(:build_conditions_for_datatable)
9392
results = records.where(query).map(&:username)
94-
expect(results).to eq ['johndoe', 'msmith']
93+
expect(results).to include('johndoe')
94+
expect(results).not_to include('msmith')
95+
end
96+
end
97+
98+
context 'when search value is space-separated string' do
99+
before(:each) do
100+
datatable.params[:search] = { value: 'john doe', regex: 'false' }
101+
end
102+
103+
it 'returns a filtering query' do
104+
query = datatable.send(:build_conditions_for_datatable)
105+
results = records.where(query).map(&:username)
106+
expect(results).to eq ['johndoe']
107+
expect(results).not_to include('msmith')
95108
end
96109
end
97110
end

0 commit comments

Comments
 (0)