Commit 45e643f4 authored by Jan Provaznik's avatar Jan Provaznik Committed by Douwe Maan

Fix selection of scoped labels

When sorting labels by user selection order, any missing labels
had index 0. If only single selected label was returned by UI,
then this label had index 0 too so it was not chosen as the "last
selected" label. To fix this, any missing labels have index -1.
parent 6f8cc02c
...@@ -28,7 +28,7 @@ class ScopedLabelSet ...@@ -28,7 +28,7 @@ class ScopedLabelSet
by_index = label_ids_order.map.with_index { |id, idx| [id.to_i, idx] }.to_h by_index = label_ids_order.map.with_index { |id, idx| [id.to_i, idx] }.to_h
label_ids.max do |id1, id2| label_ids.max do |id1, id2|
by_index[id1].to_i <=> by_index[id2].to_i by_index.fetch(id1, -1) <=> by_index.fetch(id2, -1)
end end
end end
......
...@@ -84,5 +84,11 @@ describe ScopedLabelSet do ...@@ -84,5 +84,11 @@ describe ScopedLabelSet do
expect(set.last_id_by_order([kv_label1.id, kv_label3.id, kv_label2.id])).to eq(kv_label3.id) expect(set.last_id_by_order([kv_label1.id, kv_label3.id, kv_label2.id])).to eq(kv_label3.id)
end end
it 'returns last label present in the set ordered by custom order if there is single item' do
set = described_class.new('key', [kv_label1, kv_label3])
expect(set.last_id_by_order([kv_label3.id])).to eq(kv_label3.id)
end
end end
end end
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