1# frozen_string_literal: true
2
3require 'spec_helper'
4
5RSpec.describe ChatName do
6  let_it_be(:chat_name) { create(:chat_name) }
7
8  subject { chat_name }
9
10  it { is_expected.to belong_to(:integration) }
11  it { is_expected.to belong_to(:user) }
12
13  it { is_expected.to validate_presence_of(:user) }
14  it { is_expected.to validate_presence_of(:integration) }
15  it { is_expected.to validate_presence_of(:team_id) }
16  it { is_expected.to validate_presence_of(:chat_id) }
17
18  it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:service_id) }
19  it { is_expected.to validate_uniqueness_of(:chat_id).scoped_to(:service_id, :team_id) }
20
21  it 'is removed when the project is deleted' do
22    expect { subject.reload.integration.project.delete }.to change { ChatName.count }.by(-1)
23
24    expect(ChatName.where(id: subject.id)).not_to exist
25  end
26
27  describe '#update_last_used_at', :clean_gitlab_redis_shared_state do
28    it 'updates the last_used_at timestamp' do
29      expect(subject.last_used_at).to be_nil
30
31      subject.update_last_used_at
32
33      expect(subject.last_used_at).to be_present
34    end
35
36    it 'does not update last_used_at if it was recently updated' do
37      subject.update_last_used_at
38
39      time = subject.last_used_at
40
41      subject.update_last_used_at
42
43      expect(subject.last_used_at).to eq(time)
44    end
45  end
46
47  it_behaves_like 'it has loose foreign keys' do
48    let(:factory_name) { :chat_name }
49  end
50end
51