Skip to content

Commit c5b109e

Browse files
authored
DEV: add tips for testing streaming updates in system specs (#66)
1 parent db29759 commit c5b109e

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

docs/03-code-internals/20-system-specs.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,43 @@ Finally, some strings are quite big and there's no need to check that the entire
445445

446446
Our `RateLimiter` system is disabled by default in specs. However if you need to turn it on to test some rate limiting specifically in system specs (though you should use request specs for this), use `RateLimiter.enable`.
447447

448+
### Testing Streaming Interactions (MessageBus Updates)
449+
450+
When testing UI interactions that trigger real-time updates—typically sent via `MessageBus`—you might find that these updates aren’t appearing in your system tests as expected. To ensure that streaming updates work reliably in tests, make sure the following pieces are in place:
451+
452+
1. **Track the `MessageBus.last_id` on the server and pass it to the client.**
453+
This ensures the client receives only messages that occur after the page has loaded.
454+
455+
On the server side, expose the last ID for the relevant `MessageBus` channel (usually via a serializer):
456+
457+
```ruby
458+
def foo_channel_last_id
459+
MessageBus.last_id("/foo-channel")
460+
end
461+
```
462+
463+
On the client side, use this ID when subscribing to the MessageBus channel:
464+
465+
```js
466+
this.messageBus.subscribe(
467+
this.fooChannel,
468+
this.updateResult,
469+
this.args.foo_channel_last_id
470+
);
471+
```
472+
473+
> ⚠️ Note: Be mindful that frequent calls to `MessageBus.last_id` can result in increased Redis traffic. If you're calling this in high-volume serializers or endpoints, consider optimizing to reduce the number of Redis hits—for example, by caching or batching where appropriate.
474+
475+
2. **Run jobs immediately in your system specs.**
476+
477+
This ensures background jobs that publish messages (e.g., via `MessageBus.publish`) are executed synchronously during the test:
478+
479+
```ruby
480+
before do
481+
Jobs.run_immediately!
482+
end
483+
```
484+
448485
## Advanced Chrome Interaction
449486

450487
In certain cases you will need to use some advanced features of Chrome in your system specs. Some examples are interacting with the clipboard (copy and paste) and network manipulation (simulating slow connections). This is achieved using the [Chrome DevTools Protocol](https://chromedevtools.github.io/devtools-protocol/), and sometimes with native Capybara functionality.

0 commit comments

Comments
 (0)