-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
toBeEmptyElement
matcher (#1462)
* feat: implement toBeEmptyElement * refactor: tweak formatting and tests --------- Co-authored-by: Maciej Jastrzebski <[email protected]>
- Loading branch information
1 parent
4148038
commit cf08c06
Showing
7 changed files
with
114 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function DoNotRenderChildren({ children }: { children: React.ReactNode }) { | ||
// Intentionally do not render children. | ||
return null; | ||
} | ||
|
||
test('toBeEmptyElement()', () => { | ||
render( | ||
<View testID="not-empty"> | ||
<View testID="empty" /> | ||
</View> | ||
); | ||
|
||
const empty = screen.getByTestId('empty'); | ||
expect(empty).toBeEmptyElement(); | ||
expect(() => expect(empty).not.toBeEmptyElement()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeEmptyElement() | ||
Received: | ||
(no elements)" | ||
`); | ||
|
||
const notEmpty = screen.getByTestId('not-empty'); | ||
expect(notEmpty).not.toBeEmptyElement(); | ||
expect(() => expect(notEmpty).toBeEmptyElement()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeEmptyElement() | ||
Received: | ||
<View | ||
testID="empty" | ||
/>" | ||
`); | ||
}); | ||
|
||
test('toBeEmptyElement() ignores composite-only children', () => { | ||
render( | ||
<View testID="view"> | ||
<DoNotRenderChildren> | ||
<View testID="not-rendered" /> | ||
</DoNotRenderChildren> | ||
</View> | ||
); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(view).toBeEmptyElement(); | ||
expect(() => expect(view).not.toBeEmptyElement()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeEmptyElement() | ||
Received: | ||
(no elements)" | ||
`); | ||
}); | ||
|
||
test('toBeEmptyElement() on null element', () => { | ||
expect(() => { | ||
expect(null).toBeEmptyElement(); | ||
}).toThrowErrorMatchingInlineSnapshot(` | ||
"expect(received).toBeEmptyElement() | ||
received value must be a host element. | ||
Received has value: null" | ||
`); | ||
}); |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
/// <reference path="./extend-expect.d.ts" /> | ||
|
||
import { toBeOnTheScreen } from './to-be-on-the-screen'; | ||
import { toBeEmptyElement } from './to-be-empty-element'; | ||
|
||
expect.extend({ | ||
toBeOnTheScreen, | ||
toBeEmptyElement, | ||
}); |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { toBeOnTheScreen } from './to-be-on-the-screen'; | ||
export { toBeEmptyElement } from './to-be-empty-element'; |
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,29 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils'; | ||
import { getHostChildren } from '../helpers/component-tree'; | ||
import { checkHostElement, formatElementArray } from './utils'; | ||
|
||
export function toBeEmptyElement( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance | ||
) { | ||
checkHostElement(element, toBeEmptyElement, this); | ||
|
||
const hostChildren = getHostChildren(element); | ||
|
||
return { | ||
pass: hostChildren.length === 0, | ||
message: () => { | ||
return [ | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toBeEmptyElement`, | ||
'element', | ||
'' | ||
), | ||
'', | ||
'Received:', | ||
`${RECEIVED_COLOR(formatElementArray(hostChildren))}`, | ||
].join('\n'); | ||
}, | ||
}; | ||
} |
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