BigW Consortium Gitlab

renderer_spec.rb 1.92 KB
Newer Older
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
require 'spec_helper'

describe Banzai::Renderer do
  def expect_render(project = :project)
    expected_context = { project: project }
    expect(renderer).to receive(:cacheless_render) { :html }.with(:markdown, expected_context)
  end

  def expect_cache_update
    expect(object).to receive(:update_column).with("field_html", :html)
  end

  def fake_object(*features)
    markdown = :markdown if features.include?(:markdown)
    html = :html if features.include?(:html)

    object = double(
      "object",
      banzai_render_context: { project: :project },
      field: markdown,
      field_html: html
    )

    allow(object).to receive(:markdown_cache_field_for).with(:field).and_return("field_html")
    allow(object).to receive(:new_record?).and_return(features.include?(:new))
    allow(object).to receive(:destroyed?).and_return(features.include?(:destroyed))

    object
  end

  describe "#render_field" do
    let(:renderer) { Banzai::Renderer }
    let(:subject) { renderer.render_field(object, :field) }

    context "with an empty cache" do
      let(:object) { fake_object(:markdown) }
      it "caches and returns the result" do
        expect_render
        expect_cache_update
        expect(subject).to eq(:html)
      end
    end

    context "with a filled cache" do
      let(:object) { fake_object(:markdown, :html) }

      it "uses the cache" do
        expect_render.never
        expect_cache_update.never
        should eq(:html)
      end
    end

    context "new object" do
      let(:object) { fake_object(:new, :markdown) }

      it "doesn't cache the result" do
        expect_render
        expect_cache_update.never
        expect(subject).to eq(:html)
      end
    end

    context "destroyed object" do
      let(:object) { fake_object(:destroyed, :markdown) }

      it "doesn't cache the result" do
        expect_render
        expect_cache_update.never
        expect(subject).to eq(:html)
      end
    end
  end
end