2016-07-26 17:21:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe TodosFinder do
|
|
|
|
describe '#execute' do
|
2018-03-05 11:41:48 -05:00
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:project) { create(:project, namespace: group) }
|
2018-07-16 09:35:19 -04:00
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project) }
|
2018-03-05 11:41:48 -05:00
|
|
|
let(:finder) { described_class }
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2018-03-05 11:41:48 -05:00
|
|
|
group.add_developer(user)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2018-07-16 09:35:19 -04:00
|
|
|
describe '#execute' do
|
|
|
|
context 'filtering' do
|
|
|
|
let!(:todo1) { create(:todo, user: user, project: project, target: issue) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, group: group, target: merge_request) }
|
|
|
|
|
|
|
|
it 'returns correct todos when filtered by a project' do
|
|
|
|
todos = finder.new(user, { project_id: project.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct todos when filtered by a group' do
|
|
|
|
todos = finder.new(user, { group_id: group.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo2])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns correct todos when filtered by a type' do
|
|
|
|
todos = finder.new(user, { type: 'Issue' }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1])
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with subgroups', :nested_groups do
|
|
|
|
let(:subgroup) { create(:group, parent: group) }
|
|
|
|
let!(:todo3) { create(:todo, user: user, group: subgroup, target: issue) }
|
|
|
|
|
|
|
|
it 'returns todos from subgroups when filtered by a group' do
|
|
|
|
todos = finder.new(user, { group_id: group.id }).execute
|
|
|
|
|
|
|
|
expect(todos).to match_array([todo1, todo2, todo3])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
describe '#sort' do
|
|
|
|
context 'by date' do
|
|
|
|
let!(:todo1) { create(:todo, user: user, project: project) }
|
|
|
|
let!(:todo2) { create(:todo, user: user, project: project) }
|
|
|
|
let!(:todo3) { create(:todo, user: user, project: project) }
|
|
|
|
|
|
|
|
it 'sorts with oldest created first' do
|
|
|
|
todos = finder.new(user, { sort: 'id_asc' }).execute
|
|
|
|
|
|
|
|
expect(todos.first).to eq(todo1)
|
|
|
|
expect(todos.second).to eq(todo2)
|
|
|
|
expect(todos.third).to eq(todo3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sorts with newest created first' do
|
|
|
|
todos = finder.new(user, { sort: 'id_desc' }).execute
|
|
|
|
|
|
|
|
expect(todos.first).to eq(todo3)
|
|
|
|
expect(todos.second).to eq(todo2)
|
|
|
|
expect(todos.third).to eq(todo1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sorts by priority" do
|
2018-03-05 11:41:48 -05:00
|
|
|
project_2 = create(:project)
|
|
|
|
|
2016-07-26 17:21:20 -04:00
|
|
|
label_1 = create(:label, title: 'label_1', project: project, priority: 1)
|
|
|
|
label_2 = create(:label, title: 'label_2', project: project, priority: 2)
|
|
|
|
label_3 = create(:label, title: 'label_3', project: project, priority: 3)
|
2018-03-05 11:41:48 -05:00
|
|
|
label_1_2 = create(:label, title: 'label_1', project: project_2, priority: 1)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
issue_1 = create(:issue, title: 'issue_1', project: project)
|
|
|
|
issue_2 = create(:issue, title: 'issue_2', project: project)
|
|
|
|
issue_3 = create(:issue, title: 'issue_3', project: project)
|
|
|
|
issue_4 = create(:issue, title: 'issue_4', project: project)
|
2018-03-05 11:41:48 -05:00
|
|
|
merge_request_1 = create(:merge_request, source_project: project_2)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
2018-03-05 11:41:48 -05:00
|
|
|
merge_request_1.labels << label_1_2
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
# Covers the case where Todo has more than one label
|
|
|
|
issue_3.labels << label_1
|
|
|
|
issue_3.labels << label_3
|
|
|
|
|
|
|
|
issue_2.labels << label_3
|
|
|
|
issue_1.labels << label_2
|
|
|
|
|
|
|
|
todo_1 = create(:todo, user: user, project: project, target: issue_4)
|
|
|
|
todo_2 = create(:todo, user: user, project: project, target: issue_2)
|
|
|
|
todo_3 = create(:todo, user: user, project: project, target: issue_3, created_at: 2.hours.ago)
|
|
|
|
todo_4 = create(:todo, user: user, project: project, target: issue_1)
|
2018-03-05 11:41:48 -05:00
|
|
|
todo_5 = create(:todo, user: user, project: project_2, target: merge_request_1, created_at: 1.hour.ago)
|
|
|
|
|
|
|
|
project_2.add_developer(user)
|
2016-07-26 17:21:20 -04:00
|
|
|
|
|
|
|
todos = finder.new(user, { sort: 'priority' }).execute
|
|
|
|
|
2018-03-05 11:41:48 -05:00
|
|
|
expect(todos).to eq([todo_3, todo_5, todo_4, todo_2, todo_1])
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-20 11:05:26 -04:00
|
|
|
|
|
|
|
describe '#any_for_target?' do
|
|
|
|
it 'returns true if there are any todos for the given target' do
|
|
|
|
todo = create(:todo, :pending)
|
|
|
|
finder = described_class.new(todo.user)
|
|
|
|
|
|
|
|
expect(finder.any_for_target?(todo.target)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns false if there are no todos for the given target' do
|
|
|
|
issue = create(:issue)
|
|
|
|
finder = described_class.new(issue.author)
|
|
|
|
|
|
|
|
expect(finder.any_for_target?(issue)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2016-07-26 17:21:20 -04:00
|
|
|
end
|