summaryrefslogtreecommitdiff
path: root/spec/support/shared_examples/features/wiki/user_views_asciidoc_page_with_includes_shared_examples.rb
blob: 3b2fda4e05b2ef1e781708ad13bb4c96568b19f3 (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
# frozen_string_literal: true

RSpec.shared_examples 'User views AsciiDoc page with includes' do
  let_it_be(:wiki_content_selector) { '[data-qa-selector=wiki_page_content]' }
  let!(:included_wiki_page) { create_wiki_page('included_page', content: 'Content from the included page')}
  let!(:wiki_page) { create_wiki_page('home', content: "Content from the main page.\ninclude::included_page.asciidoc[]") }

  def create_wiki_page(title, content:)
    attrs = {
      title: title,
      content: content,
      format: :asciidoc
    }

    create(:wiki_page, wiki: wiki, **attrs)
  end

  before do
    sign_in(user)
  end

  context 'when the file being included exists', :js do
    it 'includes the file contents' do
      visit(wiki_page_path(wiki, wiki_page))

      page.within(:css, wiki_content_selector) do
        expect(page).to have_content('Content from the main page. Content from the included page')
      end
    end

    context 'when there are multiple versions of the wiki pages' do
      before do
        # rubocop:disable Rails/SaveBang
        included_wiki_page.update(message: 'updated included file', content: 'Updated content from the included page')
        wiki_page.update(message: 'updated wiki page', content: "Updated content from the main page.\ninclude::included_page.asciidoc[]")
        # rubocop:enable Rails/SaveBang
      end

      let(:latest_version_id) { wiki_page.versions.first.id }
      let(:oldest_version_id) { wiki_page.versions.last.id }

      context 'viewing the latest version' do
        it 'includes the latest content' do
          visit(wiki_page_path(wiki, wiki_page, version_id: latest_version_id))

          page.within(:css, wiki_content_selector) do
            expect(page).to have_content('Updated content from the main page. Updated content from the included page')
          end
        end
      end

      context 'viewing the original version' do
        it 'includes the content from the original version' do
          visit(wiki_page_path(wiki, wiki_page, version_id: oldest_version_id))

          page.within(:css, wiki_content_selector) do
            expect(page).to have_content('Content from the main page. Content from the included page')
          end
        end
      end
    end
  end

  context 'when the file being included does not exist', :js do
    before do
      included_wiki_page.delete
    end

    it 'outputs an error' do
      visit(wiki_page_path(wiki, wiki_page))

      page.within(:css, wiki_content_selector) do
        expect(page).to have_content('Content from the main page. [ERROR: include::included_page.asciidoc[] - unresolved directive]')
      end
    end
  end
end