upload_many_pokemon_spec.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. require './lib/pkparse'
  4. RSpec.describe Mutations::UploadManyPokemon, 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_many_pokemon)] }
  9. it { is_expected.to have_graphql_fields(:pokemon, :errors) }
  10. it { is_expected.to have_graphql_arguments(:base64_encoded_pokemon_files) }
  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_files: [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(
  22. [
  23. {pokedex_number: pokedex_number, nickname: nickname},
  24. {pokedex_number: pokedex_number, nickname: nickname},
  25. ],
  26. ),
  27. ),
  28. )
  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 '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