File: allow_null_validator_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (39 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download
1
2
3
4
5
6
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
39
# frozen_string_literal: true
require "spec_helper"
require_relative "./validator_helpers"

describe GraphQL::Schema::Validator::AllowNullValidator do
  include ValidatorHelpers

  it "allows nil when permitted" do
    schema = build_schema(String, {length: { minimum: 5 }, allow_null: true})
    result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
    assert_nil result["data"]["validated"]
    refute result.key?("errors")
  end

  it "rejects null by default" do
    schema = build_schema(String, {length: { minimum: 5 }})
    result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
    assert_nil result["data"]["validated"]
    assert_equal ["value is too short (minimum is 5)"], result["errors"].map { |e| e["message"] }
  end

  it "can be used standalone" do
    schema = build_schema(String, { allow_null: false })
    result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
    assert_nil result["data"]["validated"]
    assert_equal ["value can't be null"], result["errors"].map { |e| e["message"] }
  end

  it "allows nil when no validations are configured" do
    schema = build_schema(String, {})
    result = schema.execute("query($str: String) { validated(value: $str) }", variables: { str: nil })
    assert_nil result["data"]["validated"]
    refute result.key?("errors")

    result = schema.execute("query { validated }")
    assert_nil result["data"]["validated"]
    refute result.key?("errors")
  end
end