upload_pokemon_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require './lib/pk_parse'
  4. RSpec.describe Mutations::UploadPokemon, type: :graphql do
  5. subject(:mutation) { described_class.new(object: nil, context: {}) }
  6. describe 'fields and arguments' do
  7. subject do
  8. described_class.fields[GraphQLHelpers.fieldnamerize(:upload_pokemon)]
  9. end
  10. let!(:described_class) { PokemonTradeSchema.types['Mutation'] }
  11. it { is_expected.to have_graphql_fields(:pokemon, :errors) }
  12. it { is_expected.to have_graphql_arguments(:base64_encoded_pokemon_file) }
  13. end
  14. describe '#resolve' do
  15. subject { mutation.resolve(base64_encoded_pokemon_file: base64) }
  16. let(:nickname) { Faker::Games::Pokemon.name }
  17. let(:pokedex_number) { rand(1..151) }
  18. let(:base64) { 'deadbeef' }
  19. context 'when pokemon is created' do
  20. before do
  21. parsed_pkmn = PKParse::Response.new(
  22. [{
  23. pokedex_number: pokedex_number,
  24. nickname: nickname,
  25. }],
  26. )
  27. allow_any_instance_of(PKParse::Client)
  28. .to receive(:parse_base64).and_return(parsed_pkmn)
  29. end
  30. it 'returns pokemon' do
  31. expect(subject[:pokemon]).not_to be_nil
  32. expect(subject[:pokemon]).to be_a Pokemon
  33. end
  34. specify { expect { subject }.to change(Pokemon, :count).by(1) }
  35. end
  36. context 'when pokemon could not be created' do
  37. before do
  38. allow_any_instance_of(PKParse::Client)
  39. .to receive(:parse_base64).and_raise(PKParse::Error.new('foo'))
  40. end
  41. it 'returns errors' do
  42. expect(subject[:errors].length).to eq(1)
  43. expect(subject[:errors].first).to respond_to(:message)
  44. end
  45. end
  46. end
  47. end