Skip to content

Commit

Permalink
fix: toBeOnTheScreen() with RNTL v12 (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski authored Feb 21, 2023
1 parent 130344b commit d5590df
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ You can check that an already captured element has not been removed from the ele

> **Note**<br/> This matcher requires React Native Testing Library v10.1 or later, as it includes
> the `screen` object.
>
> **Note**<br/> If you're using React Native Testing Library v12 or later, you need to install Jest
> Native v5.4.2 or later.
#### Examples

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@babel/runtime": "^7.18.9",
"@callstack/eslint-config": "^13.0.1",
"@relmify/jest-serializer-strip-ansi": "^1.0.2",
"@testing-library/react-native": "^11.0.0",
"@testing-library/react-native": "^12.0.0-rc.0",
"@types/jest": "^29.0.1",
"@types/react": "^18.0.19",
"@types/react-native": "^0.71.0",
Expand All @@ -52,12 +52,12 @@
"eslint": "^8.21.0",
"husky": "^8.0.1",
"jest": "^29.0.1",
"metro-react-native-babel-preset": "^0.72.3",
"metro-react-native-babel-preset": "0.73.7",
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"react": "18.1.0",
"react-native": "^0.70.6",
"react-test-renderer": "18.1.0",
"react": "18.2.0",
"react-native": "0.71.3",
"react-test-renderer": "18.2.0",
"semantic-release": "^19.0.3",
"typescript": "^4.8.3"
},
Expand Down
16 changes: 8 additions & 8 deletions src/__tests__/to-be-visible.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('.toBeVisible', () => {

test('handles view with display "none"', () => {
const { getByTestId } = render(<View testID="test" style={{ display: 'none' }} />);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles ancestor view with 0 opacity', () => {
Expand All @@ -31,7 +31,7 @@ describe('.toBeVisible', () => {
</View>
</View>,
);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles ancestor view with display "none"', () => {
Expand All @@ -42,7 +42,7 @@ describe('.toBeVisible', () => {
</View>
</View>,
);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles empty modal', () => {
Expand Down Expand Up @@ -77,12 +77,12 @@ describe('.toBeVisible', () => {

test('handles not visible modal', () => {
const { getByTestId } = render(<Modal testID="test" visible={false} />);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles inaccessible view (iOS)', () => {
const { getByTestId, update } = render(<View testID="test" accessibilityElementsHidden />);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();

update(<View testID="test" accessibilityElementsHidden={false} />);
expect(getByTestId('test')).toBeVisible();
Expand All @@ -96,14 +96,14 @@ describe('.toBeVisible', () => {
</View>
</View>,
);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles inaccessible view (Android)', () => {
const { getByTestId, update } = render(
<View testID="test" importantForAccessibility="no-hide-descendants" />,
);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();

update(<View testID="test" importantForAccessibility="auto" />);
expect(getByTestId('test')).toBeVisible();
Expand All @@ -117,7 +117,7 @@ describe('.toBeVisible', () => {
</View>
</View>,
);
expect(getByTestId('test')).not.toBeVisible();
expect(getByTestId('test', { includeHiddenElements: true })).not.toBeVisible();
});

test('handles null elements', () => {
Expand Down
22 changes: 11 additions & 11 deletions src/__tests__/to-have-text-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('.toHaveTextContent', () => {
test('can handle multiple levels with no explicit children prop', () => {
const NoChildren = ({ text }: { text: string }) => <Text>{text}</Text>;
const answer = 'Answer';
const { container } = render(
const { root } = render(
<View>
<Text>
{answer}
Expand All @@ -86,27 +86,27 @@ describe('.toHaveTextContent', () => {
</View>,
);

expect(container).toHaveTextContent(/^Answer: 42$/);
expect(root).toHaveTextContent(/^Answer: 42$/);
});

test('throws when no match is found', () => {
const { container } = render(<Text>Should succeed</Text>);
const { root } = render(<Text>Should succeed</Text>);

expect(() => {
expect(container).toHaveTextContent('Should fail');
expect(root).toHaveTextContent('Should fail');
}).toThrow();
});

test('does not throw error with empty content', () => {
const { container } = render(<Text />);
expect(container).toHaveTextContent('');
const { root } = render(<Text />);
expect(root).toHaveTextContent('');
});

test('is case-sensitive', () => {
const { container } = render(<Text>Sensitive text</Text>);
const { root } = render(<Text>Sensitive text</Text>);

expect(container).toHaveTextContent('Sensitive text');
expect(container).not.toHaveTextContent('sensitive text');
expect(root).toHaveTextContent('Sensitive text');
expect(root).not.toHaveTextContent('sensitive text');
});

test('can handle conditional rendering', () => {
Expand All @@ -124,8 +124,8 @@ describe('.toHaveTextContent', () => {

test('can handle text with an interpolated variable', () => {
const variable = 'variable';
const { container } = render(<Text>With a {variable}</Text>);
const { root } = render(<Text>With a {variable}</Text>);

expect(container).toHaveTextContent('With a variable');
expect(root).toHaveTextContent('With a variable');
});
});
6 changes: 3 additions & 3 deletions src/to-be-on-the-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function toBeOnTheScreen(this: jest.MatcherContext, element: ReactTestIns
checkReactElement(element, toBeOnTheScreen, this);
}

const pass = element === null ? false : getScreen().container === getRootElement(element);
const pass = element === null ? false : getScreenRoot() === getRootElement(element);

const errorFound = () => {
return `expected element tree not to contain element but found:\n${printElement(element)}`;
Expand Down Expand Up @@ -37,15 +37,15 @@ function getRootElement(element: ReactTestInstance) {
return root;
}

function getScreen() {
function getScreenRoot() {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
const { screen } = require('@testing-library/react-native');
if (!screen) {
throw new Error('screen is undefined');
}

return screen;
return screen.UNSAFE_root ?? screen.container;
} catch (error) {
throw new Error(
'Could not import `screen` object from @testing-library/react-native.\n\n' +
Expand Down

0 comments on commit d5590df

Please sign in to comment.