summaryrefslogtreecommitdiff
path: root/spec/finders/autocomplete/acts_as_taggable_on/tags_finder_spec.rb
blob: 79d2f9cdb45e61d7a9bb5795d7708dcbb7f16da3 (plain)
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
        tag1 = ActsAsTaggableOn::Tag.create!(name: 'tag1')
        tag2 = ActsAsTaggableOn::Tag.create!(name: 'tag2')

        tags = described_class.new(params: {}).execute

        expect(tags).to match_array [tag1, tag2]
      end
    end

    context 'filter by search' do
      context 'with an empty search term' do
        it 'returns an empty collection' do
          ActsAsTaggableOn::Tag.create!(name: 'tag1')
          ActsAsTaggableOn::Tag.create!(name: 'tag2')

          tags = described_class.new(params: { search: '' }).execute

          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
          tag1 = ActsAsTaggableOn::Tag.create!(name: 't1')
          ActsAsTaggableOn::Tag.create!(name: 't11')

          tags = described_class.new(params: { search: 't1' }).execute

          expect(tags).to match_array [tag1]
        end
      end

      context 'with a search containing 3 characters' do
        it 'returns the tag that partially matches the search term' do
          tag1 = ActsAsTaggableOn::Tag.create!(name: 'tag1')
          tag2 = ActsAsTaggableOn::Tag.create!(name: 'tag11')

          tags = described_class.new(params: { search: 'ag1' }).execute

          expect(tags).to match_array [tag1, tag2]
        end
      end
    end

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

        ActsAsTaggableOn::Tag.create!(name: 'tag1')
        ActsAsTaggableOn::Tag.create!(name: 'tag2')

        tags = described_class.new(params: { search: 'tag' }).execute

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