Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object_renderer_spec.rb « banzai « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bcdb95250caee162af0ffc151c907ffa832b6add (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
require 'spec_helper'

describe Banzai::ObjectRenderer do
  let(:project) { create(:empty_project) }
  let(:user) { project.owner }

  describe '#render' do
    it 'renders and redacts an Array of objects' do
      renderer = described_class.new(project, user)
      object = double(:object, note: 'hello', note_html: nil)

      expect(renderer).to receive(:render_objects).with([object], :note).
        and_call_original

      expect(renderer).to receive(:redact_documents).
        with(an_instance_of(Array)).
        and_call_original

      expect(object).to receive(:note_html=).with('<p>hello</p>')
      expect(object).to receive(:user_visible_reference_count=).with(0)

      renderer.render([object], :note)
    end
  end

  describe '#render_objects' do
    it 'renders an Array of objects' do
      object = double(:object, note: 'hello')

      renderer = described_class.new(project, user)

      expect(renderer).to receive(:render_attributes).with([object], :note).
        and_call_original

      rendered = renderer.render_objects([object], :note)

      expect(rendered).to be_an_instance_of(Array)
      expect(rendered[0]).to be_an_instance_of(Nokogiri::HTML::DocumentFragment)
    end
  end

  describe '#redact_documents' do
    it 'redacts a set of documents and returns them as an Array of Hashes' do
      doc = Nokogiri::HTML.fragment('<p>hello</p>')
      renderer = described_class.new(project, user)

      expect_any_instance_of(Banzai::Redactor).to receive(:redact).
        with([doc]).
        and_call_original

      redacted = renderer.redact_documents([doc])

      expect(redacted.count).to eq(1)
      expect(redacted.first[:visible_reference_count]).to eq(0)
      expect(redacted.first[:document].to_html).to eq('<p>hello</p>')
    end
  end

  describe '#context_for' do
    let(:object) { double(:object, note: 'hello') }
    let(:renderer) { described_class.new(project, user) }

    it 'returns a Hash' do
      expect(renderer.context_for(object, :note)).to be_an_instance_of(Hash)
    end

    it 'includes the cache key' do
      context = renderer.context_for(object, :note)

      expect(context[:cache_key]).to eq([object, :note])
    end

    context 'when the object responds to "author"' do
      it 'includes the author in the context' do
        expect(object).to receive(:author).and_return('Alice')

        context = renderer.context_for(object, :note)

        expect(context[:author]).to eq('Alice')
      end
    end

    context 'when the object does not respond to "author"' do
      it 'does not include the author in the context' do
        context = renderer.context_for(object, :note)

        expect(context.key?(:author)).to eq(false)
      end
    end
  end

  describe '#render_attributes' do
    it 'renders the attribute of a list of objects' do
      objects = [double(:doc, note: 'hello'), double(:doc, note: 'bye')]
      renderer = described_class.new(project, user, pipeline: :note)

      expect(Banzai).to receive(:cache_collection_render).
        with([
          { text: 'hello', context: renderer.context_for(objects[0], :note) },
          { text: 'bye', context: renderer.context_for(objects[1], :note) }
        ]).
        and_call_original

      docs = renderer.render_attributes(objects, :note)

      expect(docs[0]).to be_an_instance_of(Nokogiri::HTML::DocumentFragment)
      expect(docs[0].to_html).to eq('<p>hello</p>')

      expect(docs[1]).to be_an_instance_of(Nokogiri::HTML::DocumentFragment)
      expect(docs[1].to_html).to eq('<p>bye</p>')
    end

    it 'returns when no objects to render' do
      objects = []
      renderer = described_class.new(project, user, pipeline: :note)

      expect(Banzai).to receive(:cache_collection_render).
        with([]).
        and_call_original

      expect(renderer.render_attributes(objects, :note)).to eq([])
    end
  end

  describe '#base_context' do
    let(:context) do
      described_class.new(project, user, pipeline: :note).base_context
    end

    it 'returns a Hash' do
      expect(context).to be_an_instance_of(Hash)
    end

    it 'includes the custom attributes' do
      expect(context[:pipeline]).to eq(:note)
    end

    it 'includes the current user' do
      expect(context[:current_user]).to eq(user)
    end

    it 'includes the current project' do
      expect(context[:project]).to eq(project)
    end
  end
end