twitter_sync_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. RSpec.describe TwitterSync do
  3. describe '.preferences_cleanup' do
  4. describe 'sanitizing Twitter preferences' do
  5. context 'when given as a bare hash' do
  6. it 'automatically adds empty hashes at :geo and :place' do
  7. expect(described_class.preferences_cleanup({}))
  8. .to eq({ geo: {}, place: {} })
  9. end
  10. it 'does not modify values at :mention_ids' do
  11. expect(described_class.preferences_cleanup({ mention_ids: [1_234_567_890] }))
  12. .to include({ mention_ids: [1_234_567_890] })
  13. end
  14. it 'converts geo: instance_of(Twitter::NullOjbect) to empty hash' do
  15. expect(described_class.preferences_cleanup({ geo: Twitter::NullObject.new }))
  16. .to include(geo: {})
  17. end
  18. it 'converts geo: instance_of(Twitter::Geo.new) to matching hash' do
  19. expect(described_class.preferences_cleanup({ geo: Twitter::Geo.new(coordinates: [1, 1]) }))
  20. .to include(geo: { coordinates: [1, 1] })
  21. end
  22. it 'converts place: instance_of(Twitter::NullOjbect) to empty hash' do
  23. expect(described_class.preferences_cleanup({ place: Twitter::NullObject.new }))
  24. .to include(place: {})
  25. end
  26. it 'converts place: instance_of(Twitter::Place.new) to matching hash' do
  27. place_data = { country: 'da', name: 'do', woeid: 1, id: 1 }
  28. expect(described_class.preferences_cleanup({ place: Twitter::Place.new(place_data) }))
  29. .to include(place: place_data)
  30. end
  31. end
  32. context 'when given nested in an article preferences hash' do
  33. it 'automatically adds empty hashes at :geo and :place' do
  34. expect(described_class.preferences_cleanup({ twitter: {} }))
  35. .to eq(twitter: { geo: {}, place: {} })
  36. end
  37. it 'does not modify values at :mention_ids' do
  38. expect(described_class.preferences_cleanup({ twitter: { mention_ids: [1_234_567_890] } }))
  39. .to include(twitter: hash_including(mention_ids: [1_234_567_890]))
  40. end
  41. it 'converts geo: instance_of(Twitter::NullOjbect) to empty hash' do
  42. expect(described_class.preferences_cleanup({ twitter: { geo: Twitter::NullObject.new } }))
  43. .to include(twitter: hash_including(geo: {}))
  44. end
  45. it 'converts geo: instance_of(Twitter::Geo.new) to matching hash' do
  46. expect(described_class.preferences_cleanup({ twitter: { geo: Twitter::Geo.new(coordinates: [1, 1]) } }))
  47. .to include(twitter: hash_including(geo: { coordinates: [1, 1] }))
  48. end
  49. it 'converts place: instance_of(Twitter::NullOjbect) to empty hash' do
  50. expect(described_class.preferences_cleanup({ twitter: { place: Twitter::NullObject.new } }))
  51. .to include(twitter: hash_including(place: {}))
  52. end
  53. it 'converts place: instance_of(Twitter::Place.new) to matching hash' do
  54. place_data = { country: 'da', name: 'do', woeid: 1, id: 1 }
  55. expect(described_class.preferences_cleanup({ twitter: { place: Twitter::Place.new(place_data) } }))
  56. .to include(twitter: hash_including(place: place_data))
  57. end
  58. end
  59. end
  60. end
  61. end