563d2d1e3d
Bumps [rubocop-rspec](https://github.com/rubocop/rubocop-rspec) from 2.8.0 to 2.9.0. - [Release notes](https://github.com/rubocop/rubocop-rspec/releases) - [Changelog](https://github.com/rubocop/rubocop-rspec/blob/master/CHANGELOG.md) - [Commits](https://github.com/rubocop/rubocop-rspec/compare/v2.8.0...v2.9.0)
84 lines
2.2 KiB
Ruby
84 lines
2.2 KiB
Ruby
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
|
|
|
require 'rails_helper'
|
|
|
|
RSpec.describe Chat::Agent, type: :model do
|
|
|
|
describe '.state' do
|
|
|
|
let(:user) { create(:agent) }
|
|
|
|
context 'when no record exists for User' do
|
|
|
|
it 'returns false' do
|
|
expect(described_class.state(1337)).to be(false)
|
|
end
|
|
end
|
|
|
|
context 'when active flag is set to true' do
|
|
|
|
before do
|
|
create(:'chat/agent', active: true, updated_by: user)
|
|
end
|
|
|
|
it 'returns true' do
|
|
expect(described_class.state(user.id)).to be(true)
|
|
end
|
|
end
|
|
|
|
context 'when active flag is set to false' do
|
|
|
|
before do
|
|
create(:'chat/agent', active: false, updated_by: user)
|
|
end
|
|
|
|
it 'returns false' do
|
|
expect(described_class.state(user.id)).to be(false)
|
|
end
|
|
end
|
|
|
|
context 'when setting state for not existing record' do
|
|
it 'creates a record' do
|
|
expect { described_class.state(user.id, true) }.to change { described_class.exists?(updated_by: user) }.from(false).to(true)
|
|
end
|
|
end
|
|
|
|
context 'when setting same state for record' do
|
|
|
|
let(:record) { create(:'chat/agent', active: true, updated_by: user) }
|
|
|
|
before do
|
|
# avoid race condition with same updated_at time
|
|
record
|
|
travel_to 5.minutes.from_now
|
|
end
|
|
|
|
it 'updates updated_at timestamp' do
|
|
expect { described_class.state(record.updated_by_id, record.active) }.to change { record.reload.updated_at }
|
|
end
|
|
|
|
it 'returns false' do
|
|
expect(described_class.state(record.updated_by_id, record.active)).to be(false)
|
|
end
|
|
end
|
|
|
|
context 'when setting different state for record' do
|
|
|
|
let(:record) { create(:'chat/agent', active: true, updated_by: user) }
|
|
|
|
before do
|
|
# avoid race condition with same updated_at time
|
|
record
|
|
travel_to 5.minutes.from_now
|
|
end
|
|
|
|
it 'updates updated_at timestamp' do
|
|
expect { described_class.state(record.updated_by_id, !record.active) }.to change { record.reload.updated_at }
|
|
end
|
|
|
|
it 'returns true' do
|
|
expect(described_class.state(record.updated_by_id, !record.active)).to be(true)
|
|
end
|
|
end
|
|
end
|
|
end
|