summaryrefslogtreecommitdiff
path: root/spec/graphql/mutations/todos/mark_all_done_spec.rb
blob: f3b6bf52ef79dcd5ef28061c13d58ae033277571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Mutations::Todos::MarkAllDone do
  include GraphqlHelpers

  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: :pending) }
  let_it_be(:todo2) { create(:todo, user: current_user, author: author, state: :done) }
  let_it_be(:todo3) { create(:todo, user: current_user, author: author, state: :pending) }

  let_it_be(:other_user_todo) { create(:todo, user: other_user, author: author, state: :pending) }

  let_it_be(:user3) { create(:user) }

  specify { expect(described_class).to require_graphql_authorizations(:update_user) }

  describe '#resolve' do
    it 'marks all pending todos as done' do
      updated_todo_ids, todos = mutation_for(current_user).resolve.values_at(:updated_ids, :todos)

      expect(todo1.reload.state).to eq('done')
      expect(todo2.reload.state).to eq('done')
      expect(todo3.reload.state).to eq('done')
      expect(other_user_todo.reload.state).to eq('pending')

      expect(updated_todo_ids).to contain_exactly(todo1.id, todo3.id)
      expect(todos).to contain_exactly(todo1, todo3)
    end

    it 'behaves as expected if there are no todos for the requesting user' do
      updated_todo_ids = mutation_for(user3).resolve.dig(:updated_ids)

      expect(todo1.reload.state).to eq('pending')
      expect(todo2.reload.state).to eq('done')
      expect(todo3.reload.state).to eq('pending')
      expect(other_user_todo.reload.state).to eq('pending')

      expect(updated_todo_ids).to be_empty
    end

    context 'when user is not logged in' do
      it 'fails with the expected error' do
        expect { mutation_for(nil).resolve }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
      end
    end
  end

  def mutation_for(user)
    described_class.new(object: nil, context: { current_user: user }, field: nil)
  end
end