|
| 1 | +require 'test_helper' |
| 2 | +require 'generators/react/component_generator' |
| 3 | + |
| 4 | +class Es6ComponentGeneratorTest < Rails::Generators::TestCase |
| 5 | + destination File.join(Rails.root, 'tmp', 'component_generator_test_output') |
| 6 | + setup :prepare_destination |
| 7 | + tests React::Generators::ComponentGenerator |
| 8 | + |
| 9 | + def filename |
| 10 | + 'app/assets/javascripts/components/generated_component.es6.jsx' |
| 11 | + end |
| 12 | + |
| 13 | + test "creates the component file" do |
| 14 | + run_generator %w(GeneratedComponent --es6) |
| 15 | + |
| 16 | + assert_file filename |
| 17 | + end |
| 18 | + |
| 19 | + test "creates the component file with a node argument" do |
| 20 | + puts name |
| 21 | + run_generator %w(GeneratedComponent name --es6) |
| 22 | + assert_file filename, %r{name: React.PropTypes.node} |
| 23 | + end |
| 24 | + |
| 25 | + test "creates the component file with various standard proptypes" do |
| 26 | + proptypes = %w(string bool number array func number object any) |
| 27 | + run_generator %w(GeneratedComponent) + proptypes.map { |type| "my_#{type}:#{type}"} + ["--es6"] |
| 28 | + proptypes.each do |type| |
| 29 | + assert_file filename, %r(my#{type.capitalize}: React.PropTypes.#{type}) |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + test "creates a component file with an instanceOf property" do |
| 34 | + run_generator %w(GeneratedComponent favorite_food:instanceOf{food} --es6) |
| 35 | + assert_file filename, /favoriteFood: React.PropTypes.instanceOf\(Food\)/ |
| 36 | + end |
| 37 | + |
| 38 | + test "creates a component file with a oneOf property" do |
| 39 | + run_generator %w(GeneratedComponent favorite_food:oneOf{pizza,hamburgers} --es6) |
| 40 | + assert_file filename, /favoriteFood: React.PropTypes.oneOf\(\['pizza','hamburgers'\]\)/ |
| 41 | + end |
| 42 | + |
| 43 | + test "creates a component file with a oneOfType property" do |
| 44 | + run_generator %w(GeneratedComponent favorite_food:oneOfType{string,Food} --es6) |
| 45 | + expected_property = "favoriteFood: React.PropTypes.oneOfType([React.PropTypes.string,React.PropTypes.instanceOf(Food)])" |
| 46 | + |
| 47 | + assert_file filename, Regexp.new(Regexp.quote(expected_property)) |
| 48 | + end |
| 49 | + |
| 50 | + test "generates working jsx" do |
| 51 | + expected_name_div = /React\.createElement\(\s*"div",\s*null,\s*\"Name:\s*\",\s*this\.props\.name\s*\)/x |
| 52 | + expected_shape_div = /React\.createElement\(\s*"div",\s*null,\s*\"Address:\s*\",\s*this\.props\.address\s*\)/x |
| 53 | + |
| 54 | + run_generator %w(GeneratedComponent name:string address:shape --es6) |
| 55 | + jsx = React::JSX.transform(File.read(File.join(destination_root, filename))) |
| 56 | + |
| 57 | + assert_match(Regexp.new(expected_name_div), jsx) |
| 58 | + assert_match(Regexp.new(expected_shape_div), jsx) |
| 59 | + end |
| 60 | +end |
0 commit comments