1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Subscribable, 'Subscribable' do
let(:project) { create(:project) }
let(:resource) { create(:issue, project: project) }
let(:user_1) { create(:user) }
shared_examples 'returns expected values' do |method|
context 'without user' do
it 'returns false' do
expect(resource.public_send(method, nil, project)).to be_falsey
end
end
context 'without project' do
it 'returns false when no subscription exists' do
expect(resource.public_send(method, user_1)).to be_falsey
end
it 'returns true when a subscription exists and subscribed is true' do
resource.subscriptions.create!(user: user_1, subscribed: true)
expect(resource.public_send(method, user_1)).to be_truthy
end
it 'returns false when a subscription exists and subscribed is false' do
resource.subscriptions.create!(user: user_1, subscribed: false)
expect(resource.public_send(method, user_1)).to be_falsey
end
end
context 'with project' do
it 'returns false when no subscription exists' do
expect(resource.public_send(method, user_1, project)).to be_falsey
end
it 'returns true when a subscription exists and subscribed is true' do
resource.subscriptions.create!(user: user_1, project: project, subscribed: true)
expect(resource.public_send(method, user_1, project)).to be_truthy
end
it 'returns false when a subscription exists and subscribed is false' do
resource.subscriptions.create!(user: user_1, project: project, subscribed: false)
expect(resource.public_send(method, user_1, project)).to be_falsey
end
end
end
describe '#subscribed?' do
it_behaves_like 'returns expected values', :subscribed?
end
describe '#subscribers' do
it 'returns [] when no subcribers exists' do
expect(resource.subscribers(project)).to be_empty
end
it 'returns the subscribed users' do
user_2 = create(:user)
resource.subscriptions.create!(user: user_1, subscribed: true)
resource.subscriptions.create!(user: user_2, project: project, subscribed: true)
resource.subscriptions.create!(user: create(:user), project: project, subscribed: false)
expect(resource.subscribers(project)).to contain_exactly(user_1, user_2)
end
end
context 'with subscription scopes' do
let(:resource_2) { create(:issue, project: project) }
before do
resource.subscriptions.create!(user: user_1, subscribed: true)
resource_2.subscriptions.create!(user: user_1, project: project, subscribed: false)
end
describe '.explicitly_subscribed' do
it 'returns subscribed issues' do
expect(project.issues.explicitly_subscribed(user_1)).to contain_exactly(resource)
end
end
describe '.explicitly_unsubscribed' do
it 'returns unsubscribed issues' do
expect(project.issues.explicitly_unsubscribed(user_1)).to contain_exactly(resource_2)
end
end
end
describe '#toggle_subscription' do
context 'without project' do
it 'toggles the current subscription state for the given user' do
expect(resource.subscribed?(user_1)).to be_falsey
resource.toggle_subscription(user_1)
expect(resource.subscribed?(user_1)).to be_truthy
end
end
context 'with project' do
it 'toggles the current subscription state for the given user' do
expect(resource.subscribed?(user_1, project)).to be_falsey
resource.toggle_subscription(user_1, project)
expect(resource.subscribed?(user_1, project)).to be_truthy
end
end
end
describe '#subscribe' do
context 'without project' do
it 'subscribes the given user' do
expect(resource.subscribed?(user_1)).to be_falsey
resource.subscribe(user_1)
expect(resource.subscribed?(user_1)).to be_truthy
end
end
context 'with project' do
it 'subscribes the given user' do
expect(resource.subscribed?(user_1, project)).to be_falsey
resource.subscribe(user_1, project)
expect(resource.subscribed?(user_1, project)).to be_truthy
end
end
end
describe '#unsubscribe' do
context 'without project' do
it 'unsubscribes the given current user' do
resource.subscriptions.create!(user: user_1, subscribed: true)
expect(resource.subscribed?(user_1)).to be_truthy
resource.unsubscribe(user_1)
expect(resource.subscribed?(user_1)).to be_falsey
end
end
context 'with project' do
it 'unsubscribes the given current user' do
resource.subscriptions.create!(user: user_1, project: project, subscribed: true)
expect(resource.subscribed?(user_1, project)).to be_truthy
resource.unsubscribe(user_1, project)
expect(resource.subscribed?(user_1, project)).to be_falsey
end
end
end
describe '#set_subscription' do
shared_examples 'setting subscriptions' do
context 'when desired_state is set to true' do
context 'when a user is subscribed to the resource' do
it 'keeps the user subscribed' do
resource.subscriptions.create!(user: user_1, subscribed: true, project: resource_project)
resource.set_subscription(user_1, true, resource_project)
expect(resource.subscribed?(user_1, resource_project)).to be_truthy
end
end
context 'when a user is not subscribed to the resource' do
it 'subscribes the user to the resource' do
expect { resource.set_subscription(user_1, true, resource_project) }
.to change { resource.subscribed?(user_1, resource_project) }
.from(false).to(true)
end
end
end
context 'when desired_state is set to false' do
context 'when a user is subscribed to the resource' do
it 'unsubscribes the user from the resource' do
resource.subscriptions.create!(user: user_1, subscribed: true, project: resource_project)
expect { resource.set_subscription(user_1, false, resource_project) }
.to change { resource.subscribed?(user_1, resource_project) }
.from(true).to(false)
end
end
context 'when a user is not subscribed to the resource' do
it 'keeps the user unsubscribed' do
resource.set_subscription(user_1, false, resource_project)
expect(resource.subscribed?(user_1, resource_project)).to be_falsey
end
end
end
end
context 'without project' do
let(:resource_project) { nil }
it_behaves_like 'setting subscriptions'
end
context 'with project' do
let(:resource_project) { project }
it_behaves_like 'setting subscriptions'
end
end
describe '#lazy_subscription' do
let(:labels) { create_list(:group_label, 5) }
before do
labels.each do |label|
create(:subscription, :group_label, user: user_1, subscribable: label)
end
end
it 'executes only one SQL query' do
lazy_queries = ActiveRecord::QueryRecorder.new do
labels.each { |label| label.lazy_subscription(user_1) }
end
preloaded_queries = ActiveRecord::QueryRecorder.new do
labels.each { |label| label.lazy_subscription(user_1)&.subscribed? }
end
expect(lazy_queries.count).to eq(0)
expect(preloaded_queries.count).to eq(1)
end
context 'with work items' do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:work_item) { create(:work_item, :task, project: project) }
let_it_be(:issue) { create(:issue, project: project) }
before do
[issue, work_item].each do |item|
create(:subscription, user: user, subscribable: item, subscribed: true, project: project)
end
end
it 'loads correct subscribable type' do
expect(issue).to receive(:subscribable_type).and_return('Issue')
issue.lazy_subscription(user, project)
expect(work_item).to receive(:subscribable_type).and_return('Issue')
work_item.lazy_subscription(user, project)
end
it 'matches existing subscription type' do
expect(issue.subscribed?(user, project)).to eq(true)
expect(work_item.subscribed?(user, project)).to eq(true)
end
end
end
end
|