/
datatables_spec.rb
172 lines (160 loc) · 6.05 KB
/
datatables_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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require 'spec_helper.rb'
describe Daru::View::Table, 'table using daru-data_tables' do
before { Daru::View.table_library = :datatables }
before(:each) do
@data_vec1 = Daru::Vector.new([1 ,2, 4], name: :a)
@data_vec2 = Daru::Vector.new([15 ,30, 40])
@data_df = Daru::DataFrame.new(arr1: @data_vec1, arr2: @data_vec2)
@options = {scrollX: true}
@plot = Daru::View::Table.new(@data_df, @options)
end
let(:options) {{scrollX: true}}
let(:string_array) {["daru", "view"]}
let(:data_array) {[[1, 15], [2, 30], [4, 40]]}
let(:table_string_array) { Daru::View::Table.new(string_array, options) }
let(:table_array) { Daru::View::Table.new(data_array, options) }
let(:table_dv) { Daru::View::Table.new(@data_vec1, @options) }
describe "initialization Tables" do
it "Table class must be Daru::View::DataTable" do
expect(Daru::View::Table.new.table).to be_a Daru::View::DataTable
end
it "Table class must be Daru::View::DataTable when data objects" \
" are of class Array" do
expect(table_string_array.table).to be_a Daru::View::DataTable
expect(table_string_array.data).to eq string_array
expect(table_string_array.options).to eq options
end
it "Table class must be Daru::View::DataTable when data objects" \
" are of class Array of Arrays" do
expect(table_array.table).to be_a Daru::View::DataTable
expect(table_array.data).to eq data_array
expect(table_array.options).to eq options
end
end
describe "#init_script" do
subject(:js) { table_array.init_script }
it "loads correct dependent js and css files" do
expect(js).to match(/jquery-latest.min.js/)
expect(js).to match(/jquery.dataTables.js/)
expect(js).to match(/jquery.dataTables.css/)
end
end
describe "#generate_body" do
context 'when data is set as Daru::DataFrame' do
subject(:js) { @plot.adapter.generate_body(@plot.table) }
it 'generates valid script and table' do
expect(js).to match(/DataTable/)
expect(js).to match(
/scrollX: true, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/
)
expect(js).to match(/<th><\/th>/i)
expect(js).to match(/<th>arr1<\/th>/i)
expect(js).to match(/<th>arr2<\/th>/i)
end
end
# In Daru, thead is generated only when the name of the vector is provided
context 'when data is set as Daru::Vector' do
subject(:js) { table_dv.adapter.generate_body(table_dv.table) }
it 'generates valid script and table' do
expect(js).to match(/DataTable/)
expect(js).to match(
/scrollX: true, data: \[\[0,1\],\[1,2\],\[2,4\]\]/
)
expect(js).to match(/<th> <\/th>/i)
expect(js).to match(/<th>a<\/th>/i)
end
end
context 'when data is set as Array' do
subject(:js) {
table_string_array.adapter.generate_body(table_string_array.table)
}
it 'generates valid script and table' do
expect(js).to match(/DataTable/)
expect(js).to match(
/scrollX: true, data: \[\[0,"daru"\],\[1,"view"\]\]/
)
expect(js).to match(/<th><\/th>/i)
expect(js).to match(/<th>Column: 0<\/th>/i)
end
end
context 'when data is set as Array of Arrays' do
subject(:js) {
table_string_array.adapter.generate_body(table_string_array.table)
}
it 'generates valid script and table' do
expect(js).to match(/DataTable/)
expect(js).to match(
/scrollX: true, data: \[\[0,"daru"\],\[1,"view"\]\]/
)
expect(js).to match(/<th><\/th>/i)
expect(js).to match(/<th>Column: 0<\/th>/i)
end
end
end
describe "#export_html_file" do
context 'when large dataset is used' do
before do
array_large = []
for i in 0..50000
array_large << i
end
table_array_large = Daru::View::Table.new(array_large, options)
table_array_large.export_html_file('./plot.html')
@path = File.expand_path('../../plot.html', __dir__)
end
subject(:content) { File.read(@path) }
it "writes correct dependent js and css files" do
expect(content).to match(/jquery-latest.min.js/)
expect(content).to match(/jquery.dataTables.js/)
expect(content).to match(/jquery.dataTables.css/)
end
it "writes a script" do
expect(content).to match(/html/i)
expect(content).to match(/script/i)
end
it "writes server side html code of the DataTable to the file" do
expect(content).to match(
/data_array = \[\[0, 0\], \[1, 1\], \[2, 2\]/
)
expect(content).to match(
/scrollX: true, serverSide: true, ajax:/
)
expect(content).to match(
/function \( data, callback, settings \) {/)
expect(content).to match(/out.push\( data_array\[i\] \);/i)
expect(content).to match(/callback\( \{/i)
end
it "generates a table" do
expect(content).to match(/<th><\/th>/i)
expect(content).to match(/<th>Column: 0<\/th>/i)
end
end
context 'when small dataset is used' do
before do
@plot.export_html_file('./plot.html')
@path = File.expand_path('../../plot.html', __dir__)
end
subject(:content) { File.read(@path) }
it "writes correct dependent js and css files" do
expect(content).to match(/jquery-latest.min.js/)
expect(content).to match(/jquery.dataTables.js/)
expect(content).to match(/jquery.dataTables.css/)
end
it "writes a script" do
expect(content).to match(/html/i)
expect(content).to match(/script/i)
end
it "writes client side html code of the DataTable to the file" do
expect(content).to match(/DataTable/)
expect(content).to match(
/scrollX: true, data: \[\[0,1,15\],\[1,2,30\],\[2,4,40\]\]/
)
end
it "generates a table" do
expect(content).to match(/<th><\/th>/i)
expect(content).to match(/<th>arr1<\/th>/i)
expect(content).to match(/<th>arr2<\/th>/i)
end
end
end
end