1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
# frozen_string_literal: true
require "spec_helper"
require "generators/graphql/input_generator"
class GraphQLGeneratorsInputGeneratorTest < BaseGeneratorTest
tests Graphql::Generators::InputGenerator
# rubocop:disable Style/ClassAndModuleChildren
class ::InputTestUser < ActiveRecord::Base
end
# rubocop:enable Style/ClassAndModuleChildren
test "it generates arguments with types" do
commands = [
# GraphQL-style:
["Bird", "wingspan:Int!", "foliage:[Color]"],
# Ruby-style:
["BirdType", "wingspan:!Integer", "foliage:[Types::ColorType]"],
# Mixed
["BirdType", "wingspan:!Int", "foliage:[Color]"],
]
expected_content = <<-RUBY
# frozen_string_literal: true
module Types
class BirdInputType < Types::BaseInputObject
argument :wingspan, Integer, required: false
argument :foliage, [Types::ColorType], required: false
end
end
RUBY
commands.each do |c|
prepare_destination
run_generator(c)
assert_file "app/graphql/types/bird_input_type.rb", expected_content
end
end
test "it generates classified file" do
run_generator(["page"])
assert_file "app/graphql/types/page_input_type.rb", <<-RUBY
# frozen_string_literal: true
module Types
class PageInputType < Types::BaseInputObject
end
end
RUBY
end
test "it generates namespaced classified file" do
run_generator(["page", "--namespaced-types"])
assert_file "app/graphql/types/inputs/page_input_type.rb", <<-RUBY
# frozen_string_literal: true
module Types
class Inputs::PageInputType < Types::BaseInputObject
end
end
RUBY
end
test "it generates objects based on ActiveRecord schema" do
run_generator(["InputTestUser"])
assert_file "app/graphql/types/input_test_user_input_type.rb", <<-RUBY
# frozen_string_literal: true
module Types
class InputTestUserInputType < Types::BaseInputObject
argument :id, ID, required: false
argument :created_at, GraphQL::Types::ISO8601DateTime, required: false
argument :birthday, GraphQL::Types::ISO8601Date, required: false
argument :points, Integer, required: false
argument :rating, Float, required: false
argument :friend_id, Integer, required: false
end
end
RUBY
end
test "it generates namespaced objects based on ActiveRecord schema" do
run_generator(["InputTestUser", "--namespaced-types"])
assert_file "app/graphql/types/inputs/input_test_user_input_type.rb", <<-RUBY
# frozen_string_literal: true
module Types
class Inputs::InputTestUserInputType < Types::BaseInputObject
argument :id, ID, required: false
argument :created_at, GraphQL::Types::ISO8601DateTime, required: false
argument :birthday, GraphQL::Types::ISO8601Date, required: false
argument :points, Integer, required: false
argument :rating, Float, required: false
argument :friend_id, Integer, required: false
end
end
RUBY
end
test "it generates objects based on ActiveRecord schema with additional custom arguments" do
run_generator(["InputTestUser", "name:!String", "email:!Citext", "settings:jsonb"])
assert_file "app/graphql/types/input_test_user_input_type.rb", <<-RUBY
# frozen_string_literal: true
module Types
class InputTestUserInputType < Types::BaseInputObject
argument :id, ID, required: false
argument :created_at, GraphQL::Types::ISO8601DateTime, required: false
argument :birthday, GraphQL::Types::ISO8601Date, required: false
argument :points, Integer, required: false
argument :rating, Float, required: false
argument :friend_id, Integer, required: false
argument :name, String, required: false
argument :email, String, required: false
argument :settings, GraphQL::Types::JSON, required: false
end
end
RUBY
end
end
|