Sha256: 92c4ff585a55a52c0b631bef2b69babb1992e53c1d63fe0c1f055bdb4123a68f

Contents?: true

Size: 1.49 KB

Versions: 4

Compression:

Stored size: 1.49 KB

Contents

require 'spec_helper'

describe GraphQL::Visitor do
  let(:document) { GraphQL.parse("
    query cheese { cheese(id: 1) { flavor, source, producers(first: 3) { name } } }
    fragment cheeseFields on Cheese { flavor }
    ")}
  let(:counts) { {fields_entered: 0, arguments_entered: 0, arguments_left: 0, argument_names: []} }

  let(:visitor) do
    v = GraphQL::Visitor.new
    v[GraphQL::Nodes::Field] << -> (node, parent) { counts[:fields_entered] += 1 }
    # two ways to set up enter hooks:
    v[GraphQL::Nodes::Argument] <<       -> (node, parent) { counts[:argument_names] << node.name }
    v[GraphQL::Nodes::Argument].enter << -> (node, parent) { counts[:arguments_entered] += 1}
    v[GraphQL::Nodes::Argument].leave << -> (node, parent) { counts[:arguments_left] += 1 }

    v[GraphQL::Nodes::Document].leave << -> (node, parent) { counts[:finished] = true }
    v
  end

  it 'calls hooks during a depth-first tree traversal' do
    assert_equal(2, visitor[GraphQL::Nodes::Argument].enter.length)
    visitor.visit(document)
    assert_equal(6, counts[:fields_entered])
    assert_equal(2, counts[:arguments_entered])
    assert_equal(2, counts[:arguments_left])
    assert_equal(["id", "first"], counts[:argument_names])
    assert(counts[:finished])
  end

  describe 'Visitor::SKIP' do
    it 'skips the rest of the node' do
      visitor[GraphQL::Nodes::Document] << -> (node, parent) { GraphQL::Visitor::SKIP }
      visitor.visit(document)
      assert_equal(0, counts[:fields_entered])
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
graphql-0.5.0 spec/graph_ql/visitor_spec.rb
graphql-0.4.0 spec/graph_ql/visitor_spec.rb
graphql-0.3.0 spec/graph_ql/parser/visitor_spec.rb
graphql-0.2.0 spec/graph_ql/parser/visitor_spec.rb