Last active
April 4, 2019 12:32
-
-
Save sparrow/d714b754b3e21911de553c4c0dd3d068 to your computer and use it in GitHub Desktop.
Test that verifies the logic of graphql mutation for project updating. This code snippet is from the article originally published on RubyGarage’s blog: https://rubygarage.org/blog/graphql-and-trailblazer-tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'support/schemas/projects/update' | |
RSpec.describe GraphqlsController, type: :controller do | |
let(:user) { create :user } | |
let!(:project) { create :project, user: user } | |
describe 'POST #create' do | |
let(:mutation) do | |
' | |
mutation($id: ID!, $title: String!) { | |
projectUpdate(input: { | |
id: $id, | |
title: $title | |
}) { | |
project { | |
id, | |
title | |
createdAt | |
updatedAt | |
}, | |
errors { | |
messages, | |
path | |
} | |
} | |
} | |
' | |
end | |
context 'unauthenticated' do | |
let(:variables) do | |
{ | |
id: project.id, | |
title: '' | |
} | |
end | |
before { post :create, params: { query: mutation, variables: variables } } | |
it 'has errors' do | |
expect(response).to match_schema(UpdateProjectSchema::NotAuthenticated) | |
expect(response).to be_ok | |
end | |
end | |
context 'authenticated' do | |
before { sign_in user } | |
context 'fail' do | |
context 'wrong project id' do | |
let(:variables) do | |
{ | |
id: 0, | |
title: '' | |
} | |
end | |
before { post :create, params: { query: mutation, variables: variables } } | |
it 'has errors' do | |
expect(response).to match_schema(UpdateProjectSchema::NotFound) | |
expect(response).to be_ok | |
end | |
end | |
context 'invalid params' do | |
let(:variables) do | |
{ | |
id: project.id, | |
title: '' | |
} | |
end | |
before { post :create, params: { query: mutation, variables: variables } } | |
it 'has errors' do | |
expect(response).to match_schema(UpdateProjectSchema::Error) | |
expect(response).to be_ok | |
end | |
end | |
end | |
context 'success' do | |
let(:variables) do | |
{ | |
id: project.id, | |
title: FFaker::DizzleIpsum.word | |
} | |
end | |
before { post :create, params: { query: mutation, variables: variables } } | |
it 'update project' do | |
expect(response).to match_schema(UpdateProjectSchema::Success) | |
expect(response).to be_ok | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment