~cytrogen/masto-fe

ref: 8eb09466aae67235009253ef6e9e9d3722a58d03 masto-fe/spec/controllers/concerns/cache_concern_spec.rb -rw-r--r-- 1.6 KiB
8eb09466 — Claire Merge pull request #2380 from ClearlyClaire/glitch-soc/merge-upstream 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe CacheConcern do
  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

    def account_statuses_favourites
      render plain: cache_collection(Status.where(account_id: params[:id]), Status).map(&:favourites_count)
    end
  end

  before do
    routes.draw do
      get 'empty_array' => 'anonymous#empty_array'
      get 'empty_relation' => 'anonymous#empty_relation'
      get 'account_statuses_favourites' => 'anonymous#account_statuses_favourites'
    end
  end

  describe '#cache_collection' do
    context 'when given an empty array' do
      it 'returns an empty array' do
        get :empty_array
        expect(response.body).to eq '0'
      end
    end

    context 'when given an empty relation' do
      it 'returns an empty array' do
        get :empty_relation
        expect(response.body).to eq '0'
      end
    end

    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
  end
end