File: links_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 (88 lines) | stat: -rw-r--r-- 2,807 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
module SimpleNavigation
  module Renderer
    describe Links do
      describe '#render' 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) { Links.new(options) }

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

        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

        it "renders an 'a' tag for each item" do
          expect(output).to have_css('a', 3)
        end

        it "renders the 'a' tags with the corresponding item's :html_options" do
          expect(output).to have_css('a[style=float:right]')
        end

        context 'when an item has a specified id' do
          it "renders the 'a' tags with the specified id" do
            expect(output).to have_css('a#users_id')
          end
        end

        context 'when an item has no specified id' do
          it "uses a default id by stringifying the item's key" do
            expect(output).to have_css('a#invoices')
          end
        end

        context 'when no item is selected' do
          it "renders items without the 'selected' class" do
            expect(output).not_to have_css('a.selected')
          end
        end

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

          it "renders the selected item with the 'selected' class" do
            expect(output).to have_css('a#invoices.selected')
          end
        end

        context "when the :join_with option is set" do
          let(:options) {{ level: :all, join_with: ' | ' }}

          it 'separates the items with the specified separator' do
            expect(raw_output.scan(' | ').size).to eq 3
          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 the main parent as selected' do
            expect(output).to have_css('a#invoices.selected')
          end

          it "doesn't render the nested item's link" do
            expect(output).not_to have_css('a#unpaid')
          end
        end
      end
    end
  end
end