|
11 | 11 | end |
12 | 12 |
|
13 | 13 | describe "value methods" do |
14 | | - it "defines default methods to fetch graphql names" do |
15 | | - assert_equal enum.string, "STRING" |
16 | | - assert_equal enum.woodwind, "WOODWIND" |
17 | | - assert_equal enum.brass, "BRASS" |
18 | | - assert_equal enum.didgeridoo, "DIDGERIDOO" |
19 | | - assert_equal enum.keys, "KEYS" |
| 14 | + class EnumWithValueMethods < GraphQL::Schema::Enum |
| 15 | + value_methods(true) |
| 16 | + value :SOMETHING |
| 17 | + value :SOMETHING_ELSE, value_method: false |
| 18 | + value :SOMETHING_CUSTOM, value_method: :custom |
| 19 | + end |
| 20 | + it "defines methods to fetch graphql names when configured" do |
| 21 | + assert_equal "SOMETHING", EnumWithValueMethods.something |
| 22 | + assert_equal "SOMETHING", EnumWithValueMethods.something |
| 23 | + end |
| 24 | + |
| 25 | + it "inherits a value_methods config" do |
| 26 | + new_enum = Class.new(EnumWithValueMethods) |
| 27 | + new_enum.value(:NEW_VALUE) |
| 28 | + assert_equal "NEW_VALUE", new_enum.new_value |
20 | 29 | end |
21 | 30 |
|
22 | 31 | describe "when value_method is configured" do |
|
27 | 36 | end |
28 | 37 |
|
29 | 38 | describe "when value_method conflicts with existing method" do |
| 39 | + class ConflictEnum < GraphQL::Schema::Enum |
| 40 | + value_methods(true) |
| 41 | + end |
30 | 42 | it "does not define method and emits warning" do |
31 | 43 | expected_message = "Failed to define value method for :value, because ConflictEnum already responds to that method. Use `value_method:` to override the method name or `value_method: false` to disable Enum value method generation.\n" |
32 | 44 | assert_warns(expected_message) do |
33 | | - conflict_enum = Class.new(GraphQL::Schema::Enum) |
34 | | - Object.const_set("ConflictEnum", conflict_enum) |
35 | | - already_defined_method = conflict_enum.method(:value) |
36 | | - conflict_enum.value "VALUE", "Makes conflict" |
37 | | - assert_equal conflict_enum.method(:value), already_defined_method |
| 45 | + already_defined_method = ConflictEnum.method(:value) |
| 46 | + ConflictEnum.value "VALUE", "Makes conflict" |
| 47 | + assert_equal ConflictEnum.method(:value), already_defined_method |
38 | 48 | end |
39 | 49 | end |
40 | 50 | end |
41 | 51 |
|
42 | 52 | describe "when value_method = false" do |
43 | 53 | it "does not define method" do |
44 | | - assert_equal enum.respond_to?(:silence), false |
| 54 | + assert_equal EnumWithValueMethods.respond_to?(:something_else), false |
45 | 55 | end |
46 | 56 | end |
| 57 | + it "doesn't define value methods by default" do |
| 58 | + enum = Class.new(GraphQL::Schema::Enum) { graphql_name("SomeEnum") } |
| 59 | + enum.value("SOME_VALUE") |
| 60 | + refute enum.respond_to?(:some_value) |
| 61 | + end |
47 | 62 | end |
48 | 63 |
|
49 | 64 | describe "type info" do |
@@ -145,7 +160,7 @@ def value |
145 | 160 | class MultipleNameTestEnum < GraphQL::Schema::Enum |
146 | 161 | value "A" |
147 | 162 | value "B", value: :a |
148 | | - value "B", value: :b, value_method: false |
| 163 | + value "B", value: :b |
149 | 164 | end |
150 | 165 |
|
151 | 166 | it "doesn't allow it from enum_values" do |
|
0 commit comments