0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-09 21:45:25 +08:00
discourse/spec/services/notifications/consolidate_notifications_spec.rb
Alan Guo Xiang Tan 6df9b0b357
DEV: Prefer RSpec primitives for test setup (#42122)
RSpec setup becomes harder to follow at either extreme: trivial fixture
wrappers hide lifecycle and intent, while forcing every named operation
inline repeats low-level protocol and configuration details.

This change documents and applies a test-setup hierarchy:

- use `fab!`, `let`, `let!`, `subject`, and inline `Fabricate` according
to lifecycle and role;
- use a small example-group method when parameterized behavior gives one
spec useful vocabulary;
- move helpers into auto-loaded `spec/support` only when they are shared
across spec files;
- use fabricators and page objects for the data shapes and system-test
interfaces they own.

Core and plugin support files are loaded centrally by `rails_helper`, so
plugin-specific support loaders are unnecessary. The migration specs
encountered during the sweep are removed according to repository policy;
production migrations are unchanged.
2026-07-30 11:17:22 +08:00

60 lines
2 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe Notifications::ConsolidateNotifications do
describe "#before_consolidation_callbacks" do
fab!(:user)
let(:rule) do
described_class.new(
from: Notification.types[:liked],
to: Notification.types[:liked],
consolidation_window: 10.minutes,
consolidated_query_blk:
Proc.new { |notifications| notifications.where("(data::json ->> 'consolidated')::bool") },
threshold: 1,
).set_mutations(set_data_blk: Proc.new { |n| n.data_hash.merge(consolidated: true) })
end
let(:like_notifications) do
Array.new(3) do
Fabricate.build(
:notification,
user: user,
notification_type: Notification.types[:liked],
data: {}.to_json,
)
end
end
it "applies a callback when consolidating a notification" do
rule.before_consolidation_callbacks(
before_consolidation_blk:
Proc.new { |_, data| data[:consolidation_callback_called] = true },
)
rule.consolidate_or_save!(like_notifications[0])
rule.consolidate_or_save!(like_notifications[1])
consolidated_notification = Notification.where(user: user).last
expect(consolidated_notification.data_hash[:consolidation_callback_called]).to eq(true)
end
it "applies a callback when updating a consolidated notification" do
rule.before_consolidation_callbacks(
before_update_blk: Proc.new { |_, data| data[:update_callback_called] = true },
)
rule.consolidate_or_save!(like_notifications[0])
rule.consolidate_or_save!(like_notifications[1])
consolidated_notification = Notification.where(user: user).last
expect(consolidated_notification.data_hash[:update_callback_called]).to be_nil
rule.consolidate_or_save!(like_notifications[2])
consolidated_notification = Notification.where(user: user).last
expect(consolidated_notification.data_hash[:update_callback_called]).to eq(true)
end
end
end