summaryrefslogtreecommitdiff
path: root/spec/models/wiki_directory_spec.rb
blob: 9b6cec99ddbdbc7c381fea44974da52d008f4c68 (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
95
96
97
98
99
100
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WikiDirectory do
  subject(:directory) { build(:wiki_directory) }

  describe 'validations' do
    it { is_expected.to validate_presence_of(:slug) }
  end

  describe '.group_pages' do
    let_it_be(:toplevel1) { build(:wiki_page, title: 'aaa-toplevel1') }
    let_it_be(:toplevel2) { build(:wiki_page, title: 'zzz-toplevel2') }
    let_it_be(:toplevel3) { build(:wiki_page, title: 'zzz-toplevel3') }
    let_it_be(:child1) { build(:wiki_page, title: 'parent1/child1') }
    let_it_be(:child2) { build(:wiki_page, title: 'parent1/child2') }
    let_it_be(:child3) { build(:wiki_page, title: 'parent2/child3') }
    let_it_be(:grandchild1) { build(:wiki_page, title: 'parent1/subparent/grandchild1') }
    let_it_be(:grandchild2) { build(:wiki_page, title: 'parent1/subparent/grandchild2') }

    it 'returns a nested array of entries' do
      entries = described_class.group_pages(
        [toplevel1, toplevel2, toplevel3, child1, child2, child3, grandchild1, grandchild2].sort_by(&:title)
      )

      expect(entries).to match([
        toplevel1,
        a_kind_of(WikiDirectory).and(
          having_attributes(
            slug: 'parent1', entries: [
              child1,
              child2,
              a_kind_of(WikiDirectory).and(
                having_attributes(
                  slug: 'parent1/subparent',
                  entries: [grandchild1, grandchild2]
                )
              )
            ]
          )
        ),
        a_kind_of(WikiDirectory).and(
          having_attributes(
            slug: 'parent2',
            entries: [child3]
          )
        ),
        toplevel2,
        toplevel3
      ])
    end
  end

  describe '#initialize' do
    context 'when there are entries' do
      let(:entries) { [build(:wiki_page)] }
      let(:directory) { described_class.new('/path_up_to/dir', entries) }

      it 'sets the slug attribute' do
        expect(directory.slug).to eq('/path_up_to/dir')
      end

      it 'sets the entries attribute' do
        expect(directory.entries).to eq(entries)
      end
    end

    context 'when there are no entries' do
      let(:directory) { described_class.new('/path_up_to/dir') }

      it 'sets the slug attribute' do
        expect(directory.slug).to eq('/path_up_to/dir')
      end

      it 'sets the entries attribute to an empty array' do
        expect(directory.entries).to eq([])
      end
    end
  end

  describe '#title' do
    it 'returns the basename of the directory, with hyphens replaced by spaces' do
      directory.slug = 'parent'
      expect(directory.title).to eq('parent')

      directory.slug = 'parent/child'
      expect(directory.title).to eq('child')

      directory.slug = 'parent/child-foo'
      expect(directory.title).to eq('child foo')
    end
  end

  describe '#to_partial_path' do
    it 'returns the relative path to the partial to be used' do
      expect(directory.to_partial_path).to eq('../shared/wikis/wiki_directory')
    end
  end
end