germsvel / phoenix_test

MIT License
144 stars 20 forks source link

A way to assert for emptyness #53

Closed mattwynne closed 5 months ago

mattwynne commented 5 months ago

Maybe I'm being dumb, but I can't work out how to use assert_has (or refute_has) to check that this span is empty:

<span id="banana">
</span>

Maybe this is related to #46 because, if there was a way for me to just write:

assert_has("#banana", fn node -> String.trim(node.text) == "" end)

That might work, and be an extensible solution for other problems?

germsvel commented 5 months ago

@mattwynne you're not being dumb 😄 . That is an issue I've run into as well.

The fix isn't released yet (planning on doing a release today) but it's already on main if you want to use it.

I've introduced several options to help with these cases. By default assert_has/3 is doing a substring comparison (i.e. using =~) to match on the text asserted. That's helpful when you're trying to assert something inside HTML that also has HTML:

<div id="greeting">
  Hello world <span>Matt!</span>
</div>
assert_has(session, "#greeting", text: "Hello world") # <- this will pass

There's now an exact option that you can use:

assert_has(session, "#greeting", text: "Hello world", exact: true) # <- this will NOT pass

So, in your case, you can run the following assertion:

assert_has(session, "#banana", text: "", exact: true) 

NOTE the use of text: as an option in the functions above and not as the third positional argument. You'll have to change your function to use that if you want to get the options. In fact, I'm deprecating the assert_has/3 that uses text as a positional argument.

mattwynne commented 5 months ago

That looks great!