Add coverage for title/limit validations in List model (#31869)
				
					
				
			This commit is contained in:
		
							parent
							
								
									a496aeabcb
								
							
						
					
					
						commit
						17c57c46e7
					
				@ -20,21 +20,23 @@ class List < ApplicationRecord
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  enum :replies_policy, { list: 0, followed: 1, none: 2 }, prefix: :show
 | 
					  enum :replies_policy, { list: 0, followed: 1, none: 2 }, prefix: :show
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  belongs_to :account, optional: true
 | 
					  belongs_to :account
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  has_many :list_accounts, inverse_of: :list, dependent: :destroy
 | 
					  has_many :list_accounts, inverse_of: :list, dependent: :destroy
 | 
				
			||||||
  has_many :accounts, through: :list_accounts
 | 
					  has_many :accounts, through: :list_accounts
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  validates :title, presence: true
 | 
					  validates :title, presence: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  validates_each :account_id, on: :create do |record, _attr, value|
 | 
					  validate :validate_account_lists_limit, on: :create
 | 
				
			||||||
    record.errors.add(:base, I18n.t('lists.errors.limit')) if List.where(account_id: value).count >= PER_ACCOUNT_LIMIT
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  before_destroy :clean_feed_manager
 | 
					  before_destroy :clean_feed_manager
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def validate_account_lists_limit
 | 
				
			||||||
 | 
					    errors.add(:base, I18n.t('lists.errors.limit')) if account.lists.count >= PER_ACCOUNT_LIMIT
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def clean_feed_manager
 | 
					  def clean_feed_manager
 | 
				
			||||||
    FeedManager.instance.clean_feeds!(:list, [id])
 | 
					    FeedManager.instance.clean_feeds!(:list, [id])
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										27
									
								
								spec/models/list_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								spec/models/list_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					require 'rails_helper'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RSpec.describe List do
 | 
				
			||||||
 | 
					  describe 'Validations' do
 | 
				
			||||||
 | 
					    subject { Fabricate.build :list }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it { is_expected.to validate_presence_of(:title) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context 'when account has hit max list limit' do
 | 
				
			||||||
 | 
					      let(:account) { Fabricate :account }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      before { stub_const 'List::PER_ACCOUNT_LIMIT', 0 }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      context 'when creating a new list' do
 | 
				
			||||||
 | 
					        it { is_expected.to_not allow_value(account).for(:account).against(:base).with_message(I18n.t('lists.errors.limit')) }
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      context 'when updating an existing list' do
 | 
				
			||||||
 | 
					        before { subject.save(validate: false) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        it { is_expected.to allow_value(account).for(:account).against(:base) }
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user