|
3 | 3 | describe AjaxDatatablesRails::Base do |
4 | 4 |
|
5 | 5 | describe 'an instance' do |
6 | | - let(:view) { double('view', params: sample_params) } |
7 | | - |
8 | | - it 'requires a view_context' do |
| 6 | + it 'requires a hash of params' do |
9 | 7 | expect { described_class.new }.to raise_error ArgumentError |
10 | 8 | end |
11 | 9 |
|
12 | 10 | it 'accepts an options hash' do |
13 | | - datatable = described_class.new(view, foo: 'bar') |
| 11 | + datatable = described_class.new(sample_params, foo: 'bar') |
14 | 12 | expect(datatable.options).to eq(foo: 'bar') |
15 | 13 | end |
16 | 14 | end |
17 | 15 |
|
18 | 16 | context 'Public API' do |
19 | | - let(:view) { double('view', params: sample_params) } |
20 | | - |
21 | 17 | describe '#view_columns' do |
22 | 18 | it 'raises an error if not defined by the user' do |
23 | | - datatable = described_class.new(view) |
| 19 | + datatable = described_class.new(sample_params) |
24 | 20 | expect { datatable.view_columns }.to raise_error NotImplementedError |
25 | 21 | end |
26 | 22 |
|
27 | 23 | context 'child class implements view_columns' do |
28 | 24 | it 'expects a hash based defining columns' do |
29 | | - datatable = ComplexDatatable.new(view) |
| 25 | + datatable = ComplexDatatable.new(sample_params) |
30 | 26 | expect(datatable.view_columns).to be_a(Hash) |
31 | 27 | end |
32 | 28 | end |
33 | 29 | end |
34 | 30 |
|
35 | 31 | describe '#get_raw_records' do |
36 | 32 | it 'raises an error if not defined by the user' do |
37 | | - datatable = described_class.new(view) |
| 33 | + datatable = described_class.new(sample_params) |
38 | 34 | expect { datatable.get_raw_records }.to raise_error NotImplementedError |
39 | 35 | end |
40 | 36 | end |
41 | 37 |
|
42 | 38 | describe '#data' do |
43 | 39 | it 'raises an error if not defined by the user' do |
44 | | - datatable = described_class.new(view) |
| 40 | + datatable = described_class.new(sample_params) |
45 | 41 | expect { datatable.data }.to raise_error NotImplementedError |
46 | 42 | end |
47 | 43 |
|
48 | 44 | context 'when data is defined as a hash' do |
49 | | - let(:datatable) { ComplexDatatable.new(view) } |
| 45 | + let(:datatable) { ComplexDatatable.new(sample_params) } |
50 | 46 |
|
51 | 47 | it 'should return an array of hashes' do |
52 | 48 | create_list(:user, 5) |
|
66 | 62 | end |
67 | 63 |
|
68 | 64 | context 'when data is defined as a array' do |
69 | | - let(:datatable) { ComplexDatatableArray.new(view) } |
| 65 | + let(:datatable) { ComplexDatatableArray.new(sample_params) } |
70 | 66 |
|
71 | 67 | it 'should return an array of arrays' do |
72 | 68 | create_list(:user, 5) |
|
87 | 83 | end |
88 | 84 |
|
89 | 85 | describe '#as_json' do |
90 | | - let(:datatable) { ComplexDatatable.new(view) } |
| 86 | + let(:datatable) { ComplexDatatable.new(sample_params) } |
91 | 87 |
|
92 | 88 | it 'should return a hash' do |
93 | 89 | create_list(:user, 5) |
|
116 | 112 |
|
117 | 113 | context 'Private API' do |
118 | 114 |
|
119 | | - let(:view) { double('view', params: sample_params) } |
120 | | - let(:datatable) { ComplexDatatable.new(view) } |
| 115 | + let(:datatable) { ComplexDatatable.new(sample_params) } |
121 | 116 |
|
122 | 117 | before(:each) do |
123 | 118 | allow_any_instance_of(AjaxDatatablesRails::Configuration).to receive(:orm) { nil } |
|
150 | 145 | describe 'helper methods' do |
151 | 146 | describe '#offset' do |
152 | 147 | it 'defaults to 0' do |
153 | | - default_view = double('view', params: {}) |
154 | | - datatable = described_class.new(default_view) |
| 148 | + datatable = described_class.new({}) |
155 | 149 | expect(datatable.datatable.send(:offset)).to eq(0) |
156 | 150 | end |
157 | 151 |
|
158 | 152 | it 'matches the value on view params[:start]' do |
159 | | - paginated_view = double('view', params: { start: '11' }) |
160 | | - datatable = described_class.new(paginated_view) |
| 153 | + datatable = described_class.new({ start: '11' }) |
161 | 154 | expect(datatable.datatable.send(:offset)).to eq(11) |
162 | 155 | end |
163 | 156 | end |
164 | 157 |
|
165 | 158 | describe '#page' do |
166 | 159 | it 'calculates page number from params[:start] and #per_page' do |
167 | | - paginated_view = double('view', params: { start: '11' }) |
168 | | - datatable = described_class.new(paginated_view) |
| 160 | + datatable = described_class.new({ start: '11' }) |
169 | 161 | expect(datatable.datatable.send(:page)).to eq(2) |
170 | 162 | end |
171 | 163 | end |
172 | 164 |
|
173 | 165 | describe '#per_page' do |
174 | 166 | it 'defaults to 10' do |
175 | | - datatable = described_class.new(view) |
| 167 | + datatable = described_class.new(sample_params) |
176 | 168 | expect(datatable.datatable.send(:per_page)).to eq(10) |
177 | 169 | end |
178 | 170 |
|
179 | 171 | it 'matches the value on view params[:length]' do |
180 | | - other_view = double('view', params: { length: 20 }) |
| 172 | + other_view = { length: 20 } |
181 | 173 | datatable = described_class.new(other_view) |
182 | 174 | expect(datatable.datatable.send(:per_page)).to eq(20) |
183 | 175 | end |
|
0 commit comments