todos_spec.rb 5.15 KB
Newer Older
Alfredo Sumaran's avatar
Alfredo Sumaran committed
1 2 3
require 'spec_helper'

describe 'Dashboard Todos', feature: true do
4 5
  let(:user)    { create(:user) }
  let(:author)  { create(:user) }
6
  let(:project) { create(:project, visibility_level: Gitlab::VisibilityLevel::PUBLIC) }
Phil Hughes's avatar
Phil Hughes committed
7
  let(:issue)   { create(:issue, due_date: Date.today) }
Alfredo Sumaran's avatar
Alfredo Sumaran committed
8 9

  describe 'GET /dashboard/todos' do
Alfredo Sumaran's avatar
Alfredo Sumaran committed
10
    context 'User does not have todos' do
Alfredo Sumaran's avatar
Alfredo Sumaran committed
11 12 13 14 15
      before do
        login_as(user)
        visit dashboard_todos_path
      end
      it 'shows "All done" message' do
Phil Hughes's avatar
Phil Hughes committed
16
        expect(page).to have_content "Todos let you see what you should do next."
Alfredo Sumaran's avatar
Alfredo Sumaran committed
17 18 19 20 21 22 23 24 25 26
      end
    end

    context 'User has a todo', js: true do
      before do
        create(:todo, :mentioned, user: user, project: project, target: issue, author: author)
        login_as(user)
        visit dashboard_todos_path
      end

27
      it 'has todo present' do
Alfredo Sumaran's avatar
Alfredo Sumaran committed
28 29 30
        expect(page).to have_selector('.todos-list .todo', count: 1)
      end

Phil Hughes's avatar
Phil Hughes committed
31 32 33 34 35 36
      it 'shows due date as today' do
        page.within first('.todo') do
          expect(page).to have_content 'Due today'
        end
      end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
37 38 39 40 41 42 43 44 45 46
      describe 'deleting the todo' do
        before do
          first('.done-todo').click
        end

        it 'is removed from the list' do
          expect(page).not_to have_selector('.todos-list .todo')
        end

        it 'shows "All done" message' do
47
          expect(page).to have_selector('.todos-all-done', count: 1)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
48 49
        end
      end
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

      context 'todo is stale on the page' do
        before do
          todos = TodosFinder.new(user, state: :pending).execute
          TodoService.new.mark_todos_as_done(todos, user)
        end

        describe 'deleting the todo' do
          before do
            first('.done-todo').click
          end

          it 'is removed from the list' do
            expect(page).not_to have_selector('.todos-list .todo')
          end

          it 'shows "All done" message' do
67
            expect(page).to have_selector('.todos-all-done', count: 1)
68 69 70
          end
        end
      end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
71 72
    end

73 74 75 76 77 78
    context 'User has Todos with labels spanning multiple projects' do
      before do
        label1 = create(:label, project: project)
        note1 = create(:note_on_issue, note: "Hello #{label1.to_reference(format: :name)}", noteable_id: issue.id, noteable_type: 'Issue', project: issue.project)
        create(:todo, :mentioned, project: project, target: issue, user: user, note_id: note1.id)

DJ Mountney's avatar
DJ Mountney committed
79
        project2 = create(:project, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
80 81 82 83 84 85 86 87 88 89 90 91 92 93
        label2 = create(:label, project: project2)
        issue2 = create(:issue, project: project2)
        note2 = create(:note_on_issue, note: "Test #{label2.to_reference(format: :name)}", noteable_id: issue2.id, noteable_type: 'Issue', project: project2)
        create(:todo, :mentioned, project: project2, target: issue2, user: user, note_id: note2.id)

        login_as(user)
        visit dashboard_todos_path
      end

      it 'shows page with two Todos' do
        expect(page).to have_selector('.todos-list .todo', count: 2)
      end
    end

Alfredo Sumaran's avatar
Alfredo Sumaran committed
94 95
    context 'User has multiple pages of Todos' do
      before do
96 97 98 99
        allow(Todo).to receive(:default_per_page).and_return(1)

        # Create just enough records to cause us to paginate
        create_list(:todo, 2, :mentioned, user: user, project: project, target: issue, author: author)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
100 101 102 103 104

        login_as(user)
      end

      it 'is paginated' do
105 106
        visit dashboard_todos_path

Alfredo Sumaran's avatar
Alfredo Sumaran committed
107 108 109 110
        expect(page).to have_selector('.gl-pagination')
      end

      it 'is has the right number of pages' do
111 112 113
        visit dashboard_todos_path

        expect(page).to have_selector('.gl-pagination .page', count: 2)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
114 115
      end

116
      describe 'completing last todo from last page', js: true do
Alfredo Sumaran's avatar
Alfredo Sumaran committed
117
        it 'redirects to the previous page' do
118
          visit dashboard_todos_path(page: 2)
Alfredo Sumaran's avatar
Alfredo Sumaran committed
119
          expect(page).to have_css("#todo_#{Todo.last.id}")
120 121 122 123

          click_link('Done')

          expect(current_path).to eq dashboard_todos_path
Alfredo Sumaran's avatar
Alfredo Sumaran committed
124
          expect(page).to have_css("#todo_#{Todo.first.id}")
Alfredo Sumaran's avatar
Alfredo Sumaran committed
125 126
        end
      end
127 128 129 130 131 132 133 134 135

      describe 'mark all as done', js: true do
        before do
          visit dashboard_todos_path
          click_link('Mark all as done')
        end

        it 'shows "All done" message!' do
          expect(page).to have_content 'To do 0'
Phil Hughes's avatar
Phil Hughes committed
136
          expect(page).to have_content "You're all done!"
137 138 139
          expect(page).not_to have_selector('.gl-pagination')
        end
      end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
140
    end
141 142 143

    context 'User has a Todo in a project pending deletion' do
      before do
DJ Mountney's avatar
DJ Mountney committed
144
        deleted_project = create(:project, visibility_level: Gitlab::VisibilityLevel::PUBLIC, pending_delete: true)
145
        create(:todo, :mentioned, user: user, project: deleted_project, target: issue, author: author)
146
        create(:todo, :mentioned, user: user, project: deleted_project, target: issue, author: author, state: :done)
147 148 149 150 151
        login_as(user)
        visit dashboard_todos_path
      end

      it 'shows "All done" message' do
152 153 154
        within('.todos-pending-count') { expect(page).to have_content '0' }
        expect(page).to have_content 'To do 0'
        expect(page).to have_content 'Done 0'
155
        expect(page).to have_selector('.todos-all-done', count: 1)
156 157
      end
    end
Alfredo Sumaran's avatar
Alfredo Sumaran committed
158 159
  end
end