base_resolver_spec.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Resolvers::BaseResolver, type: :graphql do
  4. let!(:resolver) do
  5. Class.new(described_class) do
  6. def resolve(**args)
  7. [args, args]
  8. end
  9. end
  10. end
  11. after do
  12. # We need to make sure class instance variables are unset between runs.
  13. described_class.instance_variable_set '@plurals', nil
  14. resolver.instance_variable_set '@plurals', nil
  15. end
  16. let(:instance) { resolver.new(object: nil, context: {}) }
  17. let(:args) { {} }
  18. describe '.single' do
  19. it 'returns a subclass from the resolver' do
  20. expect(resolver.single.superclass).to eq(resolver)
  21. end
  22. it 'returns the same subclass every time' do
  23. expect(resolver.single.object_id).to eq(resolver.single.object_id)
  24. end
  25. it 'returns a resolver that gives the first result from the original resolver' do
  26. result = resolve(resolver.single, args: {test: 1})
  27. expect(result).to eq(test: 1)
  28. end
  29. end
  30. describe '.argument_with_plural' do
  31. let(:field) { :foo }
  32. let(:type) { String }
  33. let(:args) { {required: true} }
  34. subject { resolver.argument_with_plural(field, type, args) }
  35. it 'adds the plural and base fields' do
  36. expect(described_class).to receive(:argument).with(field, type, args)
  37. expect(described_class).to receive(:argument).with(:foos, [type], required: false)
  38. subject
  39. end
  40. it 'updates plurals list' do
  41. subject
  42. expect(resolver.plurals).to eq(foo: :foos)
  43. end
  44. end
  45. describe '.plurals' do
  46. subject { resolver.plurals }
  47. it 'returns hash of plurals' do
  48. expect(subject).to eq({})
  49. resolver.argument_with_plural(:foo, String, required: false)
  50. expect(subject).to eq(foo: :foos)
  51. end
  52. end
  53. describe '#plurals' do
  54. subject { instance.plurals }
  55. before { resolver.argument_with_plural(:foo, String, required: false) }
  56. it { is_expected.to eq(foo: :foos) }
  57. end
  58. describe '#clobber_id_with_iid!' do
  59. context 'args has both id and iid' do
  60. let(:args) { {id: '1', iid: '3'} }
  61. specify { expect { instance.clobber_id_with_iid!(args) }.to raise_error(APIError::BaseError) }
  62. end
  63. context 'args has only id or iid' do
  64. before { instance.clobber_id_with_iid!(args) }
  65. subject { args }
  66. context 'args has only id' do
  67. let(:args) { {id: '1'} }
  68. it { is_expected.to eq(id: '1') }
  69. end
  70. context 'args has only iid' do
  71. let(:args) { {iid: '3'} }
  72. it { is_expected.to eq(id: '3') }
  73. end
  74. end
  75. end
  76. describe '#combine_ids_with_iids!' do
  77. before { instance.combine_ids_with_iids!(args) }
  78. subject { args }
  79. context 'args has both ids and iids' do
  80. let(:args) { {ids: '1', iids: %w[3 5]} }
  81. it { is_expected.to eq(ids: %w[1 3 5]) }
  82. end
  83. context 'args has only ids' do
  84. let(:args) { {ids: ['1']} }
  85. it { is_expected.to eq(ids: ['1']) }
  86. end
  87. context 'args has only iids' do
  88. let(:args) { {iids: %w[3 5]} }
  89. it { is_expected.to eq(ids: %w[3 5]) }
  90. end
  91. end
  92. describe '#combine_plurals!' do
  93. subject { args }
  94. before do
  95. resolver.argument_with_plural(:foo, String, required: false)
  96. resolver.argument_with_plural(:bar, String, required: false)
  97. instance.combine_plurals!(args)
  98. end
  99. context 'args has both singular and plural' do
  100. let!(:args) { {foo: '1', foos: %w[3 5], bar: 'a', bars: %w[b c]} }
  101. it { is_expected.to eq(foo: %w[1 3 5], bar: %w[a b c]) }
  102. end
  103. context 'args has only singular' do
  104. let!(:args) { {foo: ['1']} }
  105. it { is_expected.to eq(foo: ['1']) }
  106. end
  107. context 'args has only plural' do
  108. let!(:args) { {foos: %w[3 5]} }
  109. it { is_expected.to eq(foo: %w[3 5]) }
  110. end
  111. end
  112. describe '#decode_id' do
  113. subject { instance.decode_id(args) }
  114. context 'with id given' do
  115. let(:args) { {id: '1'} }
  116. it 'uses the GraphQL decode built in' do
  117. expect(GraphQL::Schema::UniqueWithinType).to receive(:decode).with(args[:id]).and_return([nil, 'not 1'])
  118. subject
  119. expect(args[:id]).to eq('not 1')
  120. end
  121. end
  122. context 'without id given' do
  123. it 'changes nothing' do
  124. expect(GraphQL::Schema::UniqueWithinType).not_to receive(:decode)
  125. subject
  126. expect(args[:id]).to be_nil
  127. end
  128. end
  129. end
  130. describe '#decode_ids' do
  131. subject { instance.decode_ids(args) }
  132. context 'with ids given' do
  133. let(:args) { {ids: ['1']} }
  134. it 'uses the GraphQL decode built in' do
  135. expect(GraphQL::Schema::UniqueWithinType).to receive(:decode).with(args[:ids].first).and_return([nil, 'not 1'])
  136. subject
  137. expect(args[:ids]).to eq(['not 1'])
  138. end
  139. end
  140. context 'without ids given' do
  141. it 'uses the GraphQL decode built in' do
  142. expect(GraphQL::Schema::UniqueWithinType).not_to receive(:decode)
  143. subject
  144. expect(args[:ids]).to be_nil
  145. end
  146. end
  147. end
  148. describe '#prepare_args!' do
  149. subject { instance.prepare_args!(args) }
  150. it 'prepares arguments' do
  151. expect(instance).to receive(:decode_id).once
  152. expect(instance).to receive(:decode_ids).once
  153. expect(instance).to receive(:clobber_id_with_iid!).once
  154. expect(instance).to receive(:combine_ids_with_iids!).once
  155. expect(instance).to receive(:combine_plurals!).once
  156. subject
  157. end
  158. end
  159. end