2020}).call(this);
2121eos
2222
23+ class NullTransformer
24+ def initialize ( options = { } ) ; end
25+ def transform ( code )
26+ "TRANSFORMED CODE!;\n "
27+ end
28+ end
29+
2330class JSXTransformTest < ActionDispatch ::IntegrationTest
31+ setup do
32+ clear_sprockets_cache
33+ end
34+
35+ teardown do
36+ clear_sprockets_cache
37+ React ::JSX . transformer_class = React ::JSX ::Transformer
38+ React ::JSX . transform_options = { }
39+ end
2440
2541 test 'asset pipeline should transform JSX' do
2642 get '/assets/example.js'
27- FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
2843 assert_response :success
2944 assert_equal EXPECTED_JS , @response . body
3045 end
@@ -40,14 +55,12 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
4055 end
4156
4257 test 'can use dropped-in version of JSX transformer' do
43- hidden_path = File . expand_path ( "../dummy/ vendor/assets/react/JSXTransformer__.js", __FILE__ )
44- replacing_path = File . expand_path ( "../dummy/ vendor/assets/react/JSXTransformer.js", __FILE__ )
58+ hidden_path = Rails . root . join ( " vendor/assets/react/JSXTransformer__.js")
59+ replacing_path = Rails . root . join ( " vendor/assets/react/JSXTransformer.js")
4560
46- FileUtils . mv hidden_path , replacing_path
61+ FileUtils . cp hidden_path , replacing_path
4762 get '/assets/example3.js'
48-
49- FileUtils . mv replacing_path , hidden_path
50- FileUtils . rm_r CACHE_PATH if CACHE_PATH . exist?
63+ FileUtils . rm replacing_path
5164
5265 assert_response :success
5366 assert_equal 'test_confirmation_token_jsx_transformed;' , @response . body
@@ -69,4 +82,27 @@ class JSXTransformTest < ActionDispatch::IntegrationTest
6982 assert_response :success
7083 assert_match ( /\( i\s *,\s *name\s *\) \s *\{ / , @response . body , "type annotations are removed" )
7184 end
85+
86+ test 'accepts asset_path: option' do
87+ hidden_path = Rails . root . join ( "vendor/assets/react/JSXTransformer__.js" )
88+ custom_path = Rails . root . join ( "vendor/assets/react/custom" )
89+ replacing_path = custom_path . join ( "CustomTransformer.js" )
90+
91+ React ::JSX . transform_options = { asset_path : "custom/CustomTransformer.js" }
92+
93+ FileUtils . mkdir_p ( custom_path )
94+ FileUtils . cp ( hidden_path , replacing_path )
95+ get '/assets/example3.js'
96+
97+ FileUtils . rm_rf custom_path
98+ assert_response :success
99+ assert_equal 'test_confirmation_token_jsx_transformed;' , @response . body
100+ end
101+
102+ test 'use a custom transformer' do
103+ React ::JSX . transformer_class = NullTransformer
104+ manually_expire_asset ( 'example2.js' )
105+ get '/assets/example2.js'
106+ assert_equal "TRANSFORMED CODE!;\n " , @response . body
107+ end
72108end
0 commit comments