/
data_table_spec.rb
83 lines (59 loc) · 2.42 KB
/
data_table_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
require "spec_helper"
describe DataTable do
include DataTable::ClassMethods
context "on being included" do
it "should extend ClassMethods" do
klass = Class.new
mock(klass).send(:extend, DataTable::ClassMethods)
mock(klass).send(:extend, DataTable::Mongoid::ClassMethods)
klass.instance_eval %{include DataTable}
end
end
context "#for_data_table" do
it "should produce JSON for the datatables plugin to consume" do
params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => "10", :sEcho => "1"}
controller = mock!.params { params }.subject
fields = %w(foo bar baz)
search_fields = %w(foo bar)
block = :block
mock(self).count { 42 }
objects = mock!.total_entries { 10 }.subject
mock(self)._find_objects(params, fields, search_fields) { objects }
mock(self)._yield_and_render_array(controller, objects, block) { :results }
result = for_data_table(controller, fields, search_fields, block)
result.should == {:sEcho => 1, :iTotalRecords => 42, :iTotalDisplayRecords => 10, :aaData => :results}.to_json.html_safe
end
end
context "#_yield_and_render_array" do
it "should walk through the array and render it, passing in the appropriate local name" do
block = lambda {|x| mock!.map { [42] }.subject }
result = _yield_and_render_array Object.new, [:foo], block
result.should == [[42]]
end
end
context "#_page" do
context "with a display length of 10" do
it "should return 1 when start is blank" do
send(:_page, {:iDisplayStart => "", :iDisplayLength => "10"}).should == 1
end
it "should return 1 when start is 0" do
send(:_page, {:iDisplayStart => "0", :iDisplayLength => "10"}).should == 1
end
it "should return 2 when start is 10" do
send(:_page, {:iDisplayStart => "10", :iDisplayLength => "10"}).should == 2
end
end
end
context "#_per_page" do
it "should return 10 given an iDisplayLength of 10" do
send(:_per_page, {:iDisplayLength => "10"}).should == 10
end
it "should return a default of 25 given an invalid iDisplayLength" do
send(:_per_page, {:iDisplayLength => "foobar"}).should == 25
end
it "should return self.count given an iDisplayLength of -1" do
mock(self).count { :all }
send(:_per_page, {:iDisplayLength => "-1"}).should == :all
end
end
end