| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- # 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 'when 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
- describe '#ensure_hash' do
- subject { post :execute, params: params }
- context 'when successfully parses' do
- let(:params) { {variables: '{"foo": "bar"}'} }
- it 'parses the parameters' do
- subject
- expect(response).to have_http_status(:ok)
- end
- context 'when nil param' do
- let(:params) { {variables: 'null'} }
- it 'parses the parameters' do
- subject
- expect(response).to have_http_status(:ok)
- end
- end
- context 'when empty param' do
- let(:params) { {variables: ''} }
- it 'parses the parameters' do
- subject
- expect(response).to have_http_status(:ok)
- end
- end
- end
- context 'when 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 'when 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 'when 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
|