Skip to content

Commit

Permalink
feat: Deprecate toBeEmpty in favour of toBeEmptyDomElement (#216) (#254)
Browse files Browse the repository at this point in the history
In order to prevent name clashes with jest-extended, the toBeEmpty will
have from now on a more specific name.

Co-authored-by: Daniela Valero <daniela.valero@publicissapient.com>
  • Loading branch information
DanielaValero and Daniela Valero committed May 28, 2020
1 parent 85cf707 commit 927c5a4
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ clear to read and to maintain.
- [`toBeDisabled`](#tobedisabled)
- [`toBeEnabled`](#tobeenabled)
- [`toBeEmpty`](#tobeempty)
- [`toBeEmptyDOMElement`](#tobeemptydomelement)
- [`toBeInTheDocument`](#tobeinthedocument)
- [`toBeInvalid`](#tobeinvalid)
- [`toBeRequired`](#toberequired)
Expand Down Expand Up @@ -207,6 +208,31 @@ expect(getByTestId('empty')).toBeEmpty()
expect(getByTestId('not-empty')).not.toBeEmpty()
```

> Note: This matcher is being deprecated due to a name clash with
> `jest-extended`. See more info in #216. In the future, please use only:
> [`toBeEmptyDOMElement`](#toBeEmptyDOMElement)
<hr />

### `toBeEmptyDOMElement`

```typescript
toBeEmptyDOMElement()
```

This allows you to assert whether an element has content or not.

#### Examples

```html
<span data-testid="not-empty"><span data-testid="empty"></span></span>
```

```javascript
expect(getByTestId('empty')).toBeEmptyDOMElement()
expect(getByTestId('not-empty')).not.toBeEmptyDOMElement()
```

<hr />

### `toBeInTheDocument`
Expand Down Expand Up @@ -1135,6 +1161,7 @@ Thanks goes to these people ([emoji key][emojis]):

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification.
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/to-be-empty-dom-element.js
@@ -0,0 +1,32 @@
import {render} from './helpers/test-utils'

test('.toBeEmptyDOMElement', () => {
const {queryByTestId} = render(`
<span data-testid="not-empty">
<span data-testid="empty"></span>
<svg data-testid="svg-empty"></svg>
</span>`)

const empty = queryByTestId('empty')
const notEmpty = queryByTestId('not-empty')
const svgEmpty = queryByTestId('svg-empty')
const nonExistantElement = queryByTestId('not-exists')
const fakeElement = {thisIsNot: 'an html element'}

expect(empty).toBeEmptyDOMElement()
expect(svgEmpty).toBeEmptyDOMElement()
expect(notEmpty).not.toBeEmptyDOMElement()

// negative test cases wrapped in throwError assertions for coverage.
expect(() => expect(empty).not.toBeEmptyDOMElement()).toThrowError()

expect(() => expect(svgEmpty).not.toBeEmptyDOMElement()).toThrowError()

expect(() => expect(notEmpty).toBeEmptyDOMElement()).toThrowError()

expect(() => expect(fakeElement).toBeEmptyDOMElement()).toThrowError()

expect(() => {
expect(nonExistantElement).toBeEmptyDOMElement()
}).toThrowError()
})
3 changes: 3 additions & 0 deletions src/__tests__/to-be-empty.js
@@ -1,6 +1,8 @@
import {render} from './helpers/test-utils'

test('.toBeEmpty', () => {
// @deprecated intentionally hiding warnings for test clarity
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const {queryByTestId} = render(`
<span data-testid="not-empty">
<span data-testid="empty"></span>
Expand Down Expand Up @@ -29,4 +31,5 @@ test('.toBeEmpty', () => {
expect(() => {
expect(nonExistantElement).toBeEmpty()
}).toThrowError()
spy.mockRestore()
})
2 changes: 2 additions & 0 deletions src/matchers.js
@@ -1,6 +1,7 @@
import {toBeInTheDOM} from './to-be-in-the-dom'
import {toBeInTheDocument} from './to-be-in-the-document'
import {toBeEmpty} from './to-be-empty'
import {toBeEmptyDOMElement} from './to-be-empty-dom-element'
import {toContainElement} from './to-contain-element'
import {toContainHTML} from './to-contain-html'
import {toHaveTextContent} from './to-have-text-content'
Expand All @@ -23,6 +24,7 @@ export {
toBeInTheDOM,
toBeInTheDocument,
toBeEmpty,
toBeEmptyDOMElement,
toContainElement,
toContainHTML,
toHaveTextContent,
Expand Down
22 changes: 22 additions & 0 deletions src/to-be-empty-dom-element.js
@@ -0,0 +1,22 @@
import {matcherHint, printReceived} from 'jest-matcher-utils'
import {checkHtmlElement} from './utils'

export function toBeEmptyDOMElement(element) {
checkHtmlElement(element, toBeEmptyDOMElement, this)

return {
pass: element.innerHTML === '',
message: () => {
return [
matcherHint(
`${this.isNot ? '.not' : ''}.toBeEmptyDOMElement`,
'element',
'',
),
'',
'Received:',
` ${printReceived(element.innerHTML)}`,
].join('\n')
},
}
}
6 changes: 5 additions & 1 deletion src/to-be-empty.js
@@ -1,7 +1,11 @@
import {matcherHint, printReceived} from 'jest-matcher-utils'
import {checkHtmlElement} from './utils'
import {checkHtmlElement, deprecate} from './utils'

export function toBeEmpty(element) {
deprecate(
'toBeEmpty',
'Please use instead toBeEmptyDOMElement for finding empty nodes in the DOM.',
)
checkHtmlElement(element, toBeEmpty, this)

return {
Expand Down

0 comments on commit 927c5a4

Please sign in to comment.