application_helper_spec.rb 4.68 KB
Newer Older
1
# coding: utf-8
2 3 4
require 'spec_helper'

describe ApplicationHelper do
5
  describe 'current_controller?' do
6
    it 'returns true when controller matches argument' do
7 8 9
      stub_controller_name('foo')

      expect(helper.current_controller?(:foo)).to eq true
10 11
    end

12
    it 'returns false when controller does not match argument' do
13 14 15
      stub_controller_name('foo')

      expect(helper.current_controller?(:bar)).to eq false
16
    end
17

18 19 20 21 22 23 24 25 26
    it 'takes any number of arguments' do
      stub_controller_name('foo')

      expect(helper.current_controller?(:baz, :bar)).to eq false
      expect(helper.current_controller?(:baz, :bar, :foo)).to eq true
    end

    def stub_controller_name(value)
      allow(helper.controller).to receive(:controller_name).and_return(value)
27
    end
28 29
  end

Robert Speicher's avatar
Robert Speicher committed
30
  describe 'current_action?' do
31 32 33 34
    it 'returns true when action matches' do
      stub_action_name('foo')

      expect(helper.current_action?(:foo)).to eq true
Robert Speicher's avatar
Robert Speicher committed
35 36
    end

37 38 39 40
    it 'returns false when action does not match' do
      stub_action_name('foo')

      expect(helper.current_action?(:bar)).to eq false
Robert Speicher's avatar
Robert Speicher committed
41 42
    end

43 44 45 46 47
    it 'takes any number of arguments' do
      stub_action_name('foo')

      expect(helper.current_action?(:baz, :bar)).to eq false
      expect(helper.current_action?(:baz, :bar, :foo)).to eq true
Robert Speicher's avatar
Robert Speicher committed
48 49
    end

50 51
    def stub_action_name(value)
      allow(helper).to receive(:action_name).and_return(value)
Robert Speicher's avatar
Robert Speicher committed
52 53
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54

55
  describe 'simple_sanitize' do
56 57
    let(:a_tag) { '<a href="#">Foo</a>' }

58
    it 'allows the a tag' do
59
      expect(helper.simple_sanitize(a_tag)).to eq(a_tag)
60 61
    end

62
    it 'allows the span tag' do
63
      input = '<span class="foo">Bar</span>'
64
      expect(helper.simple_sanitize(input)).to eq(input)
65 66
    end

67
    it 'disallows other tags' do
68
      input = "<strike><b>#{a_tag}</b></strike>"
69
      expect(helper.simple_sanitize(input)).to eq(a_tag)
70 71
    end
  end
72

73 74
  describe 'time_ago_with_tooltip' do
    def element(*arguments)
75
      Time.zone = 'UTC'
Phil Hughes's avatar
Phil Hughes committed
76 77
      @time = Time.zone.parse('2015-07-02 08:23')
      element = helper.time_ago_with_tooltip(@time, *arguments)
78 79 80 81 82 83 84 85 86

      Nokogiri::HTML::DocumentFragment.parse(element).first_element_child
    end

    it 'returns a time element' do
      expect(element.name).to eq 'time'
    end

    it 'includes the date string' do
Phil Hughes's avatar
Phil Hughes committed
87
      expect(element.text).to eq @time.strftime("%b %d, %Y")
88 89 90
    end

    it 'has a datetime attribute' do
91
      expect(element.attr('datetime')).to eq '2015-07-02T08:23:00Z'
92 93 94
    end

    it 'has a formatted title attribute' do
95
      expect(element.attr('title')).to eq 'Jul 2, 2015 8:23am'
96 97 98
    end

    it 'includes a default js-timeago class' do
99
      expect(element.attr('class')).to eq 'js-timeago'
100 101 102
    end

    it 'accepts a custom html_class' do
103 104
      expect(element(html_class: 'custom_class').attr('class'))
        .to eq 'js-timeago custom_class'
105 106 107 108 109 110
    end

    it 'accepts a custom tooltip placement' do
      expect(element(placement: 'bottom').attr('data-placement')).to eq 'bottom'
    end

111 112 113
    it 'converts to Time' do
      expect { helper.time_ago_with_tooltip(Date.today) }.not_to raise_error
    end
114

115
    it 'add class for the short format' do
116 117 118 119
      timeago_element = element(short_format: 'short')
      expect(timeago_element.attr('class')).to eq 'js-short-timeago'
      expect(timeago_element.next_element).to eq nil
    end
120 121
  end

Semyon Pupkov's avatar
Semyon Pupkov committed
122 123 124 125
  describe '#active_when' do
    it { expect(helper.active_when(true)).to eq('active') }
    it { expect(helper.active_when(false)).to eq(nil) }
  end
126 127 128 129

  describe '#support_url' do
    context 'when alternate support url is specified' do
      let(:alternate_url) { 'http://company.example.com/getting-help' }
130 131

      before do
132
        stub_application_setting(help_page_support_url: alternate_url)
133
      end
134 135 136 137 138 139 140 141 142 143 144 145

      it 'returns the alternate support url' do
        expect(helper.support_url).to eq(alternate_url)
      end
    end

    context 'when alternate support url is not specified' do
      it 'builds the support url from the promo_url' do
        expect(helper.support_url).to eq(helper.promo_url + '/getting-help/')
      end
    end
  end
146 147 148

  describe '#locale_path' do
    it 'returns the locale path with an `_`' do
149 150 151
      Gitlab::I18n.with_locale('pt-BR') do
        expect(helper.locale_path).to include('assets/locale/pt_BR/app')
      end
152 153
    end
  end
154 155 156 157 158 159

  describe '#autocomplete_data_sources' do
    let(:project) { create(:project) }
    let(:noteable_type) { Issue }
    it 'returns paths for autocomplete_sources_controller' do
      sources = helper.autocomplete_data_sources(project, noteable_type)
160
      expect(sources.keys).to match_array([:members, :issues, :mergeRequests, :labels, :milestones, :commands])
161 162 163 164 165
      sources.keys.each do |key|
        expect(sources[key]).not_to be_nil
      end
    end
  end
166
end