base_resolver_spec.rb 5.5 KB

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