2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
RSpec::Matchers.define :require_graphql_authorizations do |*expected|
|
2020-05-04 23:09:46 -04:00
|
|
|
match do |klass|
|
|
|
|
permissions = if klass.respond_to?(:required_permissions)
|
|
|
|
klass.required_permissions
|
|
|
|
else
|
|
|
|
[klass.to_graphql.metadata[:authorize]]
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(permissions).to eq(expected)
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec::Matchers.define :have_graphql_fields do |*expected|
|
2018-06-25 04:59:00 -04:00
|
|
|
def expected_field_names
|
2019-12-23 07:08:18 -05:00
|
|
|
Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
|
2018-06-25 04:59:00 -04:00
|
|
|
end
|
|
|
|
|
2020-01-08 16:08:08 -05:00
|
|
|
@allow_extra = false
|
|
|
|
|
|
|
|
chain :only do
|
|
|
|
@allow_extra = false
|
|
|
|
end
|
|
|
|
|
|
|
|
chain :at_least do
|
|
|
|
@allow_extra = true
|
|
|
|
end
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
match do |kls|
|
2020-01-08 16:08:08 -05:00
|
|
|
if @allow_extra
|
|
|
|
expect(kls.fields.keys).to include(*expected_field_names)
|
|
|
|
else
|
|
|
|
expect(kls.fields.keys).to contain_exactly(*expected_field_names)
|
|
|
|
end
|
2018-06-25 04:59:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |kls|
|
|
|
|
missing = expected_field_names - kls.fields.keys
|
|
|
|
extra = kls.fields.keys - expected_field_names
|
|
|
|
|
|
|
|
message = []
|
|
|
|
|
|
|
|
message << "is missing fields: <#{missing.inspect}>" if missing.any?
|
2020-01-08 16:08:08 -05:00
|
|
|
message << "contained unexpected fields: <#{extra.inspect}>" if extra.any? && !@allow_extra
|
2018-06-25 04:59:00 -04:00
|
|
|
|
|
|
|
message.join("\n")
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-11 07:08:10 -05:00
|
|
|
RSpec::Matchers.define :include_graphql_fields do |*expected|
|
|
|
|
expected_field_names = expected.map { |name| GraphqlHelpers.fieldnamerize(name) }
|
|
|
|
|
|
|
|
match do |kls|
|
|
|
|
expect(kls.fields.keys).to include(*expected_field_names)
|
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |kls|
|
|
|
|
missing = expected_field_names - kls.fields.keys
|
|
|
|
"is missing fields: <#{missing.inspect}>" if missing.any?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 14:08:05 -04:00
|
|
|
RSpec::Matchers.define :have_graphql_field do |field_name, args = {}|
|
2018-06-14 09:06:53 -04:00
|
|
|
match do |kls|
|
2019-10-17 14:08:05 -04:00
|
|
|
field = kls.fields[GraphqlHelpers.fieldnamerize(field_name)]
|
|
|
|
|
|
|
|
expect(field).to be_present
|
|
|
|
|
|
|
|
args.each do |argument, value|
|
|
|
|
expect(field.send(argument)).to eq(value)
|
|
|
|
end
|
2018-06-14 09:06:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-10 10:19:45 -04:00
|
|
|
RSpec::Matchers.define :have_graphql_mutation do |mutation_class|
|
|
|
|
match do |mutation_type|
|
|
|
|
field = mutation_type.fields[GraphqlHelpers.fieldnamerize(mutation_class.graphql_name)]
|
|
|
|
|
|
|
|
expect(field).to be_present
|
|
|
|
expect(field.resolver).to eq(mutation_class)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-02 20:08:38 -04:00
|
|
|
# note: connection arguments do not have to be named, they will be inferred.
|
2017-08-16 09:04:41 -04:00
|
|
|
RSpec::Matchers.define :have_graphql_arguments do |*expected|
|
2018-05-23 03:55:14 -04:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
2020-06-02 20:08:38 -04:00
|
|
|
def expected_names(field)
|
2020-05-21 14:08:27 -04:00
|
|
|
@names ||= Array.wrap(expected).map { |name| GraphqlHelpers.fieldnamerize(name) }
|
2020-06-02 20:08:38 -04:00
|
|
|
|
|
|
|
if field.type.try(:ancestors)&.include?(GraphQL::Types::Relay::BaseConnection)
|
|
|
|
@names | %w(after before first last)
|
|
|
|
else
|
|
|
|
@names
|
|
|
|
end
|
2020-05-21 14:08:27 -04:00
|
|
|
end
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
match do |field|
|
2020-06-02 20:08:38 -04:00
|
|
|
names = expected_names(field)
|
|
|
|
|
|
|
|
expect(field.arguments.keys).to contain_exactly(*names)
|
2020-05-21 14:08:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
failure_message do |field|
|
2020-06-02 20:08:38 -04:00
|
|
|
names = expected_names(field)
|
|
|
|
|
|
|
|
"expected that #{field.name} would have the following fields: #{names.inspect}, but it has #{field.arguments.keys.inspect}."
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec::Matchers.define :have_graphql_type do |expected|
|
|
|
|
match do |field|
|
2020-03-18 14:09:35 -04:00
|
|
|
expect(field.type).to eq(expected)
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 07:07:55 -05:00
|
|
|
RSpec::Matchers.define :have_non_null_graphql_type do |expected|
|
|
|
|
match do |field|
|
2020-03-18 14:09:35 -04:00
|
|
|
expect(field.type.to_graphql).to eq(!expected.to_graphql)
|
2019-12-10 07:07:55 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-16 09:04:41 -04:00
|
|
|
RSpec::Matchers.define :have_graphql_resolver do |expected|
|
|
|
|
match do |field|
|
2018-05-23 03:55:14 -04:00
|
|
|
case expected
|
|
|
|
when Method
|
2020-03-18 14:09:35 -04:00
|
|
|
expect(field.to_graphql.metadata[:type_class].resolve_proc).to eq(expected)
|
2018-05-23 03:55:14 -04:00
|
|
|
else
|
2020-03-18 14:09:35 -04:00
|
|
|
expect(field.to_graphql.metadata[:type_class].resolver).to eq(expected)
|
2018-05-23 03:55:14 -04:00
|
|
|
end
|
2017-08-16 09:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-25 04:59:00 -04:00
|
|
|
|
2020-01-29 07:09:08 -05:00
|
|
|
RSpec::Matchers.define :have_graphql_extension do |expected|
|
|
|
|
match do |field|
|
2020-03-18 14:09:35 -04:00
|
|
|
expect(field.to_graphql.metadata[:type_class].extensions).to include(expected)
|
2020-01-29 07:09:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-25 04:59:00 -04:00
|
|
|
RSpec::Matchers.define :expose_permissions_using do |expected|
|
|
|
|
match do |type|
|
|
|
|
permission_field = type.fields['userPermissions']
|
|
|
|
|
|
|
|
expect(permission_field).not_to be_nil
|
|
|
|
expect(permission_field.type).to be_non_null
|
|
|
|
expect(permission_field.type.of_type.graphql_name).to eq(expected.graphql_name)
|
|
|
|
end
|
|
|
|
end
|