# frozen_string_literal: true require 'rails_helper' RSpec.describe API::GraphqlController, type: :controller do describe 'POST #execute' do subject { post :execute } it 'calls GraphQL schema#execute' do expect(PokemonTradeSchema).to receive(:execute) subject expect(response).to have_http_status(:ok) end context 'failure occured' do it 'raises error' do expect(PokemonTradeSchema).to receive(:execute).and_raise('foo') subject expect(response).to have_http_status(:internal_server_error) end end context '#ensure_hash' do subject { post :execute, params: params } context 'successfully parses' do let(:params) { {variables: '{"foo": "bar"}'} } it 'parses the parameters' do subject expect(response).to have_http_status(:ok) end context 'nil param' do let(:params) { {variables: 'null'} } it 'parses the parameters' do subject expect(response).to have_http_status(:ok) end end context 'empty param' do let(:params) { {variables: ''} } it 'parses the parameters' do subject expect(response).to have_http_status(:ok) end end end context 'fails to parse' do let(:params) { {variables: 'foo'} } it 'parses the parameters' do subject expect(response).to have_http_status(:internal_server_error) end end context 'unexpected param' do let(:params) { {variables: 1} } it 'parses the parameters' do subject expect(response).to have_http_status(:internal_server_error) end end end context 'development errors' do subject { post :execute } before do allow(PokemonTradeSchema).to receive(:execute).and_raise('foo') allow(Rails.env).to receive(:development?).and_return(true) end it 'renders error JSON' do subject json = JSON.parse(response.body) expect(json).to have_key('errors') expect(response).to have_http_status(:internal_server_error) end end end end