2019-05-08 21:28:07 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-06 23:37:29 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe UserNotification, type: :model do
|
2017-10-02 20:51:54 +08:00
|
|
|
let(:user) { create :user }
|
2019-05-08 21:28:07 +08:00
|
|
|
let(:user_notification) { build :user_notification }
|
|
|
|
|
|
|
|
it 'is valid' do
|
|
|
|
expect(user_notification).to be_valid
|
|
|
|
end
|
2017-10-02 20:51:54 +08:00
|
|
|
|
2017-07-06 23:37:29 +08:00
|
|
|
it 'should be of class UserNotification' do
|
|
|
|
expect(subject.class).to eq UserNotification
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Database table' do
|
|
|
|
it { should have_db_column :user_id }
|
|
|
|
it { should have_db_column :notification_id }
|
|
|
|
it { should have_db_column :checked }
|
|
|
|
it { should have_db_column :created_at }
|
|
|
|
it { should have_db_column :updated_at }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'Relations' do
|
2019-07-26 18:40:36 +08:00
|
|
|
it { should belong_to(:user).optional }
|
|
|
|
it { should belong_to(:notification).optional }
|
2017-07-06 23:37:29 +08:00
|
|
|
end
|
2017-10-02 20:51:54 +08:00
|
|
|
|
|
|
|
describe '#unseen_notification_count ' do
|
|
|
|
let(:notifcation) { create :notification }
|
|
|
|
it 'returns a number of unseen notifications' do
|
|
|
|
create :user_notification, user: user, notification: notifcation
|
|
|
|
expect(UserNotification.unseen_notification_count(user)).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#seen_by_user' do
|
|
|
|
let!(:notification) { create :notification }
|
|
|
|
let!(:user_notification_one) do
|
|
|
|
create :user_notification, user: user, notification: notification
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'set the check status to false' do
|
2019-07-26 18:40:36 +08:00
|
|
|
expect do
|
2017-10-02 20:51:54 +08:00
|
|
|
UserNotification.seen_by_user(user)
|
2019-07-26 18:40:36 +08:00
|
|
|
end.to change { user_notification_one.reload.checked }.from(false).to(true)
|
2017-10-02 20:51:54 +08:00
|
|
|
end
|
|
|
|
end
|
2017-07-06 23:37:29 +08:00
|
|
|
end
|