2019-01-25 21:53:06 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe SystemNotification do
|
2019-05-08 20:22:52 +08:00
|
|
|
let(:system_notification) { build :system_notification }
|
2019-01-25 21:53:06 +08:00
|
|
|
|
|
|
|
it 'is valid' do
|
|
|
|
expect(system_notification).to be_valid
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Validations' do
|
|
|
|
describe '#title' do
|
|
|
|
it { is_expected.to validate_presence_of(:title) }
|
2019-02-20 23:05:15 +08:00
|
|
|
it { is_expected.to validate_length_of(:title).is_at_most(255) }
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#modal_title' do
|
|
|
|
it { is_expected.to validate_presence_of(:modal_title) }
|
2019-02-20 23:05:15 +08:00
|
|
|
it { is_expected.to validate_length_of(:modal_title).is_at_most(255) }
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#modal_body' do
|
|
|
|
it { is_expected.to validate_presence_of(:modal_body) }
|
2019-02-20 23:05:15 +08:00
|
|
|
it { is_expected.to validate_length_of(:modal_body).is_at_most(50000) }
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#description' do
|
|
|
|
it { is_expected.to validate_presence_of(:description) }
|
2019-02-20 23:05:15 +08:00
|
|
|
it { is_expected.to validate_length_of(:description).is_at_most(255) }
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#source_id' do
|
|
|
|
it { is_expected.to validate_presence_of(:source_id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#source_created_at' do
|
|
|
|
it { is_expected.to validate_presence_of(:source_created_at) }
|
|
|
|
end
|
2019-01-31 15:53:02 +08:00
|
|
|
|
|
|
|
describe '#last_time_changed_at' do
|
|
|
|
it { is_expected.to validate_presence_of(:last_time_changed_at) }
|
|
|
|
end
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Associations' do
|
|
|
|
it { is_expected.to have_many(:users) }
|
|
|
|
end
|
2019-02-04 21:33:22 +08:00
|
|
|
|
|
|
|
describe 'self.last_sync_timestamp' do
|
|
|
|
context 'when there is no users or system notifications in db' do
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.last_sync_timestamp).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there is no system notifications in db' do
|
2019-04-25 15:18:26 +08:00
|
|
|
it 'returns first users created_at' do
|
2019-02-04 21:33:22 +08:00
|
|
|
create :user
|
2019-04-25 15:18:26 +08:00
|
|
|
create :user, created_at: Time.now + 5.seconds
|
2019-02-04 21:33:22 +08:00
|
|
|
|
|
|
|
expect(described_class.last_sync_timestamp)
|
2019-04-25 15:18:26 +08:00
|
|
|
.to be == User.first.created_at.to_i
|
2019-02-04 21:33:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when have some system notifications' do
|
|
|
|
it 'returns last system notifications last_time_changed_at timestamp' do
|
|
|
|
create :user
|
|
|
|
create :system_notification
|
|
|
|
|
|
|
|
expect(described_class.last_sync_timestamp)
|
|
|
|
.to be SystemNotification.last.last_time_changed_at.to_i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-19 18:08:59 +08:00
|
|
|
|
|
|
|
describe 'Methods' do
|
2019-05-08 20:22:52 +08:00
|
|
|
let(:user) { create :user }
|
2019-02-19 18:08:59 +08:00
|
|
|
let(:notifcation_one) { create :system_notification }
|
|
|
|
let(:notifcation_two) { create :system_notification, title: 'Special one' }
|
|
|
|
before do
|
|
|
|
create :user_system_notification,
|
|
|
|
user: user,
|
|
|
|
system_notification: notifcation_one
|
|
|
|
create :user_system_notification,
|
|
|
|
user: user,
|
|
|
|
system_notification: notifcation_two
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'get last notifications without search' do
|
|
|
|
result = SystemNotification.last_notifications(user)
|
|
|
|
expect(result.length).to eq 2
|
|
|
|
expect(result.first).to respond_to(
|
|
|
|
:id,
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:last_time_changed_at,
|
|
|
|
:seen_at,
|
|
|
|
:read_at
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'get last notifications with search' do
|
|
|
|
result = SystemNotification.last_notifications(user, 'Special one')
|
|
|
|
expect(result.length).to eq 1
|
|
|
|
expect(result.first).to respond_to(
|
|
|
|
:id,
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:last_time_changed_at,
|
|
|
|
:seen_at,
|
|
|
|
:read_at
|
|
|
|
)
|
|
|
|
expect(result.first.title).to eq 'Special one'
|
|
|
|
end
|
|
|
|
end
|
2019-01-25 21:53:06 +08:00
|
|
|
end
|