cirandas.net

ref: master

test/unit/file_presenter_test.rb


 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
require_relative "../test_helper"

class FilePresenterTest < ActiveSupport::TestCase

  should 'notify about deprecated method UploadedFile.icon_name' do
    profile = fast_create(Profile)
    file = UploadedFile.create!(
             :profile => profile,
             :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
           )
    assert_raise NoMethodError do
      UploadedFile.icon_name file
    end
    ENV.stubs('[]').with('RAILS_ENV').returns('other')
    Rails.logger.expects(:warn) # must warn on any other RAILS_ENV
    stubs(:puts)
    UploadedFile.icon_name file
  end

  should 'notify about deprecated method UploadedFile#to_html' do
    profile = fast_create(Profile)
    file = UploadedFile.create!(
             :profile => profile,
             :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
           )
    assert_raise NoMethodError do
      file.to_html
    end
    ENV.stubs('[]').with('RAILS_ENV').returns('other')
    Rails.logger.expects(:warn) # must warn on any other RAILS_ENV
    stubs(:puts)
    file.to_html
  end

  should 'return a thumbnail as icon for images ' do
    f = UploadedFile.new
    f.stubs(:image?).returns(true)
    p = FilePresenter.for f
    p.expects(:public_filename).with(:icon).returns('/path/to/file.xyz')
    assert_equal '/path/to/file.xyz', p.icon_name
  end

  should 'not crach when accepts? method receives a pure article' do
    assert_nothing_raised do
      FilePresenter.for Article.new
    end
  end

  should 'not crach when accepts? method receives a non-sense object' do
    assert_nothing_raised do
      FilePresenter.for nil
    end
    assert_nothing_raised do
      FilePresenter.for({:key => 'value'})
    end
    assert_nothing_raised do
      FilePresenter.for 'a string'
    end
  end

  should 'pass kind_of? to the encapsulated file' do
    f = FilePresenter.for(UploadedFile.new)
    assert f.kind_of?(UploadedFile)
  end

  should 'not crash with uploaded_file short description without content_type' do
    f = FilePresenter.for(UploadedFile.new)
    assert_nothing_raised do
      f.short_description
    end
  end

  should 'show unknown type when file doesn\'t have a content_type' do
    f = FilePresenter.for(UploadedFile.new)
    assert_match /Unknown/, f.short_description
  end
end