summaryrefslogtreecommitdiff
path: root/spec/helpers/breadcrumbs_helper_spec.rb
blob: 8e2a684656b5ed684607ce2ec9972d7fbbd42000 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe BreadcrumbsHelper do
  describe '#push_to_schema_breadcrumb' do
    let(:element_name) { 'BreadCrumbElement' }
    let(:link) { 'http://test.host/foo' }
    let(:breadcrumb_list) { helper.instance_variable_get(:@schema_breadcrumb_list) }

    subject { helper.push_to_schema_breadcrumb(element_name, link) }

    it 'enqueue element name, link and position' do
      subject

      aggregate_failures do
        expect(breadcrumb_list[0]['name']).to eq element_name
        expect(breadcrumb_list[0]['item']).to eq link
        expect(breadcrumb_list[0]['position']).to eq(1)
      end
    end

    context 'when link is relative' do
      let(:link) { '/foo' }

      it 'converts the url into absolute' do
        subject

        expect(breadcrumb_list[0]['item']).to eq "http://test.host#{link}"
      end
    end

    describe 'when link is invalid' do
      let(:link) { 'invalid://foo[]' }

      it 'returns the current url' do
        subject

        expect(breadcrumb_list[0]['item']).to eq 'http://test.host'
      end
    end

    describe 'when link is nil' do
      let(:link) { nil }

      it 'returns the current url' do
        subject

        expect(breadcrumb_list[0]['item']).to eq 'http://test.host'
      end
    end
  end

  describe '#schema_breadcrumb_json' do
    let(:elements) do
      [
        %w(element1 http://test.host/link1),
        %w(element2 http://test.host/link2)
      ]
    end

    subject { helper.schema_breadcrumb_json }

    it 'returns the breadcrumb schema in json format' do
      enqueue_breadcrumb_elements

      expected_result = {
        '@context' => 'https://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => [
          {
            '@type' => 'ListItem',
            'position' => 1,
            'name' => elements[0][0],
            'item' => elements[0][1]
          },
          {
            '@type' => 'ListItem',
            'position' => 2,
            'name' => elements[1][0],
            'item' => elements[1][1]
          }
        ]
      }.to_json

      expect(subject).to eq expected_result
    end

    context 'when extra breadcrumb element is added' do
      let(:extra_elements) do
        [
          %w(extra_element1 http://test.host/extra_link1),
          %w(extra_element2 http://test.host/extra_link2)
        ]
      end

      it 'include the extra elements before the last element' do
        enqueue_breadcrumb_elements

        extra_elements.each do |el|
          add_to_breadcrumbs(el[0], el[1])
        end

        expected_result = {
          '@context' => 'https://schema.org',
          '@type' => 'BreadcrumbList',
          'itemListElement' => [
            {
              '@type' => 'ListItem',
              'position' => 1,
              'name' => elements[0][0],
              'item' => elements[0][1]
            },
            {
              '@type' => 'ListItem',
              'position' => 2,
              'name' => extra_elements[0][0],
              'item' => extra_elements[0][1]
            },
            {
              '@type' => 'ListItem',
              'position' => 3,
              'name' => extra_elements[1][0],
              'item' => extra_elements[1][1]
            },
            {
              '@type' => 'ListItem',
              'position' => 4,
              'name' => elements[1][0],
              'item' => elements[1][1]
            }
          ]
        }.to_json

        expect(subject).to eq expected_result
      end
    end

    def enqueue_breadcrumb_elements
      elements.each do |el|
        helper.push_to_schema_breadcrumb(el[0], el[1])
      end
    end
  end
end