Skip to content

Instantly share code, notes, and snippets.

@RobertWSaunders
Last active October 5, 2018 02:51
Show Gist options
  • Select an option

  • Save RobertWSaunders/884f3d2b05ca21f1c0cefec56b3440f2 to your computer and use it in GitHub Desktop.

Select an option

Save RobertWSaunders/884f3d2b05ca21f1c0cefec56b3440f2 to your computer and use it in GitHub Desktop.

Revisions

  1. RobertWSaunders renamed this gist Oct 5, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. RobertWSaunders revised this gist Jun 22, 2018. No changes.
  3. RobertWSaunders revised this gist Jun 22, 2018. 1 changed file with 78 additions and 78 deletions.
    156 changes: 78 additions & 78 deletions test_helper_proposal.rb
    Original file line number Diff line number Diff line change
    @@ -2,112 +2,112 @@
    require 'pry'

    module GraphQLTestHelper
    def on_mutation(args: {}, &mutation_tests_block)
    mutation, response = mutation_query(args: args, &mutation_tests_block)
    def on_mutation(args: {}, &mutation_tests_block)
    mutation, response = mutation_query(args: args, &mutation_tests_block)

    puts mutation.selections # [{:script_tag=>[:src, :display_scope]}]
    puts mutation.selections # [{:script_tag=>[:src, :display_scope]}]

    MutationEvaluator.new(self, response) do
    @mutation = mutation
    instance_exec(&mutation_tests_block)
    end
    end
    MutationEvaluator.new(self, response) do
    @mutation = mutation
    instance_exec(&mutation_tests_block)
    end
    end

    private
    private

    def mutation_query(args: {}, &mutation_tests_block)
    mutation = MutationGenerator.new(args, &mutation_tests_block)
    def mutation_query(args: {}, &mutation_tests_block)
    mutation = MutationGenerator.new(args, &mutation_tests_block)

    response = Requestor.new(mutation).response
    response = Requestor.new(mutation).response

    [mutation, response]
    end
    [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
    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
    attr_accessor :selections

    def initialize(args, &mutation_tests_block)
    @args = args
    @selections = []
    def initialize(args, &mutation_tests_block)
    @args = args
    @selections = []

    instance_exec(&mutation_tests_block)
    instance_exec(&mutation_tests_block)

    # continue to create the mutation like normal using selections and passed in args
    end
    # 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 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] = []
    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)
    instance_exec(&field_assertions)

    @selections.push(@current_selection)
    end
    @selections.push(@current_selection)
    end

    def assert_no_user_errors
    # no op
    end
    def assert_no_user_errors
    # no op
    end

    def assert_no_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
    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
    attr_accessor :response

    def initialize(mutation)
    @query = mutation
    @response = "my response"
    #...
    end
    def initialize(mutation)
    @query = mutation
    @response = "my response"
    #...
    end
    end

    script_tag_test = ScriptTagCreateTest.new
  4. RobertWSaunders revised this gist Jun 22, 2018. No changes.
  5. RobertWSaunders revised this gist Jun 22, 2018. No changes.
  6. RobertWSaunders created this gist Jun 22, 2018.
    114 changes: 114 additions & 0 deletions test_helper_proposal.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,114 @@

    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