docbrown/spec/models/notification_subscription_spec.rb
Lawrence 0c93bde6c2
Subscribe to Comment Functionality (#19555)
* Move save over and add button labels

* comment notification design and functionality

* controller error handling

* add ternery operator for background class

* minor cleanup

* add proper styling toggles

* tests and mobile spec

* add for all buttons

* add article as a param

* comment is not required on article preview

* update policy spec

* add test for coverage

* add proper button titles

* use the last user comment as comment id

* add better error handling and remove multiple fetch calls in js

* copy change to threads

* update copy, fix multiple fetch calls, add observer for loading more notifications

* update spec

* add rspecs

* more test specs

* fix test specs

* subscribe button text tests

* add export

* optimistically update ui function tests

* button initialize test

* check for comment ancestry

* add feature flag block

* some cleanup

* update logic and specs

* oops

* rubocop issues

* change config names

* change config names

* fix spec

* add one more conditional

* check for comments

* service-object the toggler

* separate routes

* continual refactoring

* refine spec tests

* clean up policy

* Adjust js and view files to match be params

* some service object refactoring

* separate endpoints on client-side

* broken spec

* service specs

* policy spec fixes

* remove action param

* remove action param from spec

* minor updates

* trigger build

* update to post

* Services don't need permitted params

* Notifications continue to be a mess, decorator specs to follow

* Not toggling now

* This doesn't really belong here

* Fix tests after moving

* Streamline front-end

* There could be errors?

* polymorphic_name is a class thing

* ☠️ json_data in notification views

* Use feature_flag helper to get current_user

* Cannot derive current subscription status from notification, only from subscription

* Default is subscribe to all article comments

* Sync up matching unsubscribe, make subscribe idempotent-ish

* Temporarily comment-out button jest tests to see if I can unstick CI

* Stop trying to detect potential subscribe-to-comment

* Stop trying to detect potential subscribe-to-comment

* Minor spec refactoring

* Add specs for article notification decoration

* NotificationDecorator specs are rather long

* Rubocop likes it?

* Minor spec refactoring

* Doesn't need to stay pending

* Add spec for subscription finder

* Expand Subscribe service specs

* YAGNI, but I don't like hard-coded config

* Refactor Unsubscribe service specs

* nobody expects the infinite scroll

* This *might* work, except for feature flag

* Ancestry can be quite deep actually

* Rubocop

* Update subscribe button for subscribe-to-thread

* Jest tests are just tests

* hasAttribute != getAttribute

* Test knows **nothing** about window size

* Tests have been lying about mobileLabel this whole time

* Make payload/endpoint testable

* Is Jest actually happy???

* Still cleaning up jest/eslint

* Temporarily comment-out cypress tests pending feature flag

* Thanks, Rubocop, what would we do without you

* Preserve round-trip for 'top-level' and 'only-author' subscriptions

* Update specs

* Move save button icon

* Update test with better config param

* Try feature flag specific to cypress env

* Tell cypress to wait for (un)subscribing to work

---------

Co-authored-by: Lawrence S <lawrence@forem.com>
Co-authored-by: Joshua Wehner <joshua@forem.com>
2023-07-10 17:11:46 +02:00

82 lines
2.6 KiB
Ruby

require "rails_helper"
RSpec.describe NotificationSubscription do
subject(:subscription) { notification_subscription }
let(:user) { build(:user) }
let(:article) { build(:article, user: user) }
let(:notification_subscription) { build(:notification_subscription, user: user, notifiable: article) }
it { is_expected.to belong_to(:notifiable) }
it { is_expected.to belong_to(:user) }
it "validates config" do
expect(subscription).to(
validate_inclusion_of(:config)
.in_array(%w[all_comments top_level_comments only_author_comments]),
)
end
it { is_expected.to validate_presence_of(:config) }
it { is_expected.to validate_presence_of(:notifiable_type) }
it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(%i[notifiable_type notifiable_id]) }
describe "#notifiable_type" do
it "is valid if equals to Article" do
notification_subscription.notifiable_type = "Article"
expect(notification_subscription).to be_valid
end
it "is valid if equals to Comment" do
comment = create(:comment)
notification_subscription.notifiable_id = comment.id
notification_subscription.notifiable_type = "Comment"
expect(notification_subscription).to be_valid
end
it "is is invalid with Podcast" do
podcast = create(:podcast)
notification_subscription.notifiable_id = podcast.id
notification_subscription.notifiable_type = "Podcast"
expect(notification_subscription).not_to be_valid
end
end
describe ".for_notifiable" do
before do
subscription.save!
end
it "can find subscription if given a notifiable object" do
results = described_class.for_notifiable(subscription.notifiable)
expect(results).to contain_exactly(subscription)
end
it "can find subscription if given id & type" do
bad_results1 = described_class.for_notifiable(notifiable_type: "Article")
expect(bad_results1).to eq([])
bad_results2 = described_class.for_notifiable(notifiable_id: article.id)
expect(bad_results2).to eq([])
results = described_class.for_notifiable(notifiable_type: "Article", notifiable_id: article.id)
expect(results).to contain_exactly(subscription)
end
end
it "update_notification_subscriptions calls UpdateWorker later" do
allow(NotificationSubscriptions::UpdateWorker).to \
receive(:perform_async)
notifiable = build(:article, id: 123)
described_class.update_notification_subscriptions(notifiable)
expect(NotificationSubscriptions::UpdateWorker).to \
have_received(:perform_async)
.with(123, "Article")
end
end