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

describe AttachmentToHTML do
    include AttachmentToHTML

    let(:attachment) { FactoryGirl.build(:body_text) }

    describe :to_html do

        it 'sends the attachment to the correct adapter for conversion' do
            AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, {}).and_call_original
            to_html(attachment)
        end

        it 'renders the attachment as html' do
            adapter = AttachmentToHTML::Adapters::Text.new(attachment)
            expected = AttachmentToHTML::View.new(adapter).render
            to_html(attachment).should == expected
        end

        it 'passes content injections options when rendering the result' do
            html = to_html(attachment, :content_for => { :body_prefix => '<p>prefix</p>' })
            html.should include('<p>prefix</p>')
        end

        it 'accepts a hash of options to pass to the adapter' do
            options = { :wrapper => 'wrap' }
            AttachmentToHTML::Adapters::Text.should_receive(:new).with(attachment, options).and_call_original
            to_html(attachment, options)
        end

        it 'converts an attachment that has an adapter, fails to convert, but has a google viewer' do
            attachment = FactoryGirl.build(:pdf_attachment)
            AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false)
            AttachmentToHTML::Adapters::PDF.should_receive(:new).with(attachment, {}).and_call_original
            AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}).and_call_original
            to_html(attachment)
        end

        it 'converts an attachment that doesnt have an adapter, but has a google viewer' do
            attachment = FactoryGirl.build(:body_text, :content_type => 'application/vnd.ms-word')
            AttachmentToHTML::Adapters::GoogleDocsViewer.should_receive(:new).with(attachment, {}).and_call_original
            to_html(attachment)
        end

        it 'converts an attachment that has no adapter or google viewer' do
            attachment = FactoryGirl.build(:body_text, :content_type => 'application/json')
            AttachmentToHTML::Adapters::CouldNotConvert.should_receive(:new).with(attachment, {}).and_call_original
            to_html(attachment)
        end

        describe 'when wrapping the content' do

            it 'uses a the default wrapper' do
                attachment = FactoryGirl.build(:pdf_attachment)
                to_html(attachment).should include(%Q(<div id="wrapper">))
            end

            it 'uses a custom wrapper for GoogleDocsViewer attachments' do
                attachment = FactoryGirl.build(:pdf_attachment)
                # TODO: Add a document that will always render in a
                # GoogleDocsViewer for testing
                AttachmentToHTML::Adapters::PDF.any_instance.stub(:success?).and_return(false)
                to_html(attachment).should include(%Q(<div id="wrapper_google_embed">))
            end

        end

    end

end