2019-07-07 11:18:12 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
RSpec.describe WikiDirectory do
|
2021-01-03 14:25:43 +05:30
|
|
|
subject(:directory) { build(:wiki_directory) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
describe 'validations' do
|
2017-08-17 22:00:37 +05:30
|
|
|
it { is_expected.to validate_presence_of(:slug) }
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#initialize' do
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when there are entries' do
|
|
|
|
let(:entries) { [build(:wiki_page)] }
|
|
|
|
let(:directory) { described_class.new('/path_up_to/dir', entries) }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'sets the slug attribute' do
|
|
|
|
expect(directory.slug).to eq('/path_up_to/dir')
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'sets the entries attribute' do
|
|
|
|
expect(directory.entries).to eq(entries)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
context 'when there are no entries' do
|
2017-09-10 17:25:29 +05:30
|
|
|
let(:directory) { described_class.new('/path_up_to/dir') }
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
it 'sets the slug attribute' do
|
|
|
|
expect(directory.slug).to eq('/path_up_to/dir')
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
it 'sets the entries attribute to an empty array' do
|
|
|
|
expect(directory.entries).to eq([])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
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
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe '#to_partial_path' do
|
|
|
|
it 'returns the relative path to the partial to be used' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(directory.to_partial_path).to eq('../shared/wikis/wiki_directory')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|