cirandas.net

ref: master

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

class ActsAsHavingSettingsTest < ActiveSupport::TestCase

  # using Block class as a sample user of the module
  class TestClass < Block
    settings_items :flag, type: :boolean
    settings_items :flag_disabled_by_default, type: :boolean, default: false
    # to test that 'name' will be symbolized (see below)
    settings_items 'name', type: :string, default: N_('ENGLISH TEXT')
    attr_accessible :flag, :name, :flag_disabled_by_default
  end

  should 'store settings in a hash' do
    block = Block.new

    assert_kind_of Hash, block.settings
    block.save!

    assert_kind_of Hash, Block.find(block.id).settings
  end

  should 'be able to declare settings items' do
    block_class = Class.new(Block)

    block = block_class.new
    refute block.respond_to?(:limit)
    refute block.respond_to?(:limit=)

    block_class.settings_items :limit, type: :integer

    assert_respond_to block, :limit
    assert_respond_to block, :limit=

    assert_nil block.limit
    block.limit = 10
    assert_equal 10, block.limit

    assert_equal({ limit: 10}, block.settings)
  end

  should 'properly save the settings' do
    # RecentDocumentsBlock declares an actual setting called limit
    profile = create_user('testuser').person
    block = RecentDocumentsBlock.new
    block.box = profile.boxes.first
    block.limit = 15
    block.save!
    assert_equal 15, Block.find(block.id).limit
  end

  should 'be able to specify default values' do
    block_class = Class.new(Block)
    block_class.settings_items :some_setting, default: 10
    assert_equal 10, block_class.new.some_setting
  end

  should 'be able to set boolean attributes to false with a default of true' do
    obj = TestClass.new
    obj.flag = false
    assert_equal false, obj.flag
  end

  should 'return false by default when the default is false' do
    assert_equal false, TestClass.new.flag_disabled_by_default
  end

  should 'translate default values' do
    TestClass.any_instance.expects(:gettext).with('ENGLISH TEXT').returns("TRANSLATED")
    assert_equal 'TRANSLATED', TestClass.new.name
  end

  should 'be able to specify type of atrributes (boolean)' do
    obj = TestClass.new
    obj.flag = 'true'
    assert_equal true, obj.flag
  end

  should 'have keys as symbols' do
    obj = TestClass.new
    assert obj.settings.all?{ |k,v| k.is_a? Symbol }
  end

  should 'setting_changed be true if a setting passed as parameter was changed' do
    obj = TestClass.new
    obj.flag = true
    assert obj.setting_changed? 'flag'
  end

  should 'setting_changed be false if a setting passed as parameter was not changed' do
    obj = TestClass.new
    refute obj.setting_changed?('flag')
  end

  should 'setting_changed be false if a setting passed as parameter was changed with the same value' do
    obj = TestClass.new
    obj.flag = true
    obj.save
    obj.flag = true
    refute obj.setting_changed?('flag')
  end

  should 'setting_changed be false if a setting passed as parameter was not changed but another setting is changed' do
    obj = TestClass.new(name: 'some name')
    obj.save
    obj.name = 'antoher nme'
    refute obj.setting_changed?('flag')
  end

  should 'setting_changed be true for all changed fields' do
    obj = TestClass.new(name: 'some name', flag: false)
    obj.save
    obj.name = 'another nme'
    obj.flag = true
    assert obj.setting_changed?('flag')
    assert obj.setting_changed?('name')
  end
end