2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'fast_spec_helper'
|
|
|
|
require_relative '../../../../rubocop/cop/graphql/json_type'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Graphql::JSONType do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:msg) do
|
|
|
|
'Avoid using GraphQL::Types::JSON. See: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#json'
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
subject(:cop) { described_class.new }
|
|
|
|
|
|
|
|
context 'fields' do
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~RUBY)
|
2020-10-24 23:57:45 +05:30
|
|
|
class MyType
|
|
|
|
field :some_field, GraphQL::Types::JSON
|
2021-03-11 19:13:27 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~RUBY)
|
2020-10-24 23:57:45 +05:30
|
|
|
class MyType
|
|
|
|
field :some_field, GraphQL::Types::JSON, null: true, description: 'My description'
|
2021-03-11 19:13:27 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense for other types' do
|
|
|
|
expect_no_offenses(<<~RUBY.strip)
|
|
|
|
class MyType
|
2021-10-27 15:23:28 +05:30
|
|
|
field :some_field, GraphQL::Types::String
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'arguments' do
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~RUBY)
|
2020-10-24 23:57:45 +05:30
|
|
|
class MyType
|
|
|
|
argument :some_arg, GraphQL::Types::JSON
|
2021-03-11 19:13:27 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~RUBY)
|
2020-10-24 23:57:45 +05:30
|
|
|
class MyType
|
|
|
|
argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description'
|
2021-03-11 19:13:27 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense for other types' do
|
|
|
|
expect_no_offenses(<<~RUBY.strip)
|
|
|
|
class MyType
|
2021-10-27 15:23:28 +05:30
|
|
|
argument :some_arg, GraphQL::Types::String
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|