File: breadcrumbs_spec.rb

package info (click to toggle)
ruby-simple-navigation 4.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 488 kB
  • ctags: 255
  • sloc: ruby: 3,693; makefile: 3
file content (118 lines) | stat: -rw-r--r-- 3,760 bytes parent folder | download
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module SimpleNavigation
  module Renderer
    describe Breadcrumbs do
      let!(:navigation) { setup_navigation('nav_id', 'nav_class') }

      let(:item) { nil }
      let(:options) {{ level: :all }}
      let(:output) { HTML::Document.new(raw_output).root }
      let(:raw_output) { renderer.render(navigation) }
      let(:renderer) { Breadcrumbs.new(options) }

      before { select_an_item(navigation[item]) if item }

      describe '#render' do
        it "renders a 'div' tag for the navigation" do
          expect(output).to have_css('div')
        end

        it "sets the right html id on the rendered 'div' tag" do
          expect(output).to have_css('div#nav_id')
        end

        it "sets the right html classes on the rendered 'div' tag" do
          expect(output).to have_css('div.nav_class')
        end

        context 'when no item is selected' do
          it "doesn't render any 'a' tag in the 'div' tag" do
            expect(output).not_to have_css('div a')
          end
        end

        context 'when an item is selected' do
          let(:item) { :invoices }

          it "renders the selected 'a' tag" do
            expect(output).to have_css('div a')
          end

          it "remders the 'a' tag without any html id" do
            expect(output).not_to have_css('div a[id]')
          end

          it "renders the 'a' tag without any html class" do
            expect(output).not_to have_css('div a[class]')
          end

          context 'and the :allow_classes_and_ids option is true' do
            let(:options) {{ level: :all, allow_classes_and_ids: true }}

            it "renders the 'a' tag with the selected class" do
              expect(output).to have_css('div a.selected')
            end

            context "and the item hasn't any id explicitly set" do
              it "renders the 'a' tag without any html id" do
                expect(output).not_to have_css('div a[id]')
              end
            end

            context 'and the item has an explicitly set id' do
              let(:item) { :users }

              it "renders the 'a' tag with an html id" do
                expect(output).to have_css('div a#breadcrumb_users_link_id')
              end
            end
          end
        end

        context 'and the :prefix option is set' do
          let(:options) {{ prefix: 'You are here: ' }}

          context 'and there are no items to render' do
            let(:item) { nil }

            it "doesn't render the prefix before the breadcrumbs" do
              expect(raw_output).not_to match(/^<div.+>You are here: /)
            end
          end

          context 'and there are items to render' do
            let(:item) { :invoices }

            it 'renders the prefix before the breadcrumbs' do
              expect(raw_output).to match(/^<div.+>You are here: /)
            end
          end
        end

        context 'when a sub navigation item is selected' do
          before do
            allow(navigation[:invoices]).to receive_messages(selected?: true)

            allow(navigation[:invoices].sub_navigation[:unpaid]).to \
              receive_messages(selected?: true, selected_by_condition?: true)
          end

          it 'renders all items as links' do
            expect(output).to have_css('div a', 2)
          end

          context 'when the :static_leaf option is true' do
            let(:options) {{ level: :all, static_leaf: true }}

            it 'renders the items as links' do
              expect(output).to have_css('div a')
            end

            it 'renders the last item as simple text' do
              expect(output).to have_css('div span')
            end
          end
        end
      end
    end
  end
end