2021-03-18 10:41:32 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-05-04 13:49:53 +10:00
|
|
|
RSpec.describe CacheConcern do
|
2021-03-18 10:41:32 +11:00
|
|
|
controller(ApplicationController) do
|
|
|
|
include CacheConcern
|
|
|
|
|
|
|
|
def empty_array
|
|
|
|
render plain: cache_collection([], Status).size
|
|
|
|
end
|
|
|
|
|
|
|
|
def empty_relation
|
|
|
|
render plain: cache_collection(Status.none, Status).size
|
|
|
|
end
|
2023-08-18 00:11:48 +10:00
|
|
|
|
|
|
|
def account_statuses_favourites
|
|
|
|
render plain: cache_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count)
|
|
|
|
end
|
2021-03-18 10:41:32 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
routes.draw do
|
2023-08-18 00:11:48 +10:00
|
|
|
get 'empty_array' => 'anonymous#empty_array'
|
|
|
|
get 'empty_relation' => 'anonymous#empty_relation'
|
|
|
|
get 'account_statuses_favourites' => 'anonymous#account_statuses_favourites'
|
2021-03-18 10:41:32 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#cache_collection' do
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when given an empty array' do
|
2021-03-18 10:41:32 +11:00
|
|
|
it 'returns an empty array' do
|
|
|
|
get :empty_array
|
|
|
|
expect(response.body).to eq '0'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when given an empty relation' do
|
2021-03-18 10:41:32 +11:00
|
|
|
it 'returns an empty array' do
|
|
|
|
get :empty_relation
|
|
|
|
expect(response.body).to eq '0'
|
|
|
|
end
|
|
|
|
end
|
2023-08-18 00:11:48 +10:00
|
|
|
|
|
|
|
context 'when given a collection of statuses' do
|
|
|
|
let!(:account) { Fabricate(:account) }
|
|
|
|
let!(:status) { Fabricate(:status, account: account) }
|
|
|
|
|
|
|
|
it 'correctly updates with new interactions' do
|
|
|
|
get :account_statuses_favourites, params: { id: account.id }
|
|
|
|
expect(response.body).to eq '[0]'
|
|
|
|
|
|
|
|
FavouriteService.new.call(account, status)
|
|
|
|
|
|
|
|
get :account_statuses_favourites, params: { id: account.id }
|
|
|
|
expect(response.body).to eq '[1]'
|
|
|
|
end
|
|
|
|
end
|
2021-03-18 10:41:32 +11:00
|
|
|
end
|
|
|
|
end
|