1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe 'Marking all todos done' do
6  include GraphqlHelpers
7
8  let_it_be(:project) { create(:project) }
9  let_it_be(:issue) { create(:issue, project: project) }
10  let_it_be(:current_user) { create(:user) }
11  let_it_be(:author) { create(:user) }
12  let_it_be(:other_user) { create(:user) }
13  let_it_be(:other_user2) { create(:user) }
14
15  let_it_be(:todo1) { create(:todo, user: current_user, author: author, state: :pending, target: issue) }
16  let_it_be(:todo2) { create(:todo, user: current_user, author: author, state: :done, target: issue) }
17  let_it_be(:todo3) { create(:todo, user: current_user, author: author, state: :pending, target: issue) }
18
19  let_it_be(:other_user_todo) { create(:todo, user: other_user, author: author, state: :pending) }
20
21  let(:input) { {} }
22
23  let(:mutation) do
24    graphql_mutation(:todos_mark_all_done, input,
25                     <<-QL.strip_heredoc
26                       clientMutationId
27                       todos { id }
28                       errors
29                     QL
30    )
31  end
32
33  before_all do
34    project.add_developer(current_user)
35  end
36
37  def mutation_response
38    graphql_mutation_response(:todos_mark_all_done)
39  end
40
41  it 'marks all pending todos as done' do
42    post_graphql_mutation(mutation, current_user: current_user)
43
44    expect(todo1.reload.state).to eq('done')
45    expect(todo2.reload.state).to eq('done')
46    expect(todo3.reload.state).to eq('done')
47    expect(other_user_todo.reload.state).to eq('pending')
48
49    updated_todo_ids = mutation_response['todos'].map { |todo| todo['id'] }
50    expect(updated_todo_ids).to contain_exactly(global_id_of(todo1), global_id_of(todo3))
51  end
52
53  it 'behaves as expected if there are no todos for the requesting user' do
54    post_graphql_mutation(mutation, current_user: other_user2)
55
56    expect(todo1.reload.state).to eq('pending')
57    expect(todo2.reload.state).to eq('done')
58    expect(todo3.reload.state).to eq('pending')
59    expect(other_user_todo.reload.state).to eq('pending')
60
61    updated_todo_ids = mutation_response['todos']
62    expect(updated_todo_ids).to be_empty
63  end
64
65  context 'when user is not logged in' do
66    let(:current_user) { nil }
67
68    it_behaves_like 'a mutation that returns a top-level access error'
69  end
70end
71