Skip to content

Instantly share code, notes, and snippets.

@RobertWSaunders
Last active October 5, 2018 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertWSaunders/884f3d2b05ca21f1c0cefec56b3440f2 to your computer and use it in GitHub Desktop.
Save RobertWSaunders/884f3d2b05ca21f1c0cefec56b3440f2 to your computer and use it in GitHub Desktop.
require 'pry'
module GraphQLTestHelper
def on_mutation(args: {}, &mutation_tests_block)
mutation, response = mutation_query(args: args, &mutation_tests_block)
puts mutation.selections # [{:script_tag=>[:src, :display_scope]}]
MutationEvaluator.new(self, response) do
@mutation = mutation
instance_exec(&mutation_tests_block)
end
end
private
def mutation_query(args: {}, &mutation_tests_block)
mutation = MutationGenerator.new(args, &mutation_tests_block)
response = Requestor.new(mutation).response
[mutation, response]
end
end
class ScriptTagCreateTest
include GraphQLTestHelper
def test
on_mutation args: { input: { src: "https://hello.com/bar.js" }} do
assert_no_errors
assert_no_user_errors
on_return_field :script_tag do
assert_field "https://hello.com/bar.js", :src
assert_field "ONLINE_STORE", :display_scope
end
end
end
end
class MutationGenerator
attr_accessor :selections
def initialize(args, &mutation_tests_block)
@args = args
@selections = []
instance_exec(&mutation_tests_block)
# continue to create the mutation like normal using selections and passed in args
end
def assert_field(expected, field_name)
@current_selection[@current_field_name].push(field_name.to_sym)
end
def on_return_field(field_name, &field_assertions)
@current_selection = Hash.new
@current_field_name = field_name
@current_selection[@current_field_name] = []
instance_exec(&field_assertions)
@selections.push(@current_selection)
end
def assert_no_user_errors
# no op
end
def assert_no_errors
# no op
end
end
class MutationEvaluator
def initialize(execution_context, response, &block)
@execution_context = execution_context
@response = response
#...
instance_exec(&block)
end
def assert_no_errors
#...
end
def assert_no_user_errors
#...
end
def on_return_field(field_name, &block)
#...
end
def assert_field(field_name, expected = nil, &block)
#...
end
end
class Requestor
attr_accessor :response
def initialize(mutation)
@query = mutation
@response = "my response"
#...
end
end
script_tag_test = ScriptTagCreateTest.new
script_tag_test.test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment