2019-12-04 20:38:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
require 'rubocop_spec_helper'
|
2019-12-04 20:38:33 +05:30
|
|
|
require_relative '../../../../rubocop/cop/graphql/descriptions'
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
RSpec.describe RuboCop::Cop::Graphql::Descriptions do
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'with fields' do
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'adds an offense when there is no description' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~TYPE)
|
2019-12-04 20:38:33 +05:30
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_NO_DESCRIPTION}
|
|
|
|
GraphQL::Types::String,
|
2019-12-04 20:38:33 +05:30
|
|
|
null: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'adds an offense when description does not end in a period' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~TYPE)
|
2019-12-04 20:38:33 +05:30
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
2021-02-22 17:27:13 +05:30
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_NO_PERIOD}
|
|
|
|
GraphQL::Types::String,
|
2019-12-04 20:38:33 +05:30
|
|
|
null: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Description of a thing'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description begins with "A"' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
|
|
|
GraphQL::Types::String,
|
|
|
|
null: false,
|
|
|
|
description: 'A description of the thing.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description begins with "The"' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
|
|
|
GraphQL::Types::String,
|
|
|
|
null: false,
|
|
|
|
description: 'The description of the thing.'
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when description is correct' do
|
|
|
|
expect_no_offenses(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
2021-10-27 15:23:28 +05:30
|
|
|
description: 'Description of a thing.'
|
2021-02-22 17:27:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
it 'does not add an offense when there is a resolver' do
|
|
|
|
expect_no_offenses(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing, resolver: ThingResolver
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'with arguments' do
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'adds an offense when there is no description' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~TYPE)
|
2019-12-04 20:38:33 +05:30
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
argument :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^ #{described_class::MSG_NO_DESCRIPTION}
|
|
|
|
GraphQL::Types::String,
|
2019-12-04 20:38:33 +05:30
|
|
|
null: false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'adds an offense when description does not end in a period' do
|
2021-03-11 19:13:27 +05:30
|
|
|
expect_offense(<<~TYPE)
|
2019-12-04 20:38:33 +05:30
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
2021-02-22 17:27:13 +05:30
|
|
|
argument :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^ #{described_class::MSG_NO_PERIOD}
|
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
|
|
|
description: 'Behold! A description'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'adds an offense when description begins with "A"' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
argument :a_thing,
|
|
|
|
^^^^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
|
|
|
GraphQL::Types::String,
|
|
|
|
null: false,
|
|
|
|
description: 'A description.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description begins with "The"' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
argument :a_thing,
|
|
|
|
^^^^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
|
|
|
GraphQL::Types::String,
|
|
|
|
null: false,
|
|
|
|
description: 'The description.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
it 'does not add an offense when description is correct' do
|
|
|
|
expect_no_offenses(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
2019-12-04 20:38:33 +05:30
|
|
|
argument :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
|
|
|
description: 'Behold! A description.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'with enum values' do
|
2021-04-17 20:07:23 +05:30
|
|
|
it 'adds an offense when there is no description' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', value: 'foo'
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG_NO_DESCRIPTION}
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description does not end in a period' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', value: 'foo', description: 'bar'
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG_NO_PERIOD}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description begins with "The"' do
|
|
|
|
expect_offense(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', value: 'foo', description: 'The description.'
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an offense when description begins with "A"' do
|
|
|
|
expect_offense(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', value: 'foo', description: 'A description.'
|
|
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{described_class::MSG_BAD_START}
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when description is correct (defined using `description:`)' do
|
|
|
|
expect_no_offenses(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', value: 'foo', description: 'bar.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add an offense when description is correct (defined as a second argument)' do
|
|
|
|
expect_no_offenses(<<~TYPE.strip)
|
|
|
|
module Types
|
|
|
|
class FakeEnum < BaseEnum
|
|
|
|
value 'FOO', 'bar.', value: 'foo'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
describe 'autocorrecting descriptions without periods' do
|
|
|
|
it 'can autocorrect' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_NO_PERIOD}
|
|
|
|
GraphQL::Types::String,
|
2019-12-04 20:38:33 +05:30
|
|
|
null: false,
|
|
|
|
description: 'Behold! A description'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
expect_correction(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
|
|
|
description: 'Behold! A description.'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can autocorrect a heredoc' do
|
|
|
|
expect_offense(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
^^^^^^^^^^^^^^^ #{described_class::MSG_NO_PERIOD}
|
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
|
|
|
description: <<~DESC
|
|
|
|
Behold! A description
|
|
|
|
DESC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
|
|
|
|
|
|
|
expect_correction(<<~TYPE)
|
|
|
|
module Types
|
|
|
|
class FakeType < BaseObject
|
|
|
|
field :a_thing,
|
2021-10-27 15:23:28 +05:30
|
|
|
GraphQL::Types::String,
|
2021-02-22 17:27:13 +05:30
|
|
|
null: false,
|
|
|
|
description: <<~DESC
|
|
|
|
Behold! A description.
|
|
|
|
DESC
|
|
|
|
end
|
|
|
|
end
|
|
|
|
TYPE
|
2019-12-04 20:38:33 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|