|
3 | 3 | require 'spec_helper' |
4 | 4 |
|
5 | 5 | describe Intercom::Lib::FlatStore do |
6 | | - it 'raises if you try to set or merge in nested hash structures' do |
| 6 | + it 'raises if you try to set arrays but allows hashes' do |
7 | 7 | data = Intercom::Lib::FlatStore.new |
8 | 8 | _(proc { data['thing'] = [1] }).must_raise ArgumentError |
9 | | - _(proc { data['thing'] = { 1 => 2 } }).must_raise ArgumentError |
10 | | - _(proc { Intercom::Lib::FlatStore.new(1 => { 2 => 3 }) }).must_raise ArgumentError |
| 9 | + |
| 10 | + data['thing'] = { 'key' => 'value' } |
| 11 | + _(data['thing']).must_equal({ 'key' => 'value' }) |
| 12 | + |
| 13 | + flat_store = Intercom::Lib::FlatStore.new('custom_object' => { 'type' => 'Order.list', 'instances' => [{'id' => '123'}] }) |
| 14 | + _(flat_store['custom_object']).must_equal({ 'type' => 'Order.list', 'instances' => [{'id' => '123'}] }) |
11 | 15 | end |
12 | 16 |
|
13 | 17 | it 'raises if you try to use a non string key' do |
14 | 18 | data = Intercom::Lib::FlatStore.new |
15 | | - _(proc { data[1] = 'something' }).must_raise ArgumentError |
| 19 | + _(proc { data[1] = 'something' }).must_raise ArgumentErrorca |
16 | 20 | end |
17 | 21 |
|
18 | 22 | it 'sets and merges valid entries' do |
|
28 | 32 | _(data['b']).must_equal 2 |
29 | 33 | _(data[:b]).must_equal 2 |
30 | 34 | end |
| 35 | + |
| 36 | + describe '#to_submittable_hash' do |
| 37 | + it 'filters out all hash values' do |
| 38 | + data = Intercom::Lib::FlatStore.new( |
| 39 | + 'regular_attr' => 'value', |
| 40 | + 'number_attr' => 42, |
| 41 | + 'custom_object' => { |
| 42 | + 'type' => 'Order.list', |
| 43 | + 'instances' => [ |
| 44 | + { 'id' => '31', 'external_id' => 'ext_123' } |
| 45 | + ] |
| 46 | + }, |
| 47 | + 'regular_hash' => { 'key' => 'value' }, |
| 48 | + 'metadata' => { 'source' => 'api', 'version' => 2 } |
| 49 | + ) |
| 50 | + |
| 51 | + submittable = data.to_submittable_hash |
| 52 | + |
| 53 | + _(submittable['regular_attr']).must_equal 'value' |
| 54 | + _(submittable['number_attr']).must_equal 42 |
| 55 | + |
| 56 | + _(submittable.key?('custom_object')).must_equal false |
| 57 | + _(submittable.key?('regular_hash')).must_equal false |
| 58 | + _(submittable.key?('metadata')).must_equal false |
| 59 | + end |
| 60 | + end |
31 | 61 | end |
0 commit comments