How to test a liveview patch to a url with query parameters?

Is there a way to test a liveview patch to a url with query parameters?

If the url is w/o query params, then I can do it with
assert_patch(live, ~p"/companies/#{company}/edit"
But if the url has query parameters (like /companies/123/edit?sort_dir=asc ) assert_patch cannot be used.

I’m courious. Not in a place to try it, but would providing an interpolated url not work?

assert_patch(live, ~p"/companies/#{company}/edit?#{%{sort_dir: :asc}}"

Thanks, but the query parameters are not known beforehand. It could be sort_dir: desc and this query param is only an example.

So, I was wondering if there is a version of asset_patch but with a regex and not hardcoded to path.

Maybe I’m just overthinking it, and need to hardcode the query params, in order to be testable.

As long as it’s a map, you can still pass it in. Or do you want to assert that the live_patch is called with a specific route, but containing any query_params?

In that case, assert_patch is actually assert_navigation under the hood, which actually just waits for messages like assert_recieve would

So, again, operating blindly here, but based on code in

you should be able to do just

assert_receive {_ref, {:patch, _topic, %{to: new_to}}}
assert new_to =~ part_of_your_path

or, one step closer to the implementation of assert_navigation:

%{proxy: {ref, topic, _}, endpoint: endpoint} = view
assert_receive {^ref, {:patch, ^topic, %{to: new_to}}}
assert new_to =~ part_of_your_path
1 Like

Wow!
Going to the source code is something I hadn’t think of!

Thanks, this approach solves my problem!

1 Like