# frozen_string_literal: true require 'rails_helper' RSpec.describe Resolvers::BaseResolver, type: :graphql do let!(:resolver) do Class.new(described_class) do def resolve(**args) [args, args] end end end after do # We need to make sure class instance variables are unset between runs. described_class.instance_variable_set '@plurals', nil resolver.instance_variable_set '@plurals', nil end let(:instance) { resolver.new(object: nil, context: {}) } let(:args) { {} } describe '.single' do it 'returns a subclass from the resolver' do expect(resolver.single.superclass).to eq(resolver) end it 'returns the same subclass every time' do expect(resolver.single.object_id).to eq(resolver.single.object_id) end it 'returns a resolver that gives the first result from the original resolver' do result = resolve(resolver.single, args: {test: 1}) expect(result).to eq(test: 1) end end describe '.argument_with_plural' do let(:field) { :foo } let(:type) { String } let(:args) { {required: true} } subject { resolver.argument_with_plural(field, type, args) } it 'adds the plural and base fields' do expect(described_class).to receive(:argument).with(field, type, args) expect(described_class).to receive(:argument).with(:foos, [type], required: false) subject end it 'updates plurals list' do subject expect(resolver.plurals).to eq(foo: :foos) end end describe '.plurals' do subject { resolver.plurals } it 'returns hash of plurals' do expect(subject).to eq({}) resolver.argument_with_plural(:foo, String, required: false) expect(subject).to eq(foo: :foos) end end describe '#plurals' do subject { instance.plurals } before { resolver.argument_with_plural(:foo, String, required: false) } it { is_expected.to eq(foo: :foos) } end describe '#clobber_id_with_iid!' do context 'args has both id and iid' do let(:args) { {id: '1', iid: '3'} } specify { expect { instance.clobber_id_with_iid!(args) }.to raise_error(APIError::BaseError) } end context 'args has only id or iid' do before { instance.clobber_id_with_iid!(args) } subject { args } context 'args has only id' do let(:args) { {id: '1'} } it { is_expected.to eq(id: '1') } end context 'args has only iid' do let(:args) { {iid: '3'} } it { is_expected.to eq(id: '3') } end end end describe '#combine_ids_with_iids!' do before { instance.combine_ids_with_iids!(args) } subject { args } context 'args has both ids and iids' do let(:args) { {ids: '1', iids: %w[3 5]} } it { is_expected.to eq(ids: %w[1 3 5]) } end context 'args has only ids' do let(:args) { {ids: ['1']} } it { is_expected.to eq(ids: ['1']) } end context 'args has only iids' do let(:args) { {iids: %w[3 5]} } it { is_expected.to eq(ids: %w[3 5]) } end end describe '#combine_plurals!' do subject { args } before do resolver.argument_with_plural(:foo, String, required: false) resolver.argument_with_plural(:bar, String, required: false) instance.combine_plurals!(args) end context 'args has both singular and plural' do let!(:args) { {foo: '1', foos: %w[3 5], bar: 'a', bars: %w[b c]} } it { is_expected.to eq(foo: %w[1 3 5], bar: %w[a b c]) } end context 'args has only singular' do let!(:args) { {foo: ['1']} } it { is_expected.to eq(foo: ['1']) } end context 'args has only plural' do let!(:args) { {foos: %w[3 5]} } it { is_expected.to eq(foo: %w[3 5]) } end end describe '#decode_id' do subject { instance.decode_id(args) } context 'with id given' do let(:args) { {id: '1'} } it 'uses the GraphQL decode built in' do expect(GraphQL::Schema::UniqueWithinType).to receive(:decode).with(args[:id]).and_return([nil, 'not 1']) subject expect(args[:id]).to eq('not 1') end end context 'without id given' do it 'changes nothing' do expect(GraphQL::Schema::UniqueWithinType).not_to receive(:decode) subject expect(args[:id]).to be_nil end end end describe '#decode_ids' do subject { instance.decode_ids(args) } context 'with ids given' do let(:args) { {ids: ['1']} } it 'uses the GraphQL decode built in' do expect(GraphQL::Schema::UniqueWithinType).to receive(:decode).with(args[:ids].first).and_return([nil, 'not 1']) subject expect(args[:ids]).to eq(['not 1']) end end context 'without ids given' do it 'uses the GraphQL decode built in' do expect(GraphQL::Schema::UniqueWithinType).not_to receive(:decode) subject expect(args[:ids]).to be_nil end end end describe '#prepare_args!' do subject { instance.prepare_args!(args) } it 'prepares arguments' do expect(instance).to receive(:decode_id).once expect(instance).to receive(:decode_ids).once expect(instance).to receive(:clobber_id_with_iid!).once expect(instance).to receive(:combine_ids_with_iids!).once expect(instance).to receive(:combine_plurals!).once subject end end end