forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce Spree::Event's test interface to run only selected listeners #151
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
5 times, most recently
from
July 28, 2021 09:20
cb6db00
to
270af04
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events
branch
from
July 29, 2021 09:33
17198b3
to
e1662c2
Compare
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
3 times, most recently
from
August 3, 2021 15:24
63290f2
to
e12da7e
Compare
aldesantis
approved these changes
Sep 8, 2021
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@waiting-for-dev looks good! Couple of comments and then it's GTM. ❤️ Approving because it's pretty small stuff.
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
from
September 9, 2021 08:16
e12da7e
to
e98d018
Compare
Thanks for your review @aldesantis, I forced a push with the updates to rebase the dependant branches more easily. |
aldesantis
approved these changes
Sep 10, 2021
Given the global nature of our event bus, we need a system to scope the execution of a block to a selected list of subscribers. That's useful for testing purposes, as we need to be sure that others subscribers are not interfering with our expectations. This commit introduces a `Spree::Event.performing_only(listeners)` method. It takes a block during the execution of which only the provided listeners are subscribed: ```ruby listener1 = Spree::Event.subscribe('foo') { do_something } listener2 = Spree::Event.subscribe('foo') { do_something_else } Spree::Event.performing_only(listener1) do Spree::Event.fire('foo') # only listener1 will run end Spree::Event.fire('foo') # both listener1 & listener2 will run ``` This behavior is only available for the new `Spree::Event::Adapters::Default` adapter. We only need that for testing purposes, so the method is made available after calling `Spree::Event.enable_test_interface`. It prevents the main `Spree::Event` API from being bloated and sends a more explicit message to users. We also add a `Spree::Subscriber#listeners` method, which returns the set of generated listeners for a given subscriber module. It's called automatically by `Spree::Event.performing_only` so that users can directly specify that they only want the listeners for a given subscriber module to be run. `Spree::Subscriber#listeners` accepts an array of event names as arguments in case more fine-grained control is required. ```ruby module EmailSubscriber include Spree::Event::Subscriber event_action :foo event_action :bar def foo(_event) do_something end def bar(_event) do_something_else end end Spree::Event.performing_only(EmailSubscriber) do Spree::Event.fire('foo') # both foo & bar methods will run end Spree::Event.performing_only(EmailSubscriber.listeners('foo')) do Spree::Event.fire('foo') # only foo method will run end ``` A specialized `Spree::Event.performing_nothing` method calls `Spree::Event.performing_only` with no listeners at all.
waiting-for-dev
force-pushed
the
waiting-for-dev/events_testability
branch
from
November 16, 2021 05:23
e98d018
to
bde5b62
Compare
waiting-for-dev
changed the base branch from
waiting-for-dev/events
to
master
November 16, 2021 05:23
Moved to solidusio#4204 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Given the global nature of our event bus, we need a system to scope the
execution of a block to a selected list of subscribers. That's useful
for testing purposes, as we need to be sure that others subscribers are
not interfering with our expectations.
This commit introduces a
Spree::Event.performing_only(listeners)
method. It takes a block during the execution of which only the provided
listeners are subscribed:
This behavior is only available for the new
Spree::Event::Adapters::Default
adapter.We only need that for testing purposes, so the method is made available
after calling
Spree::Event.enable_test_interface
. It prevents the mainSpree::Event
API from being bloated and sends a more explicit messageto users.
We also add a
Spree::Subscriber#listeners
method, which returns theset of generated listeners for a given subscriber module. It's called
automatically by
Spree::Event.performing_only
so that users candirectly specify that they only want the listeners for a given
subscriber module to be run.
Spree::Subscriber#listeners
accepts anarray of event names as arguments in case more fine-grained control is
required.
Checklist: