Sha256: 099b7a47b2a55210e781a4d94c9ab6d4c1476e9d1b2c4cf3e0eee516ad5b7a81

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

require 'test_helper'
require 'sweet_params'

describe SweetParams do
  let(:params) { ActionController::Parameters.new(scope: 'recent', filter: { scope: 'recent' }, empty: '') }

  describe '#has?' do
    it 'should respond to method' do
      params.must_respond_to :has?
    end

    it 'should be true if parameter is present' do
      params.has?(:scope, in: [:recent, :new]).must_equal true
    end

    it 'should be false if parameter is missing' do
      params.has?(:empty).must_equal false
      params.has?(:not_here).must_equal false
    end

    it 'should handle multi-dimensional params hash' do
      params.has?([:filter, :scope]).must_equal true
    end

    it 'should handle multi-dimensional params hash' do
      params.has?([:filter, :not_here]).must_equal false
    end

    it 'should use single value as whitelist' do
      params.has?(:scope, in: :recent).must_equal true
    end

    it 'should use array as whitelist' do
      params.has?(:scope, in: [:recent, :new]).must_equal true
    end

    it 'should not allow not whitelisted params' do
      params.has?(:scope, in: [:hot, :new]).must_equal false
    end
  end

  describe '#validate' do
    it 'should respond to method' do
      params.must_respond_to :validate
    end

    it 'should allow whitelisted param' do
      params.validate(:scope, in: [:hot, :recent]).must_equal 'recent'
    end

    it 'should return nil for not whitelisted param' do
      params.validate(:scope, in: [:hot, :new]).must_equal nil
    end
  end

  describe '#validate_to_sym' do
    it 'should respond to method' do
      params.must_respond_to :validate_to_sym
    end

    it 'should symbolize whitelisted param' do
      params.validate_to_sym(:scope, in: [:hot, :recent]).must_equal :recent
    end

    it 'should return nil for not whitelisted param' do
      params.validate_to_sym(:scope, in: [:hot, :new]).must_equal nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sweet_params-0.1.1 test/sweet_params_test.rb
sweet_params-0.1.0 test/sweet_params_test.rb