Commit eccfab44 authored by Nermin Vehabovic's avatar Nermin Vehabovic

Added: Specs for sort page breaks on notes

parent 44b2d759
...@@ -299,6 +299,12 @@ module API ...@@ -299,6 +299,12 @@ module API
items.search(text) items.search(text)
end end
def order_options_with_tie_breaker
order_options = { params[:order_by] => params[:sort] }
order_options['id'] ||= 'desc'
order_options
end
# error helpers # error helpers
def forbidden!(reason = nil) def forbidden!(reason = nil)
...@@ -397,12 +403,6 @@ module API ...@@ -397,12 +403,6 @@ module API
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
def order_options_with_tie_breaker
order_options = { params[:order_by] => params[:sort] }
order_options['id'] ||= 'desc'
order_options
end
def project_finder_params def project_finder_params
finder_params = { without_deleted: true } finder_params = { without_deleted: true }
finder_params[:owned] = true if params[:owned].present? finder_params[:owned] = true if params[:owned].present?
......
...@@ -8,6 +8,7 @@ shared_examples 'noteable API' do |parent_type, noteable_type, id_name| ...@@ -8,6 +8,7 @@ shared_examples 'noteable API' do |parent_type, noteable_type, id_name|
create_list(:note, 3, params) create_list(:note, 3, params)
end end
context 'without sort params' do
it 'sorts by created_at in descending order by default' do it 'sorts by created_at in descending order by default' do
get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user) get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes", user)
...@@ -17,6 +18,37 @@ shared_examples 'noteable API' do |parent_type, noteable_type, id_name| ...@@ -17,6 +18,37 @@ shared_examples 'noteable API' do |parent_type, noteable_type, id_name|
expect(response_dates).to eq(response_dates.sort.reverse) expect(response_dates).to eq(response_dates.sort.reverse)
end end
context '2 notes with equal created_at' do
before do
@first_note = Note.first
params = { noteable: noteable, author: user }
params[:project] = parent if parent.is_a?(Project)
params[:created_at] = @first_note.created_at
@note2 = create(:note, params)
end
it 'page breaks first page correctly' do
get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes?per_page=4", user)
response_ids = json_response.map { |note| note['id'] }
expect(response_ids).to include(@note2.id)
expect(response_ids).not_to include(@first_note.id)
end
it 'page breaks second page correctly' do
get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes?per_page=4&page=2", user)
response_ids = json_response.map { |note| note['id'] }
expect(response_ids).not_to include(@note2.id)
expect(response_ids).to include(@first_note.id)
end
end
end
it 'sorts by ascending order when requested' do it 'sorts by ascending order when requested' do
get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes?sort=asc", user) get api("/#{parent_type}/#{parent.id}/#{noteable_type}/#{noteable[id_name]}/notes?sort=asc", user)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment