Skip to content

Commit 7cae91a

Browse files
committed
1 parent 1b8f4c1 commit 7cae91a

1 file changed

Lines changed: 86 additions & 77 deletions

File tree

spec/integration/actions/export_spec.rb

Lines changed: 86 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,80 +6,26 @@
66
RSpec.describe 'Export action', type: :request do
77
subject { page }
88

9-
before do
10-
Comment.all.collect(&:destroy) # rspec bug => doesn't get destroyed with transaction
11-
12-
@players = FactoryBot.create_list(:player, 4)
13-
@player = @players.first
14-
@player.team = FactoryBot.create :team
15-
@player.draft = FactoryBot.create :draft
16-
@player.comments = (@comments = Array.new(2) { FactoryBot.create(:comment) })
17-
@player.save
18-
19-
@abstract_model = RailsAdmin::AbstractModel.new(Player)
20-
21-
# removed schema=>only=>created_at
22-
@non_default_schema = {
23-
'only' => [PK_COLUMN.to_s, 'updated_at', 'deleted_at', 'name', 'position', 'number', 'retired', 'injured', 'born_on', 'notes', 'suspended'],
24-
'include' => {
25-
'team' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'name', 'logo_url', 'manager', 'ballpark', 'mascot', 'founded', 'wins', 'losses', 'win_percentage', 'revenue', 'color']},
26-
'draft' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'date', 'round', 'pick', 'overall', 'college', 'notes']},
27-
'comments' => {'only' => [PK_COLUMN.to_s, 'content', 'created_at', 'updated_at']},
28-
},
29-
}
30-
end
31-
32-
it 'allows to export to CSV with associations and default schema, containing properly translated header and follow configuration' do
33-
RailsAdmin.config do |c|
34-
c.model Player do
35-
include_all_fields
36-
field :name do
37-
export_value do
38-
"#{value} exported"
39-
end
40-
end
41-
42-
field :json_field, :json do
43-
formatted_value do
44-
'{}'
45-
end
46-
end
47-
end
48-
end
9+
let!(:player) { FactoryBot.create(:player) }
4910

11+
it 'exports to CSV' do
5012
visit export_path(model_name: 'player')
51-
is_expected.to have_content 'Select fields to export'
52-
select "<comma> ','", from: 'csv_options_generator_col_sep'
53-
click_button 'Export to csv'
54-
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
55-
expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position',
56-
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]',
57-
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
58-
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
59-
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',
60-
'Updated at [Draft]', 'Date [Draft]', 'Round [Draft]', 'Pick [Draft]', 'Overall [Draft]',
61-
'College [Draft]', 'Notes [Draft]', 'Id [Comments]', 'Content [Comments]', 'Created at [Comments]',
62-
'Updated at [Comments]']
63-
expect(csv.flatten).to include("#{@player.name} exported")
64-
expect(csv.flatten).to include(@player.team.name)
65-
expect(csv.flatten).to include(@player.draft.college)
66-
67-
expect(csv.flatten.join(' ')).to include(@player.comments.first.content.split("\n").first.strip)
68-
expect(csv.flatten.join(' ')).to include(@player.comments.second.content.split("\n").first.strip)
13+
click_button 'Export to json'
14+
is_expected.to have_content player.name
6915
end
7016

71-
it 'allows to export to JSON' do
17+
it 'exports to JSON' do
7218
visit export_path(model_name: 'player')
7319
click_button 'Export to json'
74-
is_expected.to have_content @player.team.name
20+
is_expected.to have_content player.name
7521
end
7622

77-
it 'allows to export to XML' do
23+
it 'exports to XML' do
7824
pending "Mongoid does not support to_xml's :include option" if CI_ORM == :mongoid
7925
visit export_path(model_name: 'player')
8026
click_button 'Export to xml'
8127

82-
is_expected.to have_content @player.team.name
28+
is_expected.to have_content player.name
8329
end
8430

8531
it 'works with Turbo Drive enabled', js: true do
@@ -88,30 +34,93 @@
8834
expect { find_button('Export to csv').trigger('click') }.not_to raise_error
8935
end
9036

91-
it 'exports polymorphic fields the easy way for now' do
92-
visit export_path(model_name: 'comment')
93-
select "<comma> ','", from: 'csv_options_generator_col_sep'
94-
click_button 'Export to csv'
95-
csv = CSV.parse page.driver.response.body
96-
expect(csv[0]).to match_array ['Id', 'Commentable', 'Commentable type', 'Content', 'Created at', 'Updated at']
97-
csv[1..].each do |line|
98-
expect(line[csv[0].index('Commentable')]).to eq(@player.id.to_s)
99-
expect(line[csv[0].index('Commentable type')]).to eq(@player.class.to_s)
100-
end
101-
end
102-
10337
it 'does not break when nothing is checked' do
10438
visit export_path(model_name: 'comment')
10539
all('input[type="checkbox"]').each(&:uncheck)
10640
expect { click_button 'Export to csv' }.not_to raise_error
10741
end
10842

