summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/assets/javascripts/todos.js.coffee43
-rw-r--r--app/views/dashboard/todos/index.html.haml1
-rw-r--r--spec/features/todos/todos_spec.rb79
3 files changed, 122 insertions, 1 deletions
diff --git a/app/assets/javascripts/todos.js.coffee b/app/assets/javascripts/todos.js.coffee
index 00d2b641723..10e698d6a54 100644
--- a/app/assets/javascripts/todos.js.coffee
+++ b/app/assets/javascripts/todos.js.coffee
@@ -1,5 +1,11 @@
class @Todos
- constructor: (@name) ->
+ constructor: (opts = {}) ->
+ {
+ @el = $('.js-todos-options')
+ } = opts
+
+ @perPage = @el.data('perPage')
+
@clearListeners()
@initBtnListeners()
@@ -26,6 +32,7 @@ class @Todos
dataType: 'json'
data: '_method': 'delete'
success: (data) =>
+ @redirectIfNeeded data.count
@clearDone $this.closest('li')
@updateBadges data
@@ -57,6 +64,40 @@ class @Todos
$('.todos-pending .badge, .todos-pending-count').text data.count
$('.todos-done .badge').text data.done_count
+ getTotalPages: ->
+ @el.data('totalPages')
+
+ getCurrentPage: ->
+ @el.data('currentPage')
+
+ getTodosPerPage: ->
+ @el.data('perPage')
+
+ redirectIfNeeded: (total) ->
+ currPages = @getTotalPages()
+ currPage = @getCurrentPage()
+
+ # Refresh if no remaining Todos
+ if not total
+ location.reload()
+ return
+
+ # Do nothing if no pagination
+ return if not currPages
+
+ newPages = Math.ceil(total / @getTodosPerPage())
+ url = location.href # Includes query strings
+
+ # If new total of pages is different than we have now
+ if newPages isnt currPages
+ # Redirect to previous page if there's one available
+ if currPages > 1 and currPage is currPages
+ pageParams =
+ page: currPages - 1
+ url = gl.utils.mergeUrlParams(pageParams, url)
+
+ Turbolinks.visit(url)
+
goToTodoUrl: (e)->
todoLink = $(this).data('url')
return unless todoLink
diff --git a/app/views/dashboard/todos/index.html.haml b/app/views/dashboard/todos/index.html.haml
index f9ec3a89158..49ab8aad1d5 100644
--- a/app/views/dashboard/todos/index.html.haml
+++ b/app/views/dashboard/todos/index.html.haml
@@ -45,6 +45,7 @@
.prepend-top-default
- if @todos.any?
+ .js-todos-options{ data: {per_page: @todos.limit_value, current_page: @todos.current_page, total_pages: @todos.total_pages} }
- @todos.group_by(&:project).each do |group|
.panel.panel-default.panel-small.js-todos-list
- project = group[0]
diff --git a/spec/features/todos/todos_spec.rb b/spec/features/todos/todos_spec.rb
new file mode 100644
index 00000000000..113d4c40cfc
--- /dev/null
+++ b/spec/features/todos/todos_spec.rb
@@ -0,0 +1,79 @@
+require 'spec_helper'
+
+describe 'Dashboard Todos', feature: true do
+ let(:user){ create(:user) }
+ let(:author){ create(:user) }
+ let(:project){ create(:project) }
+ let(:issue){ create(:issue) }
+ let(:todos_per_page){ Todo.default_per_page }
+ let(:todos_total){ todos_per_page + 1 }
+
+ describe 'GET /dashboard/todos' do
+ context 'User does not have todos' do
+ before do
+ login_as(user)
+ visit dashboard_todos_path
+ end
+ it 'shows "All done" message' do
+ expect(page).to have_content "You're all done!"
+ 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
+
+ it 'todo is present' do
+ expect(page).to have_selector('.todos-list .todo', count: 1)
+ 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
+ expect(page).to have_content("You're all done!")
+ end
+ end
+ end
+
+ context 'User has multiple pages of Todos' do
+ let(:todo_total_pages){ (todos_total.to_f/todos_per_page).ceil }
+
+ before do
+ todos_total.times do
+ create(:todo, :mentioned, user: user, project: project, target: issue, author: author)
+ end
+
+ login_as(user)
+ visit dashboard_todos_path
+ end
+
+ it 'is paginated' do
+ expect(page).to have_selector('.gl-pagination')
+ end
+
+ it 'is has the right number of pages' do
+ expect(page).to have_selector('.gl-pagination .page', count: todo_total_pages)
+ end
+
+ describe 'deleting last todo from last page', js: true do
+ it 'redirects to the previous page' do
+ page.within('.gl-pagination') do
+ click_link todo_total_pages.to_s
+ end
+ first('.done-todo').click
+ expect(page).to have_content(Todo.last.body)
+ end
+ end
+ end
+ end
+end