Module: RSpec::GraphQLResponse

Defined in:
lib/rspec/graphql_response.rb,
lib/rspec/graphql_response/helpers.rb,
lib/rspec/graphql_response/version.rb,
lib/rspec/graphql_response/matchers.rb,
lib/rspec/graphql_response/validators.rb,
lib/rspec/graphql_response/configuration.rb,
lib/rspec/graphql_response/dig_dug/dig_dug.rb,
lib/rspec/graphql_response/validators/validation_base.rb,
lib/rspec/graphql_response/validators/validation_result.rb,
lib/rspec/graphql_response/validators/validation_runner.rb

Defined Under Namespace

Modules: Validators Classes: Configuration, DigDug

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.add_context_helper(name, &helper) ⇒ Object



3
4
5
# File 'lib/rspec/graphql_response/helpers.rb', line 3

def self.add_context_helper(name, &helper)
  self.add_helper(name, scope: :context, &helper)
end

.add_helper(name, scope: :spec, &helper) ⇒ Object



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
# File 'lib/rspec/graphql_response/helpers.rb', line 7

def self.add_helper(name, scope: :spec, &helper)
  helper_module = Module.new do |mod|
    mod.define_method(name) do |*args, &block|
      instance_var = "@#{name}".to_sym

      if self.instance_variables.include? instance_var
        return self.instance_variable_get(instance_var)
      end

      args << block
      result = self.instance_exec(*args, &helper)
      self.instance_variable_set(instance_var, result)
    end
  end

  RSpec.configure do |config|
    config.after(:each) do
      instance_var = "@#{name}".to_sym
      helper_module.instance_variable_set(instance_var, nil)
    end

    module_method = if scope == :spec
                      :include
                    elsif scope == :context
                      :extend
                    else
                      raise ArgumentError, "A helper method's scope must be either :spec or :describe"
                    end

    config.send(module_method, helper_module, type: :graphql)
  end
end

.add_matcher(name, &matcher) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/rspec/graphql_response/matchers.rb', line 3

def self.add_matcher(name, &matcher)
  matcher_module = Module.new do |mod|
    extend RSpec::Matchers::DSL
    matcher(name, &matcher)
  end

  self.include(matcher_module)
end

.add_validator(name, &validator) ⇒ Object



7
8
9
10
11
12
# File 'lib/rspec/graphql_response/validators.rb', line 7

def self.add_validator(name, &validator)
  @validators ||= {}

  validator_class = Class.new(Validators::ValidationBase, &validator)
  @validators[name] = validator_class
end

.configurationObject



18
19
20
# File 'lib/rspec/graphql_response.rb', line 18

def self.configuration
  @configuration ||= Configuration.new
end

.configure(&block) ⇒ Object



12
13
14
15
16
# File 'lib/rspec/graphql_response.rb', line 12

def self.configure(&block)
  return if block.nil?

  block.call(configuration)
end

.remove_validator(name) ⇒ Object



14
15
16
17
# File 'lib/rspec/graphql_response/validators.rb', line 14

def self.remove_validator(name)
  @validators ||= {}
  @validators.delete(:key)
end

.reset_configurationObject



22
23
24
# File 'lib/rspec/graphql_response.rb', line 22

def self.reset_configuration
  @configuration = nil
end

.validator(name) ⇒ Object



19
20
21
# File 'lib/rspec/graphql_response/validators.rb', line 19

def self.validator(name)
  @validators[name].new
end