File: json_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 (63 lines) | stat: -rw-r--r-- 2,056 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
module SimpleNavigation
  module Renderer
    describe Json do
      describe '#render' do
        let!(:navigation) { setup_navigation('nav_id', 'nav_class') }

        let(:item) { :invoices }
        let(:options) {{ level: :all }}
        let(:output) { renderer.render(navigation) }
        let(:parsed_output) { JSON.parse(output) }
        let(:renderer) { Json.new(options) }

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

        context 'when an item is selected' do

          it 'renders the selected page' do
            invoices_item = parsed_output.find { |item| item['name'] == 'Invoices' }
            expect(invoices_item).to include('selected' => true)
          end
        end

        # FIXME: not sure if :as_hash returning an array makes sense...
        context 'when the :as_hash option is true' do
          let(:options) {{ level: :all, as_hash: true }}

          it 'returns a hash' do
            expect(output).to be_an Array
          end

          it 'renders the selected page' do
            invoices_item = output.find { |item| item[:name] == 'Invoices' }
            expect(invoices_item).to include(selected: true)
          end
        end

        context 'when a sub navigation item is selected' do
          let(:invoices_item) do
            parsed_output.find { |item| item['name'] == 'Invoices' }
          end
          let(:unpaid_item) do
            invoices_item['items'].find { |item| item['name'] == 'Unpaid' }
          end

          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 'marks all the parent items as selected' do
            expect(invoices_item).to include('selected' => true)
          end

          it 'marks the item as selected' do
            expect(unpaid_item).to include('selected' => true)
          end
        end
      end
    end
  end
end