Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add toBeEmptyElement assertion #111

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 16 additions & 6 deletions README.md
Expand Up @@ -42,7 +42,7 @@
- [Matchers](#matchers)
- [`toBeDisabled`](#tobedisabled)
- [`toBeEnabled`](#tobeenabled)
- [`toBeEmpty`](#tobeempty)
- [`toBeEmptyElement`](#tobeemptyelement)
- [`toContainElement`](#tocontainelement)
- [`toHaveProp`](#tohaveprop)
- [`toHaveTextContent`](#tohavetextcontent)
Expand Down Expand Up @@ -98,9 +98,9 @@ Alternatively, you can selectively import only the matchers you intend to use, a
`expect` yourself:

```javascript
import { toBeEmpty, toHaveTextContent } from '@testing-library/jest-native';
import { toBeEmptyElement, toHaveTextContent } from '@testing-library/jest-native';

expect.extend({ toBeEmpty, toHaveTextContent });
expect.extend({ toBeEmptyElement, toHaveTextContent });
```

## Matchers
Expand Down Expand Up @@ -160,10 +160,10 @@ expect(getByTestId('button')).toBeEnabled();
expect(getByTestId('input')).toBeEnabled();
```

### `toBeEmpty`
### `toBeEmptyElement`

```javascript
toBeEmpty();
toBeEmptyElement();
```

Check that the given element has no content.
Expand All @@ -173,9 +173,19 @@ Check that the given element has no content.
```javascript
const { getByTestId } = render(<View testID="empty" />);

expect(getByTestId('empty')).toBeEmpty();
expect(getByTestId('empty')).toBeEmptyElement();
```

joaoGabriel55 marked this conversation as resolved.
Show resolved Hide resolved
---

**NOTE**

`toBeEmptyElement()` matcher has been renamed from `toBeEmpty()` because of the naming conflict with
Jest Extended export with the
[same name](https://github.com/jest-community/jest-extended#tobeempty).

---

### `toContainElement`

```typescript
Expand Down
5 changes: 4 additions & 1 deletion extend-expect.d.ts
Expand Up @@ -6,11 +6,14 @@ declare global {
interface Matchers<R, T> {
toBeDisabled(): R;
toContainElement(element: ReactTestInstance | null): R;
toBeEmpty(): R;
toBeEmptyElement(): R;
toHaveProp(attr: string, value?: any): R;
toHaveTextContent(text: string | RegExp, options?: { normalizeWhitespace: boolean }): R;
toBeEnabled(): R;
toHaveStyle(style: object[] | object): R;

/** @deprecated This function has been renamed to `toBeEmptyElement`. */
toBeEmpty(): R;
}
}
}
30 changes: 30 additions & 0 deletions src/__tests__/to-be-empty-element.js
@@ -0,0 +1,30 @@
import React from 'react';
import { View } from 'react-native';
import { render } from '@testing-library/react-native';

test('.toBeEmptyElement', () => {
const { queryByTestId } = render(
<View testID="not-empty">
<View testID="empty" />
</View>,
);

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

expect(empty).toBeEmptyElement();
expect(notEmpty).not.toBeEmptyElement();

// negative test cases wrapped in throwError assertions for coverage.
expect(() => expect(empty).not.toBeEmptyElement()).toThrow();

expect(() => expect(notEmpty).toBeEmptyElement()).toThrow();

expect(() => expect(fakeElement).toBeEmptyElement()).toThrow();

expect(() => {
expect(nonExistantElement).toBeEmptyElement();
}).toThrow();
});
5 changes: 3 additions & 2 deletions src/index.js
@@ -1,5 +1,5 @@
import { toBeDisabled, toBeEnabled } from './to-be-disabled';
import { toBeEmpty } from './to-be-empty';
import { toBeEmptyElement, toBeEmpty } from './to-be-empty-element';
import { toHaveProp } from './to-have-prop';
import { toHaveTextContent } from './to-have-text-content';
import { toContainElement } from './to-contain-element';
Expand All @@ -8,7 +8,8 @@ import { toHaveStyle } from './to-have-style';
export {
toBeDisabled,
toContainElement,
toBeEmpty,
toBeEmptyElement,
joaoGabriel55 marked this conversation as resolved.
Show resolved Hide resolved
toBeEmpty, // Deprecated
toHaveProp,
toHaveTextContent,
toBeEnabled,
Expand Down
30 changes: 30 additions & 0 deletions src/to-be-empty-element.js
@@ -0,0 +1,30 @@
import { matcherHint } from 'jest-matcher-utils';
import { checkReactElement, isEmpty, printDeprecationWarning, printElement } from './utils';

export function toBeEmptyElement(element) {
checkReactElement(element, toBeEmptyElement, this);

return {
pass: isEmpty(element?.props?.children),
message: () => {
return [
matcherHint(`${this.isNot ? '.not' : ''}.toBeEmpty`, 'element', ''),
'',
'Received:',
printElement(element),
].join('\n');
},
};
}

/**
* @deprecated This function has been renamed to `toBeEmptyElement`.
*
* */
export function toBeEmpty(element) {
printDeprecationWarning(
'toBeEmpty',
`"toBeEmpty()" matcher has been renamed to "toBeEmptyElement()". Old name will be deleted in future versions of @testing-library/jest-native.`,
);
return toBeEmptyElement(element);
}
18 changes: 0 additions & 18 deletions src/to-be-empty.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/utils.js
Expand Up @@ -109,6 +109,18 @@ function isEmpty(value) {
return false;
}

const warned = {};

export function printDeprecationWarning(functionName, message) {
if (warned[functionName]) {
return;
}

// eslint-disable-next-line no-console
console.warn(`Deprecation Warning:\n${message}`);
warned[functionName] = true;
}

export {
ReactElementTypeError,
checkReactElement,
Expand Down