11require 'spec_helper'
22
33describe 'import' , type : :feature do
4- let ( :options ) { { } }
5-
6- before do
7- add_author_resource ( options )
8- visit '/admin/authors/import'
9- end
104
115 def with_zipped_csv ( name , &block )
126
@@ -29,113 +23,144 @@ def upload_file!(name, ext='csv')
2923 end
3024
3125
32- it "has valid form" do
33- form = find ( '#new_active_admin_import_model' )
34- expect ( form [ 'action' ] ) . to eq ( "/admin/authors/do_import" )
35- expect ( form [ 'enctype' ] ) . to eq ( "multipart/form-data" )
36- file_input = form . find ( "input#active_admin_import_model_file" )
37- expect ( file_input [ :type ] ) . to eq ( "file" )
38- expect ( file_input . value ) . to be_blank
39- submit_input = form . find ( "#active_admin_import_model_submit_action input" )
40- expect ( submit_input [ :value ] ) . to eq ( "Import" )
41- expect ( submit_input [ :type ] ) . to eq ( "submit" )
42- end
26+ context "with valid options" do
4327
44- context "with hint defined" do
45- let ( :options ) {
46- { template_object : ActiveAdminImport ::Model . new ( hint : "hint" ) }
47- }
48- it "renders hint at upload page" do
49- expect ( page ) . to have_content options [ :template_object ] . hint
28+ let ( :options ) { { } }
29+
30+ before do
31+ add_author_resource ( options )
32+ visit '/admin/authors/import'
5033 end
5134
52- end
5335
36+ it "has valid form" do
37+ form = find ( '#new_active_admin_import_model' )
38+ expect ( form [ 'action' ] ) . to eq ( "/admin/authors/do_import" )
39+ expect ( form [ 'enctype' ] ) . to eq ( "multipart/form-data" )
40+ file_input = form . find ( "input#active_admin_import_model_file" )
41+ expect ( file_input [ :type ] ) . to eq ( "file" )
42+ expect ( file_input . value ) . to be_blank
43+ submit_input = form . find ( "#active_admin_import_model_submit_action input" )
44+ expect ( submit_input [ :value ] ) . to eq ( "Import" )
45+ expect ( submit_input [ :type ] ) . to eq ( "submit" )
46+ end
47+
48+ context "with hint defined" do
49+ let ( :options ) {
50+ { template_object : ActiveAdminImport ::Model . new ( hint : "hint" ) }
51+ }
52+ it "renders hint at upload page" do
53+ expect ( page ) . to have_content options [ :template_object ] . hint
54+ end
5455
55- context "import file" do
56+ end
5657
57- [ :empty , :only_headers ] . each do |file |
58- context "when #{ file } file" do
59- it "should render warning" do
6058
61- upload_file! ( file )
62- expect ( page ) . to have_content I18n . t ( 'active_admin_import.file_empty_error' )
63- expect ( Author . count ) . to eq ( 0 )
59+ context "when importing file" do
60+
61+ [ :empty , :only_headers ] . each do |file |
62+ context "when #{ file } file" do
63+ it "should render warning" do
64+
65+ upload_file! ( file )
66+ expect ( page ) . to have_content I18n . t ( 'active_admin_import.file_empty_error' )
67+ expect ( Author . count ) . to eq ( 0 )
68+ end
6469 end
6570 end
66- end
6771
68- context "when no file" do
69- it "should render error" do
70- find_button ( 'Import' ) . click
71- expect ( Author . count ) . to eq ( 0 )
72- expect ( page ) . to have_content I18n . t ( 'active_admin_import.no_file_error' )
72+ context "when no file" do
73+ it "should render error" do
74+ find_button ( 'Import' ) . click
75+ expect ( Author . count ) . to eq ( 0 )
76+ expect ( page ) . to have_content I18n . t ( 'active_admin_import.no_file_error' )
77+ end
7378 end
74- end
7579
76- context "with headers" do
80+ context "with headers" do
7781
78- it "should import file with many records" do
79- upload_file! ( :authors )
80- expect ( page ) . to have_content "Successfully imported 2 authors"
81- expect ( Author . count ) . to eq ( 2 )
82- end
82+ it "should import file with many records" do
83+ upload_file! ( :authors )
84+ expect ( page ) . to have_content "Successfully imported 2 authors"
85+ expect ( Author . count ) . to eq ( 2 )
86+ end
8387
84- it "should import file with 1 record" do
85- upload_file! ( :author )
86- expect ( page ) . to have_content "Successfully imported 1 author"
87- expect ( Author . count ) . to eq ( 1 )
88+ it "should import file with 1 record" do
89+ upload_file! ( :author )
90+ expect ( page ) . to have_content "Successfully imported 1 author"
91+ expect ( Author . count ) . to eq ( 1 )
92+ end
8893 end
89- end
9094
9195
92- context "without headers" do
93- context "with known csv headers" do
94- before do
95- allow_any_instance_of ( ActiveAdminImport ::Model ) . to receive ( :csv_headers ) . and_return ( [ 'Name' , 'Last name' , 'Birthday' ] )
96+ context "without headers" do
97+ context "with known csv headers" do
98+ before do
99+ allow_any_instance_of ( ActiveAdminImport ::Model ) . to receive ( :csv_headers ) . and_return ( [ 'Name' , 'Last name' , 'Birthday' ] )
100+ end
101+
102+ it "should import file" do
103+ upload_file! ( :authors_no_headers )
104+ expect ( page ) . to have_content "Successfully imported 2 authors"
105+ expect ( Author . count ) . to eq ( 2 )
106+ end
96107 end
97108
98- it "should import file" do
99- upload_file! ( :authors_no_headers )
100- expect ( page ) . to have_content "Successfully imported 2 authors"
101- expect ( Author . count ) . to eq ( 2 )
109+ context "with unknown csv headers" do
110+ it "should render error" do
111+ upload_file! ( :authors_no_headers )
112+ expect ( page ) . to have_content "Error:"
113+ expect ( Author . count ) . to eq ( 0 )
114+ end
102115 end
116+
103117 end
104118
105- context "with unknown csv headers" do
106- it "should render error" do
107- upload_file! ( :authors_no_headers )
108- expect ( page ) . to have_content "Error:"
109- expect ( Author . count ) . to eq ( 0 )
119+
120+ context "when zipped" do
121+ context "when allowed" do
122+
123+ it "should import file" do
124+ with_zipped_csv ( :authors ) do
125+ upload_file! ( :authors , :zip )
126+ expect ( page ) . to have_content "Successfully imported 2 authors"
127+ expect ( Author . count ) . to eq ( 2 )
128+ end
129+ end
130+
110131 end
111- end
112132
113- end
133+ context "when not allowed" do
134+ let ( :options ) { {
135+ template_object : ActiveAdminImport ::Model . new ( allow_archive : false )
136+ } }
137+
138+ it "should render error" do
139+ with_zipped_csv ( :authors ) do
140+ upload_file! ( :authors , :zip )
141+ expect ( page ) . to have_content I18n . t ( 'active_admin_import.file_format_error' )
142+ expect ( Author . count ) . to eq ( 0 )
143+ end
144+ end
145+ end
146+ end
114147
115148
116- context "when zipped" do
117- it "should import file" do
118- with_zipped_csv ( :authors ) do
119- upload_file! ( :authors , :zip )
149+ context "with semicolons separator" do
150+ let ( :options ) {
151+ { template_object : ActiveAdminImport ::Model . new ( csv_options : { col_sep : ";" } ) }
152+ }
153+ it "should import file" do
154+ upload_file! ( :authors_with_semicolons )
120155 expect ( page ) . to have_content "Successfully imported 2 authors"
121156 expect ( Author . count ) . to eq ( 2 )
122157 end
123158 end
124- end
125159
126-
127- context "with semicolons separator" do
128- let ( :options ) {
129- { template_object : ActiveAdminImport ::Model . new ( csv_options : { col_sep : ";" } ) }
130- }
131- it "should import file" do
132- upload_file! ( :authors_with_semicolons )
133- expect ( page ) . to have_content "Successfully imported 2 authors"
134- expect ( Author . count ) . to eq ( 2 )
135- end
136160 end
137161
138162 end
139163
140164
165+
141166end
0 commit comments