# frozen_string_literal: true require 'rails_helper' require './lib/pkparse' RSpec.describe Mutations::UploadPokemon, type: :graphql do subject(:mutation) { described_class.new(object: nil, context: {}) } describe 'fields and arguments' do let!(:described_class) { PokemonTradeSchema.types['Mutation'] } subject { described_class.fields[GraphQLHelpers.fieldnamerize(:upload_pokemon)] } it { is_expected.to have_graphql_fields(:pokemon, :errors) } it { is_expected.to have_graphql_arguments(:base64_encoded_pokemon_file) } end describe '#resolve' do let(:nickname) { Faker::Games::Pokemon.name } let(:pokedex_number) { rand(1..151) } let(:base64) { 'deadbeef' } subject { mutation.resolve(base64_encoded_pokemon_file: base64) } context 'pokemon is created' do before do allow_any_instance_of(PKParse::Client).to( receive(:parse_base64).and_return( PKParse::Response.new([{pokedex_number: pokedex_number, nickname: nickname}]), ), ) end it 'returns pokemon' do expect(subject[:pokemon]).not_to be_nil expect(subject[:pokemon]).to be_a Pokemon end specify { expect { subject }.to change { Pokemon.count }.by(1) } end context 'pokemon could not be created' do before do allow_any_instance_of(PKParse::Client).to( receive(:parse_base64).and_raise(PKParse::Error.new('foo')), ) end it 'returns errors' do expect(subject[:errors].length).to eq(1) expect(subject[:errors].first).to respond_to(:message) end end end end