Skip to content

Commit f755896

Browse files
committed
Improve function components and tests
1 parent 6f51191 commit f755896

File tree

6 files changed

+24
-20
lines changed

6 files changed

+24
-20
lines changed

lib/generators/templates/component.es6.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<%= file_header %>
2-
function <%= component_name %>(props) {
2+
const <%= component_name %> = (props) => {
33
return (
44
<React.Fragment>
55
<% attributes.each do |attribute| -%>
66
<%= attribute[:name].titleize %>: {props.<%= attribute[:name].camelize(:lower) %>}
77
<% end -%>
88
</React.Fragment>
9-
);
9+
)
1010
}
1111

1212
<% if attributes.size > 0 -%>
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
<%= file_header %>var <%= component_name %> = createReactClass({
1+
<%= file_header %>
2+
function <%= component_name %>(props) {
3+
return (
4+
<React.Fragment>
5+
<% attributes.each do |attribute| -%>
6+
<%= attribute[:name].titleize %>: {props.<%= attribute[:name].camelize(:lower) %>}
7+
<% end -%>
8+
</React.Fragment>
9+
);
10+
}
11+
212
<% if attributes.size > 0 -%>
3-
propTypes: {
13+
<%= file_name.camelize %>.propTypes = {
414
<% attributes.each_with_index do |attribute, idx| -%>
5-
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %><% if (idx < attributes.length-1) %>,<% end %>
15+
<%= attribute[:name].camelize(:lower) %>: <%= attribute[:type] %><% if (idx < attributes.length-1) %>,<% end %>
616
<% end -%>
7-
},
17+
};
818
<% end -%>
919

10-
render: function() {
11-
return (
12-
<React.Fragment>
13-
<% attributes.each do |attribute| -%>
14-
<%= attribute[:name].titleize %>: {this.props.<%= attribute[:name].camelize(:lower) %>}
15-
<% end -%>
16-
</React.Fragment>
17-
);
18-
}
19-
});
2020
<%= file_footer %>

test/generators/coffee_component_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def filename
1515
run_generator %w(GeneratedComponent name)
1616

1717
es6 = File.read(File.join(destination_root, 'app/javascript/components/GeneratedComponent.js'))
18-
assert_match(%r{extends React.Component}, es6)
18+
assert_match(/const GeneratedComponent = \(props\) => {/, es6)
1919
end
2020
else
2121
def filename

test/generators/component_generator_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,6 @@ def filename_with_subfolder
7878
run_generator %w(GeneratedComponent name:string address:shape)
7979
jsx = React::JSX.transform(File.read(File.join(destination_root, filename)))
8080

81-
assert_match(Regexp.new(expected_working_jsx), jsx)
81+
assert_match(Regexp.new(expected_working_jsx_in_function_component), jsx)
8282
end
8383
end

test/generators/es6_component_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def component_name
2424
test 'uses es6 syntax' do
2525
run_generator %w(GeneratedComponent name --es6)
2626

27-
assert_file filename, /^function\s#{component_name}\(\) {/
27+
assert_file filename, /const #{component_name} = \(props\) => {/
2828
end
2929

3030
test 'assigns defaultProps after function definintion' do
@@ -37,6 +37,6 @@ def component_name
3737
run_generator %w(GeneratedComponent name:string address:shape --es6)
3838
jsx = React::JSX.transform(File.read(File.join(destination_root, filename)))
3939

40-
assert_match(Regexp.new(expected_working_jsx), jsx)
40+
assert_match(Regexp.new(expected_working_jsx_in_function_component), jsx)
4141
end
4242
end

test/test_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def expected_working_jsx
8585
/\.createElement\(\s*\S*\.Fragment,\s*null,\s*\"Name:\s*\",\s*this\.props\.name,\s*\"Address:\s*\",\s*this\.props\.address\s*\)/x
8686
end
8787

88+
def expected_working_jsx_in_function_component
89+
/\.createElement\(\s*\S*\.Fragment,\s*null,\s*\"Name:\s*\",\s*props\.name,\s*\"Address:\s*\",\s*props\.address\s*\)/x
90+
end
91+
8892
module ParamsHelper
8993
# Normalize params for Rails 5.1+
9094
def query_params(params)

0 commit comments

Comments
 (0)