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

slug_spec.rb « wiki_page « models « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf256c67277942b0114da75062d699543a66e93f (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WikiPage::Slug do
  let_it_be(:meta) { create(:wiki_page_meta) }

  describe 'Associations' do
    it { is_expected.to belong_to(:wiki_page_meta) }

    it 'refers correctly to the wiki_page_meta' do
      created = create(:wiki_page_slug, wiki_page_meta: meta)

      expect(created.reload.wiki_page_meta).to eq(meta)
    end
  end

  describe 'scopes' do
    describe 'canonical' do
      subject { described_class.canonical }

      context 'there are no slugs' do
        it { is_expected.to be_empty }
      end

      context 'there are some non-canonical slugs' do
        before do
          create(:wiki_page_slug)
        end

        it { is_expected.to be_empty }
      end

      context 'there is at least one canonical slugs' do
        before do
          create(:wiki_page_slug, :canonical)
        end

        it { is_expected.not_to be_empty }
      end
    end
  end

  describe 'Validations' do
    let(:canonical) { false }

    subject do
      build(:wiki_page_slug, canonical: canonical, wiki_page_meta: meta)
    end

    it { is_expected.to validate_presence_of(:slug) }
    it { is_expected.to validate_uniqueness_of(:slug).scoped_to(:wiki_page_meta_id) }

    describe 'only_one_slug_can_be_canonical_per_meta_record' do
      context 'there are no other slugs' do
        it { is_expected.to be_valid }

        context 'the current slug is canonical' do
          let(:canonical) { true }

          it { is_expected.to be_valid }
        end
      end

      context 'there are other slugs, but they are not canonical' do
        before do
          create(:wiki_page_slug, wiki_page_meta: meta)
        end

        it { is_expected.to be_valid }

        context 'the current slug is canonical' do
          let(:canonical) { true }

          it { is_expected.to be_valid }
        end
      end

      context 'there is already a canonical slug' do
        before do
          create(:wiki_page_slug, canonical: true, wiki_page_meta: meta)
        end

        it { is_expected.to be_valid }

        context 'the current slug is canonical' do
          let(:canonical) { true }

          it { is_expected.not_to be_valid }
        end
      end
    end
  end
end