Sha256: 3a10fefbd1605ac5869cd5c95f9b492a86f7dcaa2203e4213a5e45347a4c3aff

Contents?: true

Size: 1.55 KB

Versions: 35

Compression:

Stored size: 1.55 KB

Contents

require "spec_helper"

describe GraphQL::ScalarType do
  let(:custom_scalar) {
    GraphQL::ScalarType.define do
      name "BigInt"
      coerce_input ->(value) { value =~ /\d+/ ? Integer(value) : nil }
      coerce_result ->(value) { value.to_s }
    end
  }
  let(:bignum) { 2 ** 128 }

  it "coerces nil into nil" do
    assert_equal(nil, custom_scalar.coerce_input(nil))
  end

  it "coerces input into objects" do
    assert_equal(bignum, custom_scalar.coerce_input(bignum.to_s))
  end

  it "coerces result value for serialization" do
    assert_equal(bignum.to_s, custom_scalar.coerce_result(bignum))
  end

  describe "custom scalar errors" do
    let(:result) { custom_scalar.validate_input("xyz") }

    it "returns an invalid result" do
      assert !result.valid?
      assert_equal 'Could not coerce value "xyz" to BigInt', result.problems[0]["explanation"]
    end
  end

  describe "validate_input with good input" do
    let(:result) { GraphQL::INT_TYPE.validate_input(150) }

    it "returns a valid result" do
      assert(result.valid?)
    end
  end

  describe "validate_input with bad input" do
    let(:result) { GraphQL::INT_TYPE.validate_input("bad num") }

    it "returns an invalid result for bad input" do
      assert(!result.valid?)
    end

    it "has one problem" do
      assert_equal(result.problems.length, 1)
    end

    it "has the correct explanation" do
      assert(result.problems[0]["explanation"].include?("Could not coerce value"))
    end

    it "has an empty path" do
      assert(result.problems[0]["path"].empty?)
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
graphql-1.0.0 spec/graphql/scalar_type_spec.rb
graphql-0.19.4 spec/graphql/scalar_type_spec.rb
graphql-0.19.3 spec/graphql/scalar_type_spec.rb
graphql-0.19.2 spec/graphql/scalar_type_spec.rb
graphql-0.19.1 spec/graphql/scalar_type_spec.rb
graphql-0.19.0 spec/graphql/scalar_type_spec.rb
graphql-0.18.15 spec/graphql/scalar_type_spec.rb
graphql-0.18.14 spec/graphql/scalar_type_spec.rb
graphql-0.18.13 spec/graphql/scalar_type_spec.rb
graphql-0.18.12 spec/graphql/scalar_type_spec.rb
graphql-0.18.11 spec/graphql/scalar_type_spec.rb
graphql-0.18.10 spec/graphql/scalar_type_spec.rb
graphql-0.18.9 spec/graphql/scalar_type_spec.rb
graphql-0.18.8 spec/graphql/scalar_type_spec.rb
graphql-0.18.7 spec/graphql/scalar_type_spec.rb
graphql-0.18.6 spec/graphql/scalar_type_spec.rb
graphql-0.18.5 spec/graphql/scalar_type_spec.rb
graphql-0.18.4 spec/graphql/scalar_type_spec.rb
graphql-0.18.3 spec/graphql/scalar_type_spec.rb
graphql-0.18.2 spec/graphql/scalar_type_spec.rb