summaryrefslogtreecommitdiff
path: root/spec/lib/sidebars/menu_spec.rb
blob: 7c4d74aecc862b33e5a43ad0c6a06082cca610be (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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Sidebars::Menu, feature_category: :navigation do
  let(:menu) { described_class.new(context) }
  let(:context) { Sidebars::Context.new(current_user: nil, container: nil) }

  let(:nil_menu_item) { Sidebars::NilMenuItem.new(item_id: :foo) }

  describe '#all_active_routes' do
    it 'gathers all active routes of items and the current menu' do
      menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: { path: %w(bar test) }))
      menu.add_item(Sidebars::MenuItem.new(title: 'foo2', link: 'foo2', active_routes: { controller: 'fooc' }))
      menu.add_item(Sidebars::MenuItem.new(title: 'foo3', link: 'foo3', active_routes: { controller: 'barc' }))
      menu.add_item(nil_menu_item)

      allow(menu).to receive(:active_routes).and_return({ path: 'foo' })

      expect(menu).to receive(:renderable_items).and_call_original
      expect(menu.all_active_routes).to eq({ path: %w(foo bar test), controller: %w(fooc barc) })
    end
  end

  describe '#serialize_for_super_sidebar' do
    it 'returns a tree-like structure of itself and all menu items' do
      menu.add_item(Sidebars::MenuItem.new(title: 'Is active', link: 'foo2', active_routes: { controller: 'fooc' }))
      menu.add_item(Sidebars::MenuItem.new(title: 'Not active', link: 'foo3', active_routes: { controller: 'barc' }))
      menu.add_item(nil_menu_item)

      allow(context).to receive(:route_is_active).and_return(->(x) { x[:controller] == 'fooc' })
      allow(menu).to receive(:title).and_return('Title')
      allow(menu).to receive(:active_routes).and_return({ path: 'foo' })

      expect(menu.serialize_for_super_sidebar).to eq(
        {
          title: "Title",
          icon: nil,
          link: "foo2",
          is_active: true,
          items: [
            {
              title: "Is active",
              icon: nil,
              link: "foo2",
              is_active: true
            },
            {
              title: "Not active",
              icon: nil,
              link: "foo3",
              is_active: false
            }
          ]
        })
    end
  end

  describe '#serialize_as_menu_item_args' do
    it 'returns hash of title, link, active_routes, container_html_options' do
      allow(menu).to receive(:title).and_return('Title')
      allow(menu).to receive(:active_routes).and_return({ path: 'foo' })
      allow(menu).to receive(:container_html_options).and_return({ class: 'foo' })
      allow(menu).to receive(:link).and_return('/link')

      expect(menu.serialize_as_menu_item_args).to eq({
        title: 'Title',
        link: '/link',
        active_routes: { path: 'foo' },
        container_html_options: { class: 'foo' }
      })
    end
  end

  describe '#render?' do
    context 'when the menus has no items' do
      it 'returns false' do
        expect(menu.render?).to be false
      end

      context 'when menu has a partial' do
        it 'returns true' do
          allow(menu).to receive(:menu_partial).and_return('foo')

          expect(menu.render?).to be true
        end
      end
    end

    context 'when the menu has items' do
      it 'returns true' do
        menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}))

        expect(menu.render?).to be true
      end

      context 'when menu items are NilMenuItem' do
        it 'returns false' do
          menu.add_item(nil_menu_item)

          expect(menu.render?).to be false
        end
      end
    end
  end

  describe '#has_items?' do
    it 'returns true when there are regular menu items' do
      menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}))

      expect(menu.has_items?).to be true
    end

    it 'returns true when there are nil menu items' do
      menu.add_item(nil_menu_item)

      expect(menu.has_items?).to be true
    end
  end

  describe '#has_renderable_items?' do
    it 'returns true when there are regular menu items' do
      menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}))

      expect(menu.has_renderable_items?).to be true
    end

    it 'returns false when there are nil menu items' do
      menu.add_item(nil_menu_item)

      expect(menu.has_renderable_items?).to be false
    end

    it 'returns true when there are both regular and nil menu items' do
      menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}))
      menu.add_item(nil_menu_item)

      expect(menu.has_renderable_items?).to be true
    end
  end

  describe '#renderable_items' do
    it 'returns only regular menu items' do
      item = Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {})
      menu.add_item(item)
      menu.add_item(nil_menu_item)

      expect(menu.renderable_items.size).to eq 1
      expect(menu.renderable_items.first).to eq item
    end
  end

  describe '#insert_element_before' do
    let(:item1) { Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}, item_id: :foo1) }
    let(:item2) { Sidebars::MenuItem.new(title: 'foo2', link: 'foo2', active_routes: {}, item_id: :foo2) }
    let(:item3) { Sidebars::MenuItem.new(title: 'foo3', link: 'foo3', active_routes: {}, item_id: :foo3) }
    let(:list) { [item1, item2] }

    it 'adds element before the specific element class' do
      menu.insert_element_before(list, :foo2, item3)

      expect(list).to eq [item1, item3, item2]
    end

    it 'does not add nil elements' do
      menu.insert_element_before(list, :foo2, nil)

      expect(list).to eq [item1, item2]
    end

    context 'when reference element does not exist' do
      it 'adds the element to the top of the list' do
        menu.insert_element_before(list, :non_existent, item3)

        expect(list).to eq [item3, item1, item2]
      end
    end
  end

  describe '#insert_element_after' do
    let(:item1) { Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}, item_id: :foo1) }
    let(:item2) { Sidebars::MenuItem.new(title: 'foo2', link: 'foo2', active_routes: {}, item_id: :foo2) }
    let(:item3) { Sidebars::MenuItem.new(title: 'foo3', link: 'foo3', active_routes: {}, item_id: :foo3) }
    let(:list) { [item1, item2] }

    it 'adds element after the specific element class' do
      menu.insert_element_after(list, :foo1, item3)

      expect(list).to eq [item1, item3, item2]
    end

    it 'does not add nil elements' do
      menu.insert_element_after(list, :foo1, nil)

      expect(list).to eq [item1, item2]
    end

    context 'when reference element does not exist' do
      it 'adds the element to the end of the list' do
        menu.insert_element_after(list, :non_existent, item3)

        expect(list).to eq [item1, item2, item3]
      end
    end
  end

  describe '#remove_element' do
    let(:item1) { Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: {}, item_id: :foo1) }
    let(:item2) { Sidebars::MenuItem.new(title: 'foo2', link: 'foo2', active_routes: {}, item_id: :foo2) }
    let(:item3) { Sidebars::MenuItem.new(title: 'foo3', link: 'foo3', active_routes: {}, item_id: :foo3) }
    let(:list) { [item1, item2, item3] }

    it 'removes specific element' do
      menu.remove_element(list, :foo2)

      expect(list).to eq [item1, item3]
    end

    it 'does not remove nil elements' do
      menu.remove_element(list, nil)

      expect(list).to eq [item1, item2, item3]
    end
  end

  describe '#container_html_options' do
    before do
      allow(menu).to receive(:title).and_return('Foo Menu')
    end

    context 'when menu can be rendered' do
      before do
        allow(menu).to receive(:render?).and_return(true)
      end

      context 'when menu has renderable items' do
        before do
          menu.add_item(Sidebars::MenuItem.new(title: 'foo1', link: 'foo1', active_routes: { path: 'bar' }))
        end

        it 'contains the special class' do
          expect(menu.container_html_options[:class]).to eq 'has-sub-items'
        end

        context 'when menu already has other classes' do
          it 'appends special class' do
            allow(menu).to receive(:extra_container_html_options).and_return(class: 'foo')

            expect(menu.container_html_options[:class]).to eq 'foo has-sub-items'
          end
        end
      end

      context 'when menu does not have renderable items' do
        it 'does not contain the special class' do
          expect(menu.container_html_options[:class]).to be_nil
        end
      end
    end

    context 'when menu cannot be rendered' do
      before do
        allow(menu).to receive(:render?).and_return(false)
      end

      it 'does not contain special class' do
        expect(menu.container_html_options[:class]).to be_nil
      end
    end
  end

  describe '#link' do
    let(:foo_path) { '/foo_path' }

    let(:foo_menu) do
      ::Sidebars::MenuItem.new(
        title: 'foo',
        link: foo_path,
        active_routes: {},
        item_id: :foo
      )
    end

    it 'returns first visible menu item link' do
      menu.add_item(foo_menu)

      expect(menu.link).to eq foo_path
    end

    it 'returns nil if there are no visible menu items' do
      expect(menu.link).to be_nil
    end
  end
end