109-
context 'with csv format' do
110-
it 'exports with modified schema' do
111-
page.driver.post(export_path(model_name: 'player', schema: @non_default_schema, csv: true, all: true, csv_options: {generator: {col_sep: ','}}))
43+
describe 'with associations' do
44+
let!(:players) { FactoryBot.create_list(:player, 3) }
45+
let(:team) { FactoryBot.create :team }
46+
let(:draft) { FactoryBot.create :draft }
47+
let(:comments) { FactoryBot.create_list(:comment, 2) }
48+
49+
before do
50+
player.team = team
51+
player.draft = draft
52+
player.comments = comments
53+
player.save
54+
end
55+
56+
it 'exports to CSV with default schema, containing properly translated header and follow configuration' do
57+
RailsAdmin.config do |c|
58+
c.model Player do
59+
include_all_fields
60+
field :name do
61+
export_value do
62+
"#{value} exported"
63+
end
64+
end
65+
66+
field :json_field, :json do
67+
formatted_value do
68+
'{}'
69+
end
70+
end
71+
end
72+
end
73+
74+
visit export_path(model_name: 'player')
75+
is_expected.to have_content 'Select fields to export'
76+
select "<comma> ','", from: 'csv_options_generator_col_sep'
77+
click_button 'Export to csv'
78+
csv = CSV.parse page.driver.response.body.force_encoding('utf-8') # comes through as us-ascii on some platforms
79+
expect(csv[0]).to match_array ['Id', 'Created at', 'Updated at', 'Deleted at', 'Name', 'Position',
80+
'Number', 'Retired', 'Injured', 'Born on', 'Notes', 'Suspended', 'Formation', 'Json field', 'Id [Team]', 'Created at [Team]',
81+
'Updated at [Team]', 'Name [Team]', 'Logo url [Team]', 'Team Manager [Team]', 'Ballpark [Team]',
82+
'Mascot [Team]', 'Founded [Team]', 'Wins [Team]', 'Losses [Team]', 'Win percentage [Team]',
83+
'Revenue [Team]', 'Color [Team]', 'Custom field [Team]', 'Main Sponsor [Team]', 'Id [Draft]', 'Created at [Draft]',
84+
'Updated at [Draft]', 'Date [Draft]', 'Round [Draft]', 'Pick [Draft]', 'Overall [Draft]',
85+
'College [Draft]', 'Notes [Draft]', 'Id [Comments]', 'Content [Comments]', 'Created at [Comments]',
86+
'Updated at [Comments]']
87+
expect(csv.flatten).to include("#{player.name} exported")
88+
expect(csv.flatten).to include(player.team.name)
89+
expect(csv.flatten).to include(player.draft.college)
90+
91+
expect(csv.flatten.join(' ')).to include(player.comments.first.content.split("\n").first.strip)
92+
expect(csv.flatten.join(' ')).to include(player.comments.second.content.split("\n").first.strip)
93+
end
94+
95+
let(:custom_schema) do
96+
# removed schema=>only=>created_at
97+
{
98+
'only' => [PK_COLUMN.to_s, 'updated_at', 'deleted_at', 'name', 'position', 'number', 'retired', 'injured', 'born_on', 'notes', 'suspended'],
99+
'include' => {
100+
'team' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'name', 'logo_url', 'manager', 'ballpark', 'mascot', 'founded', 'wins', 'losses', 'win_percentage', 'revenue', 'color']},
101+
'draft' => {'only' => [PK_COLUMN.to_s, 'created_at', 'updated_at', 'date', 'round', 'pick', 'overall', 'college', 'notes']},
102+
'comments' => {'only' => [PK_COLUMN.to_s, 'content', 'created_at', 'updated_at']},
103+
},
104+
}
105+
end
106+
107+
it 'exports to CSV with custom schema' do
108+
page.driver.post(export_path(model_name: 'player', schema: custom_schema, csv: true, all: true, csv_options: {generator: {col_sep: ','}}))
112109
csv = CSV.parse page.driver.response.body
113110
expect(csv[0]).not_to include('Created at')
114111
end
112+
113+
it 'exports polymorphic fields the easy way for now' do
114+
visit export_path(model_name: 'comment')
115+
select "<comma> ','", from: 'csv_options_generator_col_sep'
116+
click_button 'Export to csv'
117+
csv = CSV.parse page.driver.response.body
118+
expect(csv[0]).to match_array ['Id', 'Commentable', 'Commentable type', 'Content', 'Created at', 'Updated at']
119+
csv[1..].each do |line|
120+
expect(line[csv[0].index('Commentable')]).to eq(player.id.to_s)
121+
expect(line[csv[0].index('Commentable type')]).to eq(player.class.to_s)
122+
end
123+
end
115124
end
116125

117126
context 'on cancel' do

0 commit comments

Comments
 (0)