[5.8] Bind mix helper to the container to allow swapping in tests #26289
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.
This PR binds the mix helper functionality to the container to allow it to be swapped easily during testing.
Why is this even needed?
If a view is using the mix helper to retrieve an asset, and the key passed to the mix helper is created dynamically i.e. related to a model attribute, you need to:
webpack.mix.js
Example
You have a
Business
class. The class has aresource_key
that relates to assets, such as a background image. This key is generated, in your tests, usingFaker
.In order for me to be able to test this view I would need to do something like this...
With it bound to the container we can essentially "fake" the mix method:
Obviously the assets that go through the mix helper are known when writing your test, so you could create a list of known resource keys that you know are going to be available, however this means you have to have run
npm run dev
to ensure the mix manifest is available, and if the known assets change you need to update either your tests or your model factories.To keep the PR focused on what is actually happening I have not made any changes to the helpers code, but moving it to a dedicated class will mean that, if you wanted, we could also: