aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/renderer/render_spec.rb
blob: 30d22256313e86acac5da8c152da5d3aa7c0e530 (plain) (blame)
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
require 'spec_helper'

describe Zero::Renderer, '#render' do
  subject { Zero::Renderer.new(template_path, type_map) }
  let(:template_path) { 'spec/fixtures/templates' }
  let(:type_map) {{
    'html' => ['text/html', 'text/xml', '*/*'],
    'json' => ['application/json', 'plain/text']
  }}
  let(:html_types) { ['text/html'] }
  let(:json_types) { ['application/json'] }
  let(:foo_types)  { ['foo/bar', 'bar/foo'] }
  let(:binding) { SpecTemplateContext.new('foo') }

  before :each do
    subject.read_template_path!
  end

  it 'returns a tilt template' do
    subject.render('index', html_types, binding).should be_kind_of(String)
  end

  it 'renders html content' do
    subject.render('index', html_types, binding).should match('success')
  end

  it 'returns a tilt template for different types' do
    subject.render('index', json_types, binding).should be_kind_of(String)
  end

  it 'renders json content' do
    subject.render('index', json_types, binding).should match("{text: 'success'}")
  end

  it 'returns an ArgumentError, if given template does not exist' do
    expect {
      subject.render('foobar', html_types, binding)
    }.to raise_error(ArgumentError, "No template found for 'foobar'!")
  end

  it 'returns an ArgumentError, if no template fits types' do
    expect {
      subject.render('index', foo_types, binding)
    }.to raise_error(
      ArgumentError,
      "No template found for any of this types #{foo_types.join ', '}!"
    )
  end

  it 'uses the context' do
    subject.render('context', html_types, binding).should match('foo')

  end
end