graphql_controller_spec.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe API::GraphqlController, type: :controller do
  4. describe 'POST #execute' do
  5. subject { post :execute }
  6. it 'calls GraphQL schema#execute' do
  7. expect(PokemonTradeSchema).to receive(:execute)
  8. subject
  9. expect(response).to have_http_status(:ok)
  10. end
  11. context 'failure occured' do
  12. it 'raises error' do
  13. expect(PokemonTradeSchema).to receive(:execute).and_raise('foo')
  14. subject
  15. expect(response).to have_http_status(:internal_server_error)
  16. end
  17. end
  18. context '#ensure_hash' do
  19. subject { post :execute, params: params }
  20. context 'successfully parses' do
  21. let(:params) { {variables: '{"foo": "bar"}'} }
  22. it 'parses the parameters' do
  23. subject
  24. expect(response).to have_http_status(:ok)
  25. end
  26. context 'nil param' do
  27. let(:params) { {variables: 'null'} }
  28. it 'parses the parameters' do
  29. subject
  30. expect(response).to have_http_status(:ok)
  31. end
  32. end
  33. context 'empty param' do
  34. let(:params) { {variables: ''} }
  35. it 'parses the parameters' do
  36. subject
  37. expect(response).to have_http_status(:ok)
  38. end
  39. end
  40. end
  41. context 'fails to parse' do
  42. let(:params) { {variables: 'foo'} }
  43. it 'parses the parameters' do
  44. subject
  45. expect(response).to have_http_status(:internal_server_error)
  46. end
  47. end
  48. context 'unexpected param' do
  49. let(:params) { {variables: 1} }
  50. it 'parses the parameters' do
  51. subject
  52. expect(response).to have_http_status(:internal_server_error)
  53. end
  54. end
  55. end
  56. context 'development errors' do
  57. subject { post :execute }
  58. before do
  59. allow(PokemonTradeSchema).to receive(:execute).and_raise('foo')
  60. allow(Rails.env).to receive(:development?).and_return(true)
  61. end
  62. it 'renders error JSON' do
  63. subject
  64. json = JSON.parse(response.body)
  65. expect(json).to have_key('errors')
  66. expect(response).to have_http_status(:internal_server_error)
  67. end
  68. end
  69. end
  70. end