Skip to content

Commit

Permalink
Make Query.text/2 docs also point to assert_text/{2,3} (#770)
Browse files Browse the repository at this point in the history
* Make Query.text/2 docs also point to assert_has_text/{2,3}

This beefs up the docs on `Query.text/2`, which a lot of examples combine with `assert_has/2` for asserting that a particular element has some text. Pointing to `assert_has_text/{2,3}` from the docs here would have resolved a recent issue Mitch kindly helped me with [in Slack](https://elixir-lang.slack.com/archives/C4H1XRC0J/p1715093283885629) where I was trying to assert on the text of multiple nested elements.

* Fix typo in function name
  • Loading branch information
s3cur3 authored May 7, 2024
1 parent 82685dc commit 1fcfc9c
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion lib/wallaby/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,49 @@ defmodule Wallaby.Query do
The second is by providing an existing query and a value to set as the `text`
option.
## Example
Note that the text you're querying for must appear in a single element.
To assert on text that appears in multiple (potentially nested) elements,
rather than using `assert_has/2` in combination with `Query.text/2`, use
`assert_text/{2,3}` directly.
## Examples
### Querying the text of a specific element
```
submit_button = Query.css("#submit-button")
update_button = submit_button |> Query.text("Update")
create_button = submit_button |> Query.text("Create")
```
### Asserting on the text of a single element
```
submit_button = Query.css("#submit-button")
assert_has(session, Query.text(submit_button, "Create"))
```
### Asserting on the text of nested elements
HTML:
```
<div id="unread-notifications">
Unread messages:
<strong>1 message</strong>
</div>
```
Test:
```
assert_text(session, "Unread messages: 1 message")
notifications_block = Query.css("#unread-notifications")
# It would *not* work to query as: Query.text(notifications_block, "Unread messages: 1 message")
assert_has(session, Query.text(notifications_block, "Unread messages:"))
```
"""
def text(query_or_selector, value_or_opts \\ [])

Expand Down

0 comments on commit 1fcfc9c

Please sign in to comment.