-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: .toBeVisible error with Pressable function style (#134)
- Loading branch information
Showing
5 changed files
with
139 additions
and
18 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,52 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
import { getParentElement } from '../component-tree'; | ||
|
||
function MultipleHostChildren() { | ||
return ( | ||
<> | ||
<View testID="child1" /> | ||
<View testID="child2" /> | ||
<View testID="child3" /> | ||
</> | ||
); | ||
} | ||
|
||
describe('getParentElement()', () => { | ||
it('returns host parent for host component', () => { | ||
const view = render( | ||
<View testID="grandparent"> | ||
<View testID="parent"> | ||
<View testID="subject" /> | ||
<View testID="sibling" /> | ||
</View> | ||
</View>, | ||
); | ||
|
||
const hostParent = getParentElement(view.getByTestId('subject')); | ||
expect(hostParent).toBe(view.getByTestId('parent')); | ||
|
||
const hostGrandparent = getParentElement(hostParent); | ||
expect(hostGrandparent).toBe(view.getByTestId('grandparent')); | ||
|
||
expect(getParentElement(hostGrandparent)).toBe(null); | ||
}); | ||
|
||
it('returns host parent for null', () => { | ||
expect(getParentElement(null)).toBe(null); | ||
}); | ||
|
||
it('returns host parent for composite component', () => { | ||
const view = render( | ||
<View testID="parent"> | ||
<MultipleHostChildren /> | ||
<View testID="subject" /> | ||
</View>, | ||
); | ||
|
||
const compositeComponent = view.UNSAFE_getByType(MultipleHostChildren); | ||
const hostParent = getParentElement(compositeComponent); | ||
expect(hostParent).toBe(view.getByTestId('parent')); | ||
}); | ||
}); |
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,37 @@ | ||
import type React from 'react'; | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
|
||
/** | ||
* Checks if the given element is a host element. | ||
* @param element The element to check. | ||
*/ | ||
export function isHostElement(element?: ReactTestInstance | null): boolean { | ||
return typeof element?.type === 'string'; | ||
} | ||
|
||
/** | ||
* Returns first host ancestor for given element or first ancestor of one of | ||
* passed component types. | ||
* | ||
* @param element The element start traversing from. | ||
* @param componentTypes Additional component types to match. | ||
*/ | ||
export function getParentElement( | ||
element: ReactTestInstance | null, | ||
componentTypes: React.ElementType[] = [], | ||
): ReactTestInstance | null { | ||
if (element == null) { | ||
return null; | ||
} | ||
|
||
let current = element.parent; | ||
while (current) { | ||
if (isHostElement(current) || componentTypes.includes(current.type)) { | ||
return current; | ||
} | ||
|
||
current = current.parent; | ||
} | ||
|
||
return 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