aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/attachment_to_html/view_spec.rb
blob: 50179b0f7053c2e3485d0b9cf4d1f3d19812afed (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 File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')

describe AttachmentToHTML::View do
    
    let(:adapter) do
        OpenStruct.new(
            :body => '<p>hello</p>',
            :title => 'An attachment.txt',
            :success? => true)
    end
    
    let(:view) { AttachmentToHTML::View.new(adapter) }
    
    let(:default_template) do
        "#{ Rails.root }/lib/attachment_to_html/template.html.erb"
    end
    
    describe '.template' do
        
        after(:each) do
            AttachmentToHTML::View.template = nil
        end
        
        it 'has a default template location' do
            AttachmentToHTML::View.template.should == default_template
        end
    
    end
    
    describe '.template=' do
        
        after(:each) do
            AttachmentToHTML::View.template = nil
        end
        
        it 'allows a global template to be set' do
            template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
            AttachmentToHTML::View.template = template
            AttachmentToHTML::View.template.should == template
        end
    
    end
    
    describe :new do
        
        it 'sets the title on initialization' do
            view.title.should == adapter.title
        end
        
        it 'sets the body on initialization' do
            view.body.should == adapter.body
        end
        
        it 'sets a default template if none is specified' do
            view.template.should == default_template
        end
        
        it 'allows a template to be set through an option' do
            template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
            opts = { :template => template }
            view = AttachmentToHTML::View.new(adapter, opts)
            view.template.should == template
        end
    
    end
    
    describe :title= do
        
        it 'allows the title to be set' do
            view.title = adapter.title
            view.title.should == adapter.title
        end
    
    end
    
    describe :body= do
        
        it 'allows the body to be set' do
            view.body = adapter.body
            view.body.should == adapter.body
        end
    
    end
    
    describe :template= do
        
        it 'allows the template to be set' do
            template = file_fixture_name('attachment_to_html/alternative_template.html.erb')
            view.template = template
            view.template.should == template
        end
    
    end
    
    describe :wrapper do
        
        it 'is set to wrapper by default' do
            view.wrapper.should == 'wrapper'
        end
    
    end
    
    describe :wrapper= do
        
        it 'allows the wrapper div to be customised' do
            view.wrapper = 'wrap'
            view.wrapper.should == 'wrap'
        end
    
    end
    
    # Need to remove all whitespace to assert equal because
    # ERB adds additional indentation after ERB tags
    describe :render do
        
        it 'renders the contents in to the template' do
            view.wrapper = 'wrap'
            expected = <<-HTML
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>An attachment.txt</title>
</head>
<body>
  <div id="wrap">
    <div id="view-html-content">
      <p>hello</p>
    </div>
  </div>
</body>
</html>
            HTML
            
            view.render.gsub(/\s+/, '').should == expected.gsub(/\s+/, '')
        end
        
        it 'allows the dynamic injection of content' do
            content = %Q(<meta charset="utf-8">)
            result = view.render { inject_content(:head_suffix) { content } }
            result.should include(content)
        end
    
    end

end