# frozen_string_literal: true require 'rails_helper' require './lib/pk_parse' RSpec.describe Mutations::UploadPokemon, type: :graphql do subject(:mutation) { described_class.new(object: nil, context: {}) } describe 'fields and arguments' do subject do described_class.fields[GraphQLHelpers.fieldnamerize(:upload_pokemon)] end let!(:described_class) { PokemonTradeSchema.types['Mutation'] } it { is_expected.to have_graphql_fields(:pokemon, :errors) } it { is_expected.to have_graphql_arguments(:base64_encoded_pokemon_file) } end describe '#resolve' do subject { mutation.resolve(base64_encoded_pokemon_file: base64) } let(:nickname) { Faker::Games::Pokemon.name } let(:pokedex_number) { rand(1..151) } let(:base64) { 'deadbeef' } context 'when pokemon is created' do before do parsed_pkmn = PKParse::Response.new( [{ pokedex_number: pokedex_number, nickname: nickname, }], ) allow_any_instance_of(PKParse::Client) .to receive(:parse_base64).and_return(parsed_pkmn) 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 'when 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