1
0

preloading_concern_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PreloadingConcern do
  4. controller(ApplicationController) do
  5. include PreloadingConcern
  6. def empty_array
  7. render plain: preload_collection([], Status).size
  8. end
  9. def empty_relation
  10. render plain: preload_collection(Status.none, Status).size
  11. end
  12. def account_statuses_favourites
  13. render plain: preload_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count)
  14. end
  15. end
  16. before do
  17. routes.draw do
  18. get 'empty_array' => 'anonymous#empty_array'
  19. get 'empty_relation' => 'anonymous#empty_relation'
  20. get 'account_statuses_favourites' => 'anonymous#account_statuses_favourites'
  21. end
  22. end
  23. describe '#preload_collection' do
  24. context 'when given an empty array' do
  25. it 'returns an empty array' do
  26. get :empty_array
  27. expect(response.body).to eq '0'
  28. end
  29. end
  30. context 'when given an empty relation' do
  31. it 'returns an empty array' do
  32. get :empty_relation
  33. expect(response.body).to eq '0'
  34. end
  35. end
  36. context 'when given a collection of statuses' do
  37. let!(:account) { Fabricate(:account) }
  38. let!(:status) { Fabricate(:status, account: account) }
  39. it 'correctly updates with new interactions' do
  40. get :account_statuses_favourites, params: { id: account.id }
  41. expect(response.body).to eq '[0]'
  42. FavouriteService.new.call(account, status)
  43. get :account_statuses_favourites, params: { id: account.id }
  44. expect(response.body).to eq '[1]'
  45. end
  46. end
  47. end
  48. end