Skip to content

Commit 12a55fc

Browse files
committed
Fixing install generator to have inserted content properly on the new line.
- Added new line matcher regex (\s+\n) in inject_react for inserting after turbolinks. - Modified tests to check new lines in the inserted content from install_generator.
1 parent 7c7e009 commit 12a55fc

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

lib/generators/react/install_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def inject_react
2222
if manifest.exist?
2323
manifest_contents = File.read(manifest)
2424

25-
if match = manifest_contents.match(/\/\/=\s+require\s+turbolinks/)
25+
if match = manifest_contents.match(/\/\/=\s+require\s+turbolinks\s+\n/)
2626
inject_into_file manifest, require_react, { after: match[0] }
2727
elsif match = manifest_contents.match(/\/\/=\s+require_tree[^\n]*/)
2828
inject_into_file manifest, require_react, { before: match[0] }

test/generators/install_generator_test.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ def copy_directory(dir)
3030
File.delete destination_root + '/app/assets/javascripts/application.js'
3131

3232
run_generator
33-
34-
assert_application_file_modified
33+
assert_application_file_created
3534
end
3635

3736
test "modifies `application.js` if it's empty" do
3837
init_application_js ''
3938

4039
run_generator
41-
assert_application_file_modified
40+
assert_application_file_created
4241
end
4342

4443
test "updates `application.js` if require_tree is commented" do
@@ -68,9 +67,14 @@ def init_application_js(content)
6867
File.write destination_root + '/app/assets/javascripts/application.js', content
6968
end
7069

70+
def assert_application_file_created
71+
assert_file 'app/assets/javascripts/application.js',
72+
%r{//= require react\n//= require react_ujs\n//= require components\n}
73+
end
74+
7175
def assert_application_file_modified
72-
assert_file 'app/assets/javascripts/application.js', %r{//= require react}
73-
assert_file 'app/assets/javascripts/application.js', %r{//= require react_ujs}
74-
assert_file 'app/assets/javascripts/application.js', %r{//= require components}
76+
assert_file 'app/assets/javascripts/application.js', %r{\n//= require react\n}
77+
assert_file 'app/assets/javascripts/application.js', %r{\n//= require react_ujs\n}
78+
assert_file 'app/assets/javascripts/application.js', %r{\n//= require components\n}
7579
end
7680
end

0 commit comments

Comments
 (0)