Skip to content

Commit bd15add

Browse files
committed
Improve function components and tests
1 parent 57b437c commit bd15add

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ def filename
1717
run_generator %w[GeneratedComponent name]
1818

1919
es6 = File.read(File.join(destination_root, "app/javascript/components/GeneratedComponent.js"))
20-
21-
assert_match(/extends React.Component/, es6)
20+
assert_match(/const GeneratedComponent = \(props\) => {/, es6)
2221
end
2322
else
2423
def filename

test/generators/component_generator_test.rb

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

89-
assert_match(Regexp.new(expected_working_jsx), jsx)
89+
assert_match(Regexp.new(expected_working_jsx_in_function_component), jsx)
9090
end
9191
end

test/generators/es6_component_generator_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def component_name
2525
test "uses es6 syntax" do
2626
run_generator %w[GeneratedComponent name --es6]
2727

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

3131
test "assigns defaultProps after function definintion" do
@@ -38,6 +38,6 @@ def component_name
3838
run_generator %w[GeneratedComponent name:string address:shape --es6]
3939
jsx = React::JSX.transform(File.read(File.join(destination_root, filename)))
4040

41-
assert_match(Regexp.new(expected_working_jsx), jsx)
41+
assert_match(Regexp.new(expected_working_jsx_in_function_component), jsx)
4242
end
4343
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 # rubocop:disable Layout/LineLength
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)