2020-08-03 12:09:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-08-18 18:10:07 +00:00
|
|
|
require 'rubocop_spec_helper'
|
2020-08-03 12:09:47 +00:00
|
|
|
require_relative '../../../../rubocop/cop/graphql/json_type'
|
|
|
|
|
2021-01-07 18:10:38 +00:00
|
|
|
RSpec.describe RuboCop::Cop::Graphql::JSONType do
|
2021-01-20 09:10:52 +00:00
|
|
|
let(:msg) do
|
|
|
|
'Avoid using GraphQL::Types::JSON. See: https://docs.gitlab.com/ee/development/api_graphql_styleguide.html#json'
|
|
|
|
end
|
2020-08-03 12:09:47 +00:00
|
|
|
|
|
|
|
context 'fields' do
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used' do
|
2021-01-20 09:10:52 +00:00
|
|
|
expect_offense(<<~RUBY)
|
2020-08-03 12:09:47 +00:00
|
|
|
class MyType
|
|
|
|
field :some_field, GraphQL::Types::JSON
|
2021-01-20 09:10:52 +00:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-08-03 12:09:47 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
|
2021-01-20 09:10:52 +00:00
|
|
|
expect_offense(<<~RUBY)
|
2020-08-03 12:09:47 +00:00
|
|
|
class MyType
|
|
|
|
field :some_field, GraphQL::Types::JSON, null: true, description: 'My description'
|
2021-01-20 09:10:52 +00:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-08-03 12:09:47 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense for other types' do
|
|
|
|
expect_no_offenses(<<~RUBY.strip)
|
|
|
|
class MyType
|
2021-08-03 21:09:39 +00:00
|
|
|
field :some_field, GraphQL::Types::String
|
2020-08-03 12:09:47 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'arguments' do
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used' do
|
2021-01-20 09:10:52 +00:00
|
|
|
expect_offense(<<~RUBY)
|
2020-08-03 12:09:47 +00:00
|
|
|
class MyType
|
|
|
|
argument :some_arg, GraphQL::Types::JSON
|
2021-01-20 09:10:52 +00:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-08-03 12:09:47 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when GraphQL::Types::JSON is used with other keywords' do
|
2021-01-20 09:10:52 +00:00
|
|
|
expect_offense(<<~RUBY)
|
2020-08-03 12:09:47 +00:00
|
|
|
class MyType
|
|
|
|
argument :some_arg, GraphQL::Types::JSON, null: true, description: 'My description'
|
2021-01-20 09:10:52 +00:00
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg}
|
2020-08-03 12:09:47 +00:00
|
|
|
end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense for other types' do
|
|
|
|
expect_no_offenses(<<~RUBY.strip)
|
|
|
|
class MyType
|
2021-08-03 21:09:39 +00:00
|
|
|
argument :some_arg, GraphQL::Types::String
|
2020-08-03 12:09:47 +00:00
|
|
|
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
|