Welcome to mirror list, hosted at ThFree Co, Russian Federation.

1_acts_as_taggable_spec.rb « initializers « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f9ccc9718d5ba1f48b7cc76821ae94aa5a3731b8 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'ActsAsTaggableOn::Tag' do
  describe '.find_or_create_all_with_like_by_name' do
    let(:tags) { %w[tag] }

    subject(:find_or_create) { ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tags) }

    it 'creates a tag' do
      expect { find_or_create }.to change(ActsAsTaggableOn::Tag, :count).by(1)
    end

    it 'returns the Tag record' do
      results = find_or_create

      expect(results.size).to eq(1)
      expect(results.first).to be_an_instance_of(ActsAsTaggableOn::Tag)
      expect(results.first.name).to eq('tag')
    end

    context 'some tags already existing' do
      let(:tags) { %w[tag preexisting_tag tag2] }

      before_all do
        ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag')
      end

      it 'creates only the missing tag' do
        expect(ActsAsTaggableOn::Tag).to receive(:insert_all)
          .with([{ name: 'tag' }, { name: 'tag2' }], unique_by: :name)
          .and_call_original

        expect { find_or_create }.to change(ActsAsTaggableOn::Tag, :count).by(2)
      end

      it 'returns the Tag records' do
        results = find_or_create

        expect(results.map(&:name)).to match_array(tags)
      end
    end

    context 'all tags already existing' do
      let(:tags) { %w[preexisting_tag preexisting_tag2] }

      before_all do
        ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag')
        ActsAsTaggableOn::Tag.create!(name: 'preexisting_tag2')
      end

      it 'does not create new tags' do
        expect { find_or_create }.not_to change(ActsAsTaggableOn::Tag, :count)
      end

      it 'returns the Tag records' do
        results = find_or_create

        expect(results.map(&:name)).to match_array(tags)
      end
    end
  end
end