|
6 | 6 | let(:view) { double('view', params: sample_params) }
|
7 | 7 |
|
8 | 8 | it 'requires a view_context' do
|
9 |
| - expect { AjaxDatatablesRails::Base.new }.to raise_error ArgumentError |
| 9 | + expect { described_class.new }.to raise_error ArgumentError |
10 | 10 | end
|
11 | 11 |
|
12 | 12 | it 'accepts an options hash' do
|
13 |
| - datatable = AjaxDatatablesRails::Base.new(view, foo: 'bar') |
| 13 | + datatable = described_class.new(view, foo: 'bar') |
14 | 14 | expect(datatable.options).to eq(foo: 'bar')
|
15 | 15 | end
|
16 | 16 | end
|
17 | 17 |
|
18 | 18 | context 'Public API' do
|
19 | 19 | let(:view) { double('view', params: sample_params) }
|
20 |
| - let(:datatable) { AjaxDatatablesRails::Base.new(view) } |
21 | 20 |
|
22 | 21 | describe '#view_columns' do
|
23 | 22 | it 'raises an error if not defined by the user' do
|
| 23 | + datatable = described_class.new(view) |
24 | 24 | expect { datatable.view_columns }.to raise_error AjaxDatatablesRails::NotImplemented
|
25 | 25 | end
|
26 | 26 |
|
|
39 | 39 |
|
40 | 40 | describe '#get_raw_records' do
|
41 | 41 | it 'raises an error if not defined by the user' do
|
| 42 | + datatable = described_class.new(view) |
42 | 43 | expect { datatable.get_raw_records }.to raise_error AjaxDatatablesRails::NotImplemented
|
43 | 44 | end
|
44 | 45 | end
|
45 | 46 |
|
46 | 47 | describe '#data' do
|
47 | 48 | it 'raises an error if not defined by the user' do
|
| 49 | + datatable = described_class.new(view) |
48 | 50 | expect { datatable.data }.to raise_error AjaxDatatablesRails::NotImplemented
|
49 | 51 | end
|
50 | 52 |
|
51 | 53 | context 'when data is defined as a hash' do
|
| 54 | + let(:datatable) { ComplexDatatableHash.new(view) } |
| 55 | + |
52 | 56 | it 'should return an array of hashes' do
|
53 |
| - datatable = ComplexDatatableHash.new(view) |
54 | 57 | create_list(:user, 5)
|
55 | 58 | expect(datatable.data).to be_a(Array)
|
56 | 59 | expect(datatable.data.size).to eq 5
|
|
59 | 62 | end
|
60 | 63 |
|
61 | 64 | it 'should html escape data' do
|
62 |
| - datatable = ComplexDatatableHash.new(view) |
63 | 65 | create(:user, first_name: 'Name "><img src=x onerror=alert("first_name")>', last_name: 'Name "><img src=x onerror=alert("last_name")>')
|
64 | 66 | data = datatable.send(:sanitize, datatable.data)
|
65 | 67 | item = data.first
|
|
69 | 71 | end
|
70 | 72 |
|
71 | 73 | context 'when data is defined as a array' do
|
| 74 | + let(:datatable) { ComplexDatatableArray.new(view) } |
| 75 | + |
72 | 76 | it 'should return an array of arrays' do
|
73 |
| - datatable = ComplexDatatableArray.new(view) |
74 | 77 | create_list(:user, 5)
|
75 | 78 | expect(datatable.data).to be_a(Array)
|
76 | 79 | expect(datatable.data.size).to eq 5
|
|
79 | 82 | end
|
80 | 83 |
|
81 | 84 | it 'should html escape data' do
|
82 |
| - datatable = ComplexDatatableArray.new(view) |
83 | 85 | create(:user, first_name: 'Name "><img src=x onerror=alert("first_name")>', last_name: 'Name "><img src=x onerror=alert("last_name")>')
|
84 | 86 | data = datatable.send(:sanitize, datatable.data)
|
85 | 87 | item = data.first
|
|
90 | 92 | end
|
91 | 93 |
|
92 | 94 | describe '#as_json' do
|
| 95 | + let(:datatable) { ComplexDatatableHash.new(view) } |
| 96 | + |
93 | 97 | it 'should return a hash' do
|
94 |
| - datatable = ComplexDatatableHash.new(view) |
95 | 98 | create_list(:user, 5)
|
96 | 99 | data = datatable.as_json
|
97 | 100 | expect(data[:recordsTotal]).to eq 5
|
|
102 | 105 |
|
103 | 106 | context 'with additional_datas' do
|
104 | 107 | it 'should return a hash' do
|
105 |
| - datatable = ComplexDatatableHash.new(view) |
106 | 108 | create_list(:user, 5)
|
107 | 109 | expect(datatable).to receive(:additional_datas){ { foo: 'bar' } }
|
108 | 110 | data = datatable.as_json
|
|
154 | 156 | describe '#offset' do
|
155 | 157 | it 'defaults to 0' do
|
156 | 158 | default_view = double('view', params: {})
|
157 |
| - datatable = AjaxDatatablesRails::Base.new(default_view) |
| 159 | + datatable = described_class.new(default_view) |
158 | 160 | expect(datatable.datatable.send(:offset)).to eq(0)
|
159 | 161 | end
|
160 | 162 |
|
161 | 163 | it 'matches the value on view params[:start] minus 1' do
|
162 | 164 | paginated_view = double('view', params: { start: '11' })
|
163 |
| - datatable = AjaxDatatablesRails::Base.new(paginated_view) |
| 165 | + datatable = described_class.new(paginated_view) |
164 | 166 | expect(datatable.datatable.send(:offset)).to eq(10)
|
165 | 167 | end
|
166 | 168 | end
|
167 | 169 |
|
168 | 170 | describe '#page' do
|
169 | 171 | it 'calculates page number from params[:start] and #per_page' do
|
170 | 172 | paginated_view = double('view', params: { start: '11' })
|
171 |
| - datatable = AjaxDatatablesRails::Base.new(paginated_view) |
| 173 | + datatable = described_class.new(paginated_view) |
172 | 174 | expect(datatable.datatable.send(:page)).to eq(2)
|
173 | 175 | end
|
174 | 176 | end
|
175 | 177 |
|
176 | 178 | describe '#per_page' do
|
177 | 179 | it 'defaults to 10' do
|
178 |
| - datatable = AjaxDatatablesRails::Base.new(view) |
| 180 | + datatable = described_class.new(view) |
179 | 181 | expect(datatable.datatable.send(:per_page)).to eq(10)
|
180 | 182 | end
|
181 | 183 |
|
182 | 184 | it 'matches the value on view params[:length]' do
|
183 | 185 | other_view = double('view', params: { length: 20 })
|
184 |
| - datatable = AjaxDatatablesRails::Base.new(other_view) |
| 186 | + datatable = described_class.new(other_view) |
185 | 187 | expect(datatable.datatable.send(:per_page)).to eq(20)
|
186 | 188 | end
|
187 | 189 | end
|
|
0 commit comments