Skip to content

Share the view context in tests #2410

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

camertron
Copy link
Contributor

@camertron camertron commented Aug 1, 2025

What are you trying to accomplish?

Fixes: #2408

This PR addresses an issue causing out-of-order rendering issues in tests when the test attempts to re-use the view context provided by the test controller.

The test controller does not memoize the view context, so calling vc_test_controller.view_context always returns a new instance. If this method is called by the test code, any subsequent calls to eg. render_inline will create a new view context and append to its buffer while the test code appends to a different buffer.

This was an issue in v3 as well, but could be avoided by enabling the capture compatibility patch. Since v4 has removed the patch, there is no way to avoid the problem (although very few people will get bitten by this).

What approach did you choose and why?

I propose introducing a new method, vc_test_view_context, that can be called both by test code and render_inline so that both can reference the same shared view context. This shared context is reset after each call to render_inline, meaning test code will have to call it again if additional calls to render_inline are made. This is not ideal, but all the other avenues I explored were less ergonomic or required even more knowledge of the inner workings of ActionView and ViewComponent.

Todo

I suspect the with_variant and with_format helpers are not actually working as expected since they set values on vc_test_controller.view_context, which returns a view context that is immediately discarded 😬

I need to look at this further.

The with_variant and with_format helpers actually set values on the lookup context, which, confusingly, is shared between view context instances. That's why they work as expected.

@camertron camertron marked this pull request as draft August 1, 2025 06:16
@camertron camertron force-pushed the shared_view_context_in_test_helpers branch from 5ca6a9d to 2f88815 Compare August 2, 2025 05:47
@camertron camertron marked this pull request as ready for review August 6, 2025 05:41
@camertron camertron requested a review from joelhawksley August 6, 2025 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Form label not rendering correctly
1 participant