debian-mirror-gitlab/spec/graphql/mutations/todos/restore_many_spec.rb

96 lines
3.1 KiB
Ruby
Raw Normal View History

2020-03-09 13:42:32 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Mutations::Todos::RestoreMany do
2021-01-03 14:25:43 +05:30
include GraphqlHelpers
2020-03-09 13:42:32 +05:30
let_it_be(:current_user) { create(:user) }
let_it_be(:author) { create(:user) }
let_it_be(:other_user) { create(:user) }
let_it_be(:todo1) { create(:todo, user: current_user, author: author, state: :done) }
let_it_be(:todo2) { create(:todo, user: current_user, author: author, state: :pending) }
let_it_be(:other_user_todo) { create(:todo, user: other_user, author: author, state: :done) }
2020-04-08 14:13:33 +05:30
let(:mutation) { described_class.new(object: nil, context: { current_user: current_user }, field: nil) }
2020-03-09 13:42:32 +05:30
describe '#resolve' do
it 'restores a single todo' do
result = restore_mutation([todo1])
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
todo_ids = result[:updated_ids]
expect(todo_ids.size).to eq(1)
expect(todo_ids.first).to eq(todo1.to_global_id.to_s)
2020-07-28 23:09:34 +05:30
expect(result[:todos]).to contain_exactly(todo1)
2020-03-09 13:42:32 +05:30
end
it 'handles a todo which is already pending as expected' do
result = restore_mutation([todo2])
expect_states_were_not_changed
expect(result[:updated_ids]).to eq([])
2020-07-28 23:09:34 +05:30
expect(result[:todos]).to be_empty
2020-03-09 13:42:32 +05:30
end
it 'ignores requests for todos which do not belong to the current user' do
restore_mutation([other_user_todo])
expect_states_were_not_changed
end
2021-01-03 14:25:43 +05:30
it 'raises an error with invalid or non-Todo GIDs' do
expect { mutation.resolve(ids: [author.to_global_id.to_s]) }
.to raise_error(GraphQL::CoercionError)
2020-03-09 13:42:32 +05:30
expect_states_were_not_changed
end
it 'restores multiple todos' do
todo4 = create(:todo, user: current_user, author: author, state: :done)
result = restore_mutation([todo1, todo4, todo2])
expect(result[:updated_ids].size).to eq(2)
returned_todo_ids = result[:updated_ids]
expect(returned_todo_ids).to contain_exactly(todo1.to_global_id.to_s, todo4.to_global_id.to_s)
2020-07-28 23:09:34 +05:30
expect(result[:todos]).to contain_exactly(todo1, todo4)
2020-03-09 13:42:32 +05:30
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(todo4.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
it 'fails if one todo does not belong to the current user' do
restore_mutation([todo1, todo2, other_user_todo])
expect(todo1.reload.state).to eq('pending')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
it 'fails if too many todos are requested for update' do
expect { restore_mutation([todo1] * 51) }.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
end
def restore_mutation(todos)
mutation.resolve(ids: todos.map { |todo| global_id_of(todo) } )
end
def expect_states_were_not_changed
expect(todo1.reload.state).to eq('done')
expect(todo2.reload.state).to eq('pending')
expect(other_user_todo.reload.state).to eq('done')
end
end