upload_pokemon_spec.rb 1.6 KB

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