Skip to content

Commit

Permalink
Add assertions module (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Serabe authored Oct 8, 2023
1 parent 08bf6fc commit 46c008f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dom_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule DomHelpers do
defmacro __using__(_opts) do
quote do
import DomHelpers.Accessors
import DomHelpers.Assertions
import DomHelpers.Selectors
end
end
Expand Down
32 changes: 32 additions & 0 deletions lib/dom_helpers/assertions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule DomHelpers.Assertions do
@moduledoc """
This module contains some extra helpers specially for assertions,
though is preferred to use direct comparisons with the different
[selectors](`DomHelpers.Selectors`) and [accessors](`DomHelpers.Accessors`).
"""

import DomHelpers.Accessors

@doc """
Checks if the given selector is found at least once
in the given document. Works like [`has_element?/2`](`Phoenix.LiveViewTest.has_element?/2`)
but with the parameters reversed so you can pipe when
building the selector.
## Example
```
iex> "li" |> is_in?("<ul><li>First</li><li>Second</li></ul>")
true
iex> "li" |> is_in?("<ul><li>First</li></ul>")
true
iex> "span" |> is_in?("<ul><li>First</li></ul>")
false
```
"""
def is_in?(selector, htmlable) do
find_count(htmlable, selector) > 0
end
end
8 changes: 8 additions & 0 deletions test/dom_helpers/assetions_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule DomHelpers.AssertionsTest do
@moduledoc false
use DomHelpers.Case

import DomHelpers.Assertions

doctest DomHelpers.Assertions
end

0 comments on commit 46c008f

Please sign in to comment.