summaryrefslogtreecommitdiff
path: root/spec/rubocop/cop/graphql/json_type_spec.rb
blob: c72e5b5b1c91bd5d7c92b5da7ab1e8a6fdd166c3 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

require 'rubocop_spec_helper'
require_relative '../../../../rubocop/cop/graphql/json_type'

RSpec.describe RuboCop::Cop::Graphql::JSONType do
  let(:msg) do
    'Avoid using GraphQL::Types::JSON. See: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#json'
  end

  context 'fields' do
    it 'adds an offense when GraphQL::Types::JSON is used' do
      expect_offense(<<~RUBY)
        class MyType
          field :some_field, GraphQL::Types::JSON
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
        end
      RUBY
    end

    it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
      expect_offense(<<~RUBY)
        class MyType
          field :some_field, GraphQL::Types::JSON, null: true, description: 'My description'
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
        end
      RUBY
    end

    it 'does not add an offense for other types' do
      expect_no_offenses(<<~RUBY.strip)
        class MyType
          field :some_field, GraphQL::Types::String
        end
      RUBY
    end
  end

  context 'arguments' do
    it 'adds an offense when GraphQL::Types::JSON is used' do
      expect_offense(<<~RUBY)
        class MyType
          argument :some_arg, GraphQL::Types::JSON
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
        end
      RUBY
    end

    it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
      expect_offense(<<~RUBY)
        class MyType
          argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description'
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
        end
      RUBY
    end

    it 'does not add an offense for other types' do
      expect_no_offenses(<<~RUBY.strip)
        class MyType
          argument :some_arg, GraphQL::Types::String
        end
      RUBY
    end
  end

  it 'does not add an offense for uses outside of field or argument' do
    expect_no_offenses(<<~RUBY.strip)
      class MyType
        foo :some_field, GraphQL::Types::JSON
      end
    RUBY
  end
end