-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathtest_rubocop_overfetch.rb
More file actions
43 lines (32 loc) · 1.27 KB
/
test_rubocop_overfetch.rb
File metadata and controls
43 lines (32 loc) · 1.27 KB
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
# frozen_string_literal: true
require "graphql/client/erb"
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
result = investigate(@cop, "#{Root}/views/users/show.html.erb")
assert_empty result.offenses.map(&:message)
end
def test_all_fields_used_with_safe_navigation_operator
skip if RUBY_VERSION < "2.3"
result = investigate(@cop, "#{Root}/views/users/show-2-3.html.erb")
assert_empty result.offenses.map(&:message)
end
def test_field_unused
result = investigate(@cop, "#{Root}/views/users/overfetch.html.erb")
assert_equal 1, result.offenses.count
assert_equal "GraphQL/Overfetch: GraphQL field 'birthday' query but was not used in template.", result.offenses.first.message
end
private
def investigate(cop, path)
engine = GraphQL::Client::ERB.new(File.read(path))
processed_source = RuboCop::ProcessedSource.new(engine.src.dup, RUBY_VERSION.to_f, path)
commissioner = RuboCop::Cop::Commissioner.new([cop], [cop], raise_error: true)
commissioner.investigate(processed_source)
end
end