Allow text/value arguments to be non-binaries#294
Conversation
Phoenix uses the `Phoenix.HTML.Safe` protocol to allow non-binary values to be safely rendered in the DOM. It would be very convenient if `assert_has`/`refute_has` would more closely match the code under test. This PR modifies `assert_has` and `refute_has` to call `Safe.to_iodata` (followed by `iodata_to_binary`) on their `text` and `value` arguments. This allows callers to do do things like: ``` assert_has(session, ".answer", 42) assert_has(session, ".answer", text: 42) assert_has(session, "Active Date", value: ~D[2026-01-10]) # etc. ``` I originally tried always passing text and value to `Safe.to_iodata` and `iodata_to_binary`, but that ended up failing a bunch of tests because the default implementation of `Safe.to_iodata` does HTML escaping on binaries, so I added a helper function that only calls the pipeline for non-binary values.
7097d3d to
338ef79
Compare
|
Thanks for opening this @randycoulman! My one hesitation with this is... will people pass a date struct but expect the date to be in a different format? I can imagine it's an easy mistake to make. That's why thus far, it's always required passing explicit text. Also, is there somewhere in the docs we could point out that we "fallback" to the Phoenix.HTML.Safe representation of the values? And that it allows you to pass custom structs that implement the protocol? |
|
Thanks for looking at this @germsvel! I'm happy to update some docs if you think this PR is worth merging. I wanted to get the idea in front of you first to see what you thought. I don't feel like I understand your concern about date structs. Can you say more? I'll write some words that will hopefully help, but if not, please clarify. With phoenix_test as it is, if I tried to do Because Phoenix uses I note that I can already pass a date struct to |
Update the function docs for `assert_has/3` and `refute_has/3` to mention the use of the `Phoenix.HTML.Safe` protocol.
|
@germsvel I've added a paragraph to the docs for |
germsvel
left a comment
There was a problem hiding this comment.
Thanks @randycoulman ! Looks great!
Phoenix uses the
Phoenix.HTML.Safeprotocol to allow non-binary values to be safely rendered in the DOM.It would be very convenient if
assert_has/refute_haswould more closely match the code under test.This PR modifies
assert_hasandrefute_hasto callSafe.to_iodata(followed byiodata_to_binary) on theirtextandvaluearguments.This allows callers to do do things like:
I originally tried always passing text and value to
Safe.to_iodataandiodata_to_binary, but that ended up failing a bunch of tests because the default implementation ofSafe.to_iodatadoes HTML escaping on binaries, so I added a helper function that only calls the pipeline for non-binary values.