Skip to content

Commit 97eef5a

Browse files
committed
Add test
1 parent 4932d2a commit 97eef5a

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
require 'test_helper'
3+
require 'generators/react/component_generator'
4+
5+
class FunctionComponentGeneratorTest < Rails::Generators::TestCase
6+
destination File.join(Rails.root, 'tmp', 'component_generator_test_output')
7+
setup :prepare_destination
8+
tests React::Generators::ComponentGenerator
9+
10+
if WebpackerHelpers.available?
11+
def filename
12+
'app/javascript/components/GeneratedComponent.js'
13+
end
14+
else
15+
def filename
16+
'app/assets/javascripts/components/generated_component.func.jsx'
17+
end
18+
end
19+
20+
def component_name
21+
'GeneratedComponent'
22+
end
23+
24+
test 'uses function syntax' do
25+
run_generator %w(GeneratedComponent name --function)
26+
27+
assert_file filename, /^function\s#{component_name}\(\) {/
28+
end
29+
30+
test 'accepts `--func`` as an alias to `--function` option' do
31+
run_generator %w(GeneratedComponent name --func)
32+
33+
assert_file filename, /^function\s#{component_name}\(\) {/
34+
end
35+
36+
test 'assigns defaultProps after class definintion' do
37+
run_generator %w(GeneratedComponent name --function)
38+
39+
assert_file filename, /\s^#{component_name}\.propTypes/
40+
end
41+
42+
test 'generates working function' do
43+
run_generator %w(GeneratedComponent name:string address:shape --function)
44+
jsx = React::JSX.transform(File.read(File.join(destination_root, filename)))
45+
46+
assert_match(Regexp.new(expected_working_jsx), jsx)
47+
end
48+
end

0 commit comments

Comments
 (0)