tags_finder_spec.rb 2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
# frozen_string_literal: true

require 'spec_helper'

describe Autocomplete::ActsAsTaggableOn::TagsFinder do
  describe '#execute' do
    context 'with empty params' do
      it 'returns all tags' do
        create :ci_runner, tag_list: ['tag1']
        create :ci_runner, tag_list: ['tag2']

        tags = described_class.new(taggable_type: Ci::Runner, params: {}).execute.map(&:name)

        expect(tags).to match_array %w(tag1 tag2)
      end
    end

    context 'filter by search' do
      context 'with an empty search term' do
        it 'returns an empty collection' do
          create :ci_runner, tag_list: ['tag1']
          create :ci_runner, tag_list: ['tag2']

          tags = described_class.new(taggable_type: Ci::Runner, params: { search: '' }).execute.map(&:name)

          expect(tags).to be_empty
        end
      end

      context 'with a search containing 2 characters' do
        it 'returns the tag that strictly matches the search term' do
          create :ci_runner, tag_list: ['t1']
          create :ci_runner, tag_list: ['t11']

          tags = described_class.new(taggable_type: Ci::Runner, params: { search: 't1' }).execute.map(&:name)

          expect(tags).to match_array ['t1']
        end
      end

      context 'with a search containing 3 characters' do
        it 'returns the tag that partially matches the search term' do
          create :ci_runner, tag_list: ['tag1']
          create :ci_runner, tag_list: ['tag11']

          tags = described_class.new(taggable_type: Ci::Runner, params: { search: 'ag1' }).execute.map(&:name)

          expect(tags).to match_array %w(tag1 tag11)
        end
      end
    end

    context 'limit' do
      it 'limits the result set by the limit constant' do
        stub_const("#{described_class}::LIMIT", 1)

        create :ci_runner, tag_list: ['tag1']
        create :ci_runner, tag_list: ['tag2']

        tags = described_class.new(taggable_type: Ci::Runner, params: { search: 'tag' }).execute

        expect(tags.count).to eq 1
      end
    end
  end
end