File: item_adapter_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 (188 lines) | stat: -rw-r--r-- 5,393 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
module SimpleNavigation
  describe ItemAdapter do
    let(:item_adapter) { ItemAdapter.new(item) }

    context 'when item is an object' do
      let(:item) { double(:item, key: 'key', name: 'name', url: 'url') }

      shared_examples 'delegating to item' do |meth|
        it "delegates #{meth} to item" do
          expect(item).to receive(meth)
          item_adapter.public_send(meth)
        end
      end

      it_behaves_like 'delegating to item', :key
      it_behaves_like 'delegating to item', :url
      it_behaves_like 'delegating to item', :name

      describe '#initialize' do
        it 'sets the item' do
          expect(item_adapter.item).to be item
        end
      end

      describe '#options' do
        context 'when item responds to options' do
          let(:options) { double(:options) }

          before { allow(item).to receive_messages(options: options) }

          it "returns the item's options" do
            expect(item_adapter.options).to be options
          end
        end

        context 'item does not respond to options' do
          it 'returns an empty hash' do
            expect(item_adapter.options).to eq({})
          end
        end
      end

      describe '#items' do
        context 'when item responds to items' do
          context 'and items is nil' do
            before { allow(item).to receive_messages(items: nil) }

            it 'returns nil' do
              expect(item_adapter.items).to be_nil
            end
          end

          context 'when items is not nil' do
            context 'and items is empty' do
              before { allow(item).to receive_messages(items: []) }

              it 'returns nil' do
                expect(item_adapter.items).to be_nil
              end
            end

            context 'and items is not empty' do
              let(:items) { [true] }

              before { allow(item).to receive_messages(items: items) }

              it 'returns the items' do
                expect(item_adapter.items).to eq items
              end
            end
          end
        end

        context "when item doesn't respond to items" do
          it 'returns nil' do
            expect(item_adapter.items).to be_nil
          end
        end
      end

      describe '#to_simple_navigation_item' do
        let(:container) { double(:container) }

        before { allow(item).to receive_messages(items: [], options: {}) }

        it 'creates an Item' do
          expect(Item).to receive(:new)
                          .with(container, 'key', 'name', 'url', {})
          item_adapter.to_simple_navigation_item(container)
        end
      end
    end

    context 'when item is a kind of hash' do
      class ModifiedHash < Hash; end

      let(:item) { ModifiedHash[key: 'key', url: 'url', name: 'name'] }

      shared_examples 'delegating to item' do |meth|
        it "delegates #{meth} to item" do
          expect(item_adapter.item).to receive(meth)
          item_adapter.public_send(meth)
        end
      end

      it_behaves_like 'delegating to item', :key
      it_behaves_like 'delegating to item', :url
      it_behaves_like 'delegating to item', :name

      describe '#initialize' do
        it 'sets the item' do
          expect(item_adapter.item).not_to be_nil
        end

        it 'converts the item into an object' do
          expect(item_adapter.item).to respond_to(:url)
        end
      end

      describe '#options' do
        context 'when item responds to options' do
          before { item[:options] = { my: :options } }

          it "returns the item's options" do
            expect(item_adapter.options).to eq({ my: :options })
          end
        end

        context 'when item does not respond to options' do
          it 'returns an empty hash' do
            expect(item_adapter.options).to eq({})
          end
        end
      end

      describe '#items' do
        context 'when item responds to items' do
          context 'and items is nil' do
            before { item[:items] = nil }

            it 'returns nil' do
              expect(item_adapter.items).to be_nil
            end
          end

          context 'when items is not nil' do
            context 'and items is empty' do
              it 'returns nil' do
                expect(item_adapter.items).to be_nil
              end
            end

            context 'and items is not empty' do
              before { item[:items] = ['not', 'empty'] }

              it 'returns the items' do
                expect(item_adapter.items).to eq ['not', 'empty']
              end
            end
          end
        end

        context 'when item does not respond to items' do
          it 'returns nil' do
            expect(item_adapter.items).to be_nil
          end
        end
      end

      describe '#to_simple_navigation_item' do
        let(:container) { double(:container) }

        before { item.merge(options: {}) }

        it 'passes the right arguments to Item' do
          expect(Item).to receive(:new)
                          .with(container, 'key', 'name', 'url', {})
          item_adapter.to_simple_navigation_item(container)
        end

        it 'creates an Item' do
          created_item = item_adapter.to_simple_navigation_item(container)
          expect(created_item).to be_an(Item)
        end
      end
    end
  end
end