neurodynamic

neurodynamic

How do I test a LiveView hook triggered by a Stripe form?

I have a page in my Phoenix app with some LiveView code. When you click a button, it shows the Stripe credit card modal form via a Hook. It looks roughly like this:

Hooks.StripeFormButton = {
  mounted() {    
    this.el.addEventListener("click", () => {
      showStripeForm({
        onSuccess: (token) => {
          this.pushEvent("save-card", {token: token})
        }
      })
    })
  }
}

So a user clicks the button, fills out the Stripe form, and when they submit it, the "save-card" event gets sent to the back-end. That will save their credit card info and then render some info to show that the save was successful.

My problem is, since the click and the form submit are done through hooks instead of phx-click and phx-submit attributes, I can’t figure out a way to test them. I want to be able to write a test that says “after the "save-card" event gets triggered, a confirmation message shows on the page”. But I can’t find a way to trigger the "save-card" event in a LiveView test without using something like render_click or render_submit which won’t work since I’m not using phx-click or phx-submit for the Stripe form. Is there a push_event testing function somewhere that I’m missing in the documentation? Or is there another way to do this?

Marked As Solved

PJUllrich

PJUllrich

Author of Building Table Views with Phoenix LiveView

Just FYI: There’s the render_hook/3 option now, which simulates sending an event through this.pushEvent("foo", "bar") in your JavaScript. It will then trigger the handle_event("foo", "bar", socket)-callback in your LiveView.

So, in your use case, you could write a test like:

render_hook(view, "save-card", %{"token" => stripe_token})

Also Liked

outlog

outlog

think you’ll need to test using something like wallaby eg headless chrome/firefox.. since this requires a js runtime..

you can see https://www.tddphoenix.com for getting started - excellent resource..

could be wrong though, but don’t see how elixir side should/can know what goes on in js hooks..

amnu3387

amnu3387

If you have the socket in the test (I haven’t written any tests with liveview, but I imagine you have?) you can just call YourLiveView.Module.handle_event("save-card", %{"token" => "your_token"}, the_socket) and assert on the result of save card and any side effects?

This doesn’t test the client interface tough, just that if an event "save-card" with those params are sent through the live view it works correctly.

neurodynamic

neurodynamic

That works perfectly; thank you!

Last Post!

unyraw

unyraw

Bit of a late answer, but could potentially help someone coming across this post :

You need to target the element with phx-hook attached to it. In your case it would be the div(s) you perform the iteration on.

Also, if your code resides in a Live Component and you don’t want the pushEventTo to be sent to the parent, you need to add a phx-target={@myself} on the targetted element.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement