secret
Created

Testing a DataTable implementation based on Ryan Bates RailsCast

  • Download Gist
datatable_spec.rb
Ruby
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
require 'spec_helper'
 
describe OrderDatatable do
describe '#initialize' do
params = {
:sSortDir_0 => 'asc', :iSortCol_0 => "1", :iDisplayStart => "26",
:iDisplayLength => "50", :sSearch => "", :sEcho => "1"
}
let(:view) { double("view", :params => params) }
let(:datatable) { OrderDatatable.new(view) }
 
it 'receives a view as context' do
datatable.view.should eql view
end
end
 
describe 'delegations' do
params = {
:sSortDir_0 => 'asc', :iSortCol_0 => "1", :iDisplayStart => "26",
:iDisplayLength => "50", :sSearch => "", :sEcho => "1"
}
let(:view) { double("view", :params => params) }
let(:datatable) { OrderDatatable.new(view) }
 
it 'delegates params call to view' do
view.expects(:params).returns({:sEcho => 4})
datatable.params
end
 
it 'delegates h call to view' do
view.expects(:h).returns('foo')
datatable.h
end
 
it 'delegates link_to to view' do
view.expects(:link_to).returns('foo')
datatable.link_to
end
 
it 'delegates number_to_currency to view' do
view.expects(:number_to_currency).returns('foo')
datatable.number_to_currency
end
end
 
describe '#as_json' do
params = {
:sSortDir_0 => 'asc', :iSortCol_0 => "1", :iDisplayStart => "26",
:iDisplayLength => "50", :sSearch => "", :sEcho => "1"
}
let(:view) { double("view", :params => params) }
let(:datatable) { OrderDatatable.new(view) }
 
it 'returns a json hash' do
datatable.as_json.should be_a Hash
end
 
it 'has jquery.dataTables required keys' do
datatable.as_json.keys.should include(:sEcho, :iTotalRecords, :iTotalDisplayRecords, :aaData)
end
end
 
context 'helper methods' do
params = {
:sSortDir_0 => 'asc', :iSortCol_0 => "1", :iDisplayStart => "26",
:iDisplayLength => "50", :sSearch => "", :sEcho => "1"
}
let(:view) { double("view", :params => params) }
let(:datatable) { OrderDatatable.new(view) }
 
describe '#sort_direction' do
it 'returns the value of params[:sSortDir_0]' do
datatable.send(:sort_direction).should eql view.params[:sSortDir_0]
datatable.send(:sort_direction).should be_a String
end
end
 
describe '#sort_column' do
it 'returns value of params[:iSortCol_0]' do
datatable.send(:sort_column).should be_a String
end
end
 
describe '#page' do
it 'returns an integer that represents a page number from pagination' do
datatable.send(:page).should be_a Integer
end
end
 
describe '#per_page' do
it 'returns the value of params[:iDisplayLength] converted to Integer' do
datatable.send(:per_page).should eql 50
end
 
it 'defaults to 25 if value is not provided or equal to 0' do
view.stubs(:params).returns({ :iDisplayLength => "0" })
datatable.send(:per_page).should eql 25
 
view.stubs(:params).returns({ :iDisplayLength => "" })
datatable.send(:per_page).should eql 25
end
end
 
describe '#fetch_orders' do
before :each do
Order.should_receive(:where).with(any_args()).at_least(:once)
end
 
it 'returns an ActiveRecord::Relation of Order objects' do
datatable.send(:fetch_orders).should be_a ActiveRecord::Relation
end
 
it 'calls private methods :sort_column and :sort_direction' do
datatable.should_receive(:sort_column)
datatable.should_receive(:sort_direction)
datatable.send(:fetch_orders)
end
 
it 'calls private methods :page and :per_page' do
datatable.should_receive(:per_page)
datatable.should_receive(:page)
datatable.send(:fetch_orders)
end
 
it 'performs search on :id, :purchased_at, :account, :transaction_id, :total columns' do
view.stubs(:params).returns({ :sSearch => "1234" })
datatable.send(:fetch_orders)
end
end
 
describe '#orders' do
it 'calls :fetch_orders the first time' do
datatable.should_receive(:fetch_orders)
datatable.send(:orders)
end
 
it 'caches the value and does not call :fetch_orders if nothing changed' do
datatable.send(:orders)
datatable.should_not_receive(:fetch_orders)
datatable.send(:orders)
end
end
 
describe '#data' do
it 'returns an Array' do
datatable.send(:data).should be_a Array
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.