2016-11-29 15:37:10 +03:00
|
|
|
# frozen_string_literal: true
|
2018-07-16 23:56:58 +03:00
|
|
|
require "graphql/client/erb"
|
2016-11-01 01:28:09 +03:00
|
|
|
require "rubocop/cop/graphql/overfetch"
|
|
|
|
require "minitest/autorun"
|
|
|
|
|
|
|
|
class TestRubocopOverfetch < MiniTest::Test
|
|
|
|
Root = File.expand_path("..", __FILE__)
|
|
|
|
|
|
|
|
def setup
|
|
|
|
config = RuboCop::Config.new
|
|
|
|
@cop = RuboCop::Cop::GraphQL::Overfetch.new(config)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_all_fields_used
|
|
|
|
investigate(@cop, "#{Root}/views/users/show.html.erb")
|
|
|
|
|
|
|
|
assert_empty @cop.offenses.map(&:message)
|
|
|
|
end
|
|
|
|
|
2017-01-25 20:41:02 +03:00
|
|
|
def test_all_fields_used_with_safe_navigation_operator
|
|
|
|
skip if RUBY_VERSION < "2.3"
|
|
|
|
|
|
|
|
investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")
|
|
|
|
|
|
|
|
assert_empty @cop.offenses.map(&:message)
|
|
|
|
end
|
|
|
|
|
2016-11-01 01:28:09 +03:00
|
|
|
def test_field_unused
|
|
|
|
investigate(@cop, "#{Root}/views/users/overfetch.html.erb")
|
|
|
|
|
|
|
|
assert_equal 1, @cop.offenses.count
|
|
|
|
assert_equal "GraphQL field 'birthday' query but was not used in template.", @cop.offenses.first.message
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def investigate(cop, path)
|
2018-07-16 23:56:58 +03:00
|
|
|
engine = GraphQL::Client::ERB.new(File.read(path))
|
2018-07-16 23:47:43 +03:00
|
|
|
processed_source = RuboCop::ProcessedSource.new(engine.src.dup, RUBY_VERSION.to_f, path)
|
2016-11-01 01:28:09 +03:00
|
|
|
commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true)
|
|
|
|
commissioner.investigate(processed_source)
|
|
|
|
commissioner
|
|
|
|
end
|
|
|
|
end
|