forked from testing-library/jest-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add toBeEmptyElement assertion (testing-library#111)
- Loading branch information
1 parent
044229b
commit d38fc80
Showing
7 changed files
with
95 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters