|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +RSpec.describe GraphqlDevise::MountMethod::OptionSanitizer do |
| 4 | + subject(:clean_options) { described_class.new(options, supported_options).call! } |
| 5 | + |
| 6 | + describe '#call!' do |
| 7 | + let(:supported_options) do |
| 8 | + { |
| 9 | + my_string: GraphqlDevise::MountMethod::OptionSanitizers::StringChecker.new('default string'), |
| 10 | + hash_multiple: GraphqlDevise::MountMethod::OptionSanitizers::HashChecker.new([String, Numeric]), |
| 11 | + array: GraphqlDevise::MountMethod::OptionSanitizers::ArrayChecker.new(Symbol), |
| 12 | + hash_single: GraphqlDevise::MountMethod::OptionSanitizers::HashChecker.new(Float), |
| 13 | + my_class: GraphqlDevise::MountMethod::OptionSanitizers::ClassChecker.new(Numeric) |
| 14 | + } |
| 15 | + end |
| 16 | + |
| 17 | + context 'when all options are provided and correct' do |
| 18 | + let(:options) do |
| 19 | + { |
| 20 | + my_string: 'non default', |
| 21 | + hash_multiple: { first: String, second: Float, third: Float }, |
| 22 | + array: [:one, :two, :three], |
| 23 | + hash_single: { first: Float, second: Float }, |
| 24 | + my_class: Float |
| 25 | + } |
| 26 | + end |
| 27 | + |
| 28 | + it 'returns a struct with clean options' do |
| 29 | + expect( |
| 30 | + my_string: clean_options.my_string, |
| 31 | + hash_multiple: clean_options.hash_multiple, |
| 32 | + array: clean_options.array, |
| 33 | + hash_single: clean_options.hash_single, |
| 34 | + my_class: clean_options.my_class |
| 35 | + ).to match( |
| 36 | + my_string: 'non default', |
| 37 | + hash_multiple: { first: String, second: Float, third: Float }, |
| 38 | + array: [:one, :two, :three], |
| 39 | + hash_single: { first: Float, second: Float }, |
| 40 | + my_class: Float |
| 41 | + ) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context 'when some options are provided but all correct' do |
| 46 | + let(:options) do |
| 47 | + { |
| 48 | + hash_multiple: { first: String, second: Float, third: Float }, |
| 49 | + array: [:one, :two, :three], |
| 50 | + my_class: Float |
| 51 | + } |
| 52 | + end |
| 53 | + |
| 54 | + it 'returns a struct with clean options and default values' do |
| 55 | + expect( |
| 56 | + my_string: clean_options.my_string, |
| 57 | + hash_multiple: clean_options.hash_multiple, |
| 58 | + array: clean_options.array, |
| 59 | + hash_single: clean_options.hash_single, |
| 60 | + my_class: clean_options.my_class |
| 61 | + ).to match( |
| 62 | + my_string: 'default string', |
| 63 | + hash_multiple: { first: String, second: Float, third: Float }, |
| 64 | + array: [:one, :two, :three], |
| 65 | + hash_single: {}, |
| 66 | + my_class: Float |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context 'when an option provided is invalid' do |
| 72 | + let(:options) do |
| 73 | + { |
| 74 | + hash_multiple: { first: String, second: Float, third: Float }, |
| 75 | + array: ['not symbol 1', 'not symbol 2'], |
| 76 | + my_class: Float |
| 77 | + } |
| 78 | + end |
| 79 | + |
| 80 | + it 'raises an error' do |
| 81 | + expect { clean_options }.to raise_error(GraphqlDevise::InvalidMountOptionsError, '`array` option has invalid elements. Symbol expected.') |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | +end |
0 commit comments