Skip to content

Commit b74173a

Browse files
committed
add rails/convert_rails_test_xhr_4_2_to_5_0 snippet
1 parent 2d68303 commit b74173a

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
Synvert::Rewriter.new 'rails', 'convert_rails_test_xhr_4_2_to_5_0' do
4+
description <<~EOS
5+
It converts xhr method in rails test from 4.2 to 5.0.
6+
7+
```ruby
8+
xhr :get, :show
9+
```
10+
11+
=>
12+
13+
```ruby
14+
get :show, xhr: true
15+
```
16+
EOS
17+
18+
if_gem 'rails', { gte: '5.0' }
19+
20+
within_files '{test,spec}/{functional,controllers}/**/*.rb' do
21+
with_node type: 'send', receiver: nil, message: 'xhr', arguments: { size: 2 } do
22+
method, action = node.arguments.map(&:to_value)
23+
replace_with("#{method} :#{action}, xhr: true")
24+
end
25+
end
26+
end

lib/rails/upgrade_4_2_to_5_0.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
add_snippet 'rails', 'convert_head_response'
2222
add_snippet 'rails', 'convert_render_text_to_render_plain'
2323
add_snippet 'rails', 'convert_rails_test_request_methods_4_2_to_5_0'
24+
add_snippet 'rails', 'convert_rails_test_xhr_4_2_to_5_0'
2425
add_snippet 'rails', 'add_application_record'
2526
add_snippet 'rails', 'add_application_job'
2627
add_snippet 'rails', 'convert_after_commit'
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'Convert Foo to Bar' do
6+
let(:rewriter_name) { 'rails/convert_rails_test_xhr_4_2_to_5_0' }
7+
let(:fake_file_path) { 'test/functional/posts_controller_test.rb' }
8+
let(:test_content) { <<~EOS }
9+
class PostsControllerTest < ActionController::TestCase
10+
context "on XHR GET to show" do
11+
setup do
12+
xhr :get, :show
13+
end
14+
15+
should respond_with :method_not_allowed
16+
end
17+
end
18+
EOS
19+
let(:test_rewritten_content) { <<~EOS }
20+
class PostsControllerTest < ActionController::TestCase
21+
context "on XHR GET to show" do
22+
setup do
23+
get :show, xhr: true
24+
end
25+
26+
should respond_with :method_not_allowed
27+
end
28+
end
29+
EOS
30+
31+
include_examples 'convertable'
32+
end

spec/rails/upgrade_4_2_to_5_0_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def test_load_error
114114
rails/convert_head_response
115115
rails/convert_render_text_to_render_plain
116116
rails/convert_rails_test_request_methods_4_2_to_5_0
117+
rails/convert_rails_test_xhr_4_2_to_5_0
117118
rails/add_application_record
118119
rails/add_application_job
119120
rails/convert_after_commit

0 commit comments

Comments
 (0)