2019-12-21 20:55:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe TabHelper do
|
2014-09-02 18:07:02 +05:30
|
|
|
include ApplicationHelper
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
describe 'gl_tabs_nav' do
|
|
|
|
it 'creates a tabs navigation' do
|
2023-03-04 22:38:38 +05:30
|
|
|
expect(helper.gl_tabs_nav).to match(%r{<ul class="nav gl-tabs-nav"></ul>})
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'captures block output' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tabs_nav { "block content" }).to match(/block content/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds custom class' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tabs_nav(class: 'my-class' )).to match(/class=".*my-class.*"/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'gl_tab_link_to' do
|
|
|
|
before do
|
2022-01-26 12:08:38 +05:30
|
|
|
allow(helper).to receive(:current_page?).and_return(false)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a tab' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url')).to eq('<li class="nav-item"><a class="nav-link gl-tab-nav-item" href="/url">Link</a></li>')
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a tab with block output' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('/url') { 'block content' }).to match(/block content/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
it 'creates a tab with custom classes for enclosing list item without content block provided' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url', { tab_class: 'my-class' })).to match(/<li class=".*my-class.*"/)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a tab with custom classes for enclosing list item with content block provided' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('/url', { tab_class: 'my-class' }) { 'Link' }).to match(/<li class=".*my-class.*"/)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a tab with custom classes for anchor element' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url', { class: 'my-class' })).to match(/<a class=".*my-class.*"/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an active tab with item_active = true' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url', { item_active: true })).to match(/<a class=".*active gl-tab-nav-item-active.*"/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'when on the active page' do
|
|
|
|
before do
|
2022-01-26 12:08:38 +05:30
|
|
|
allow(helper).to receive(:current_page?).and_return(true)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an active tab' do
|
2022-04-04 11:22:00 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url')).to match(/<a class=".*active gl-tab-nav-item-active.*"/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates an inactive tab with item_active = false' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_link_to('Link', '/url', { item_active: false })).not_to match(/<a class=".*active.*"/)
|
2021-11-18 22:05:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
describe 'nav_link' do
|
2021-04-29 21:17:54 +05:30
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
before do
|
2015-04-26 12:48:37 +05:30
|
|
|
allow(controller).to receive(:controller_name).and_return('foo')
|
2022-01-26 12:08:38 +05:30
|
|
|
allow(helper).to receive(:action_name).and_return('foo')
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
context 'with the content of the li' do
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'captures block output' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.nav_link { "Testing Blocks" }).to match(/Testing Blocks/)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
it 'passes extra html options to the list element' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.nav_link(action: :foo, html_options: { class: 'home' })).to match(/<li class="home active">/)
|
|
|
|
expect(helper.nav_link(html_options: { class: 'active' })).to match(/<li class="active">/)
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
where(:controller_param, :action_param, :path_param, :active) do
|
|
|
|
nil | nil | nil | false
|
|
|
|
:foo | nil | nil | true
|
|
|
|
:bar | nil | nil | false
|
|
|
|
:bar | :foo | nil | false
|
|
|
|
:foo | :bar | nil | false
|
|
|
|
:foo | :foo | nil | true
|
|
|
|
:bar | nil | 'foo#foo' | true
|
|
|
|
:bar | nil | ['foo#foo', 'bar#bar'] | true
|
|
|
|
:bar | :bar | ['foo#foo', 'bar#bar'] | true
|
|
|
|
:foo | nil | 'bar#foo' | true
|
|
|
|
:bar | nil | 'bar#foo' | false
|
|
|
|
:foo | [:foo, :bar] | 'bar#foo' | true
|
|
|
|
:bar | :bar | 'foo#foo' | true
|
|
|
|
:foo | :foo | 'bar#foo' | true
|
|
|
|
:bar | :foo | 'bar#foo' | false
|
|
|
|
:foo | :bar | 'bar#foo' | false
|
|
|
|
[:foo, :bar] | nil | nil | true
|
|
|
|
[:foo, :bar] | nil | 'bar#foo' | true
|
|
|
|
[:foo, :bar] | :foo | 'bar#foo' | true
|
|
|
|
nil | :foo | nil | true
|
|
|
|
nil | :bar | nil | false
|
|
|
|
nil | nil | 'foo#bar' | false
|
|
|
|
nil | nil | 'foo#foo' | true
|
|
|
|
nil | :bar | ['foo#foo', 'bar#bar'] | true
|
|
|
|
nil | :bar | 'foo#foo' | true
|
|
|
|
nil | :foo | 'bar#foo' | true
|
|
|
|
nil | [:foo, :bar] | nil | true
|
|
|
|
nil | [:foo, :bar] | 'bar#foo' | true
|
|
|
|
nil | :bar | 'bar#foo' | false
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
with_them do
|
|
|
|
specify do
|
2022-01-26 12:08:38 +05:30
|
|
|
result = helper.nav_link(controller: controller_param, action: action_param, path: path_param)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(result.include?('active')).to eq(active)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
where(:page, :excluded_page, :active) do
|
|
|
|
nil | nil | false
|
|
|
|
'_some_page_' | nil | true
|
|
|
|
'_some_page_' | '_excluded_page_' | true
|
|
|
|
'_some_page_' | '_some_page_' | false
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
|
|
|
specify do
|
|
|
|
allow(helper).to receive(:route_matches_pages?).and_return(page.present?, page == excluded_page)
|
|
|
|
|
|
|
|
result = helper.nav_link(page: page, exclude_page: excluded_page)
|
|
|
|
|
|
|
|
expect(result.include?('active')).to eq(active)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
context 'with namespace in path notation' do
|
|
|
|
before do
|
|
|
|
allow(controller).to receive(:controller_path).and_return('bar/foo')
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
where(:controller_param, :action_param, :path_param, :active) do
|
|
|
|
'foo/foo' | nil | nil | false
|
|
|
|
'bar/foo' | nil | nil | true
|
|
|
|
'foo/foo' | :foo | nil | false
|
|
|
|
'bar/foo' | :bar | nil | false
|
|
|
|
'bar/foo' | :foo | nil | true
|
|
|
|
nil | nil | 'foo/foo#foo' | false
|
|
|
|
nil | nil | 'bar/foo#foo' | true
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
with_them do
|
|
|
|
specify do
|
2022-01-26 12:08:38 +05:30
|
|
|
result = helper.nav_link(controller: controller_param, action: action_param, path: path_param)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(result.include?('active')).to eq(active)
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
describe 'gl_tab_counter_badge' do
|
|
|
|
it 'creates a tab counter badge' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_counter_badge(1)).to eq(
|
|
|
|
'<span class="gl-badge badge badge-pill badge-muted sm gl-tab-counter-badge">1</span>'
|
|
|
|
)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with extra classes' do
|
|
|
|
it 'creates a tab counter badge with the correct class attribute' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_counter_badge(1, { class: 'js-test' })).to eq(
|
|
|
|
'<span class="gl-badge badge badge-pill badge-muted sm gl-tab-counter-badge js-test">1</span>'
|
|
|
|
)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with data attributes' do
|
|
|
|
it 'creates a tab counter badge with the data attributes' do
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(helper.gl_tab_counter_badge(1, { data: { some_attribute: 'foo' } })).to eq(
|
2022-10-11 01:57:18 +05:30
|
|
|
'<span class="gl-badge badge badge-pill badge-muted sm gl-tab-counter-badge" data-some-attribute="foo">1</span>'
|
2022-01-26 12:08:38 +05:30
|
|
|
)
|
2021-12-11 22:18:48 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|