upload_many_pokemon_spec.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require './lib/pk_parse'
  4. RSpec.describe Mutations::UploadManyPokemon, 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_many_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_files) }
  13. end
  14. describe '#resolve' do
  15. subject { mutation.resolve(base64_encoded_pokemon_files: [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, nickname: nickname},
  24. {pokedex_number: pokedex_number, 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].length).to eq(2)
  33. end
  34. specify { expect { subject }.to change(Pokemon, :count).by(2) }
  35. end
  36. context 'when pokemon could not be created' do
  37. before do
  38. allow_any_instance_of(PKParse::Client).to(
  39. receive(:parse_base64).and_raise(PKParse::Error.new('foo')),
  40. )
  41. end
  42. it 'returns errors' do
  43. expect(subject[:errors].length).to eq(1)
  44. expect(subject[:errors].first).to respond_to(:message)
  45. end
  46. end
  47. end
  48. end