-
Notifications
You must be signed in to change notification settings - Fork 276
feature: Jest matchers core #1454
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9caa54a
chore: jest matchers.
mdjastrzebski 9bd4527
refactor: cleanup
mdjastrzebski 67b2402
chore: test for not exposing matchers by default
mdjastrzebski b8120b3
chore: cleanup
mdjastrzebski 5cfaee5
refactor: finishing touches
mdjastrzebski c5cd4b2
chore: improve code cov
mdjastrzebski 6af95f8
Update package.json
mdjastrzebski b880246
refactor: code review changes
mdjastrzebski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
import * as React from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
// Note: that must point to root of the /src to reliably replicate default import. | ||
import { render } from '../..'; | ||
|
||
// This is check that RNTL does not extend "expect" by default, until we actually want to expose Jest matchers publically. | ||
test('does not extend "expect" by default', () => { | ||
render(<View />); | ||
|
||
// @ts-expect-error | ||
expect(expect.toBeOnTheScreen).toBeUndefined(); | ||
}); |
This file contains hidden or 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,72 @@ | ||
import * as React from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('example test', () => { | ||
render( | ||
<View> | ||
<View testID="child" /> | ||
</View> | ||
); | ||
|
||
const child = screen.getByTestId('child'); | ||
expect(child).toBeOnTheScreen(); | ||
|
||
screen.update(<View />); | ||
expect(child).not.toBeOnTheScreen(); | ||
}); | ||
|
||
test('toBeOnTheScreen() on attached element', () => { | ||
render(<View testID="test" />); | ||
|
||
const element = screen.getByTestId('test'); | ||
expect(element).toBeOnTheScreen(); | ||
expect(() => expect(element).not.toBeOnTheScreen()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toBeOnTheScreen() | ||
|
||
expected element tree not to contain element, but found | ||
<View | ||
testID="test" | ||
/>" | ||
`); | ||
}); | ||
|
||
function ShowChildren({ show }: { show: boolean }) { | ||
return show ? ( | ||
<View> | ||
<Text testID="text">Hello</Text> | ||
</View> | ||
) : ( | ||
<View /> | ||
); | ||
} | ||
|
||
test('toBeOnTheScreen() on detached element', () => { | ||
render(<ShowChildren show={true} />); | ||
|
||
const element = screen.getByTestId('text'); | ||
// Next line will unmount the element, yet `element` variable will still hold reference to it. | ||
screen.update(<ShowChildren show={false} />); | ||
|
||
expect(element).toBeTruthy(); | ||
expect(element).not.toBeOnTheScreen(); | ||
expect(() => expect(element).toBeOnTheScreen()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toBeOnTheScreen() | ||
|
||
element could not be found in the element tree" | ||
`); | ||
}); | ||
|
||
test('toBeOnTheScreen() on null element', () => { | ||
expect(null).not.toBeOnTheScreen(); | ||
expect(() => expect(null).toBeOnTheScreen()) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(received).toBeOnTheScreen() | ||
|
||
received value must be a host element. | ||
Received has value: null" | ||
`); | ||
}); |
This file contains hidden or 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,42 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { render } from '../..'; | ||
import { formatElement, checkHostElement } from '../utils'; | ||
|
||
function fakeMatcher() { | ||
// Do nothing. | ||
} | ||
|
||
test('formatElement', () => { | ||
expect(formatElement(null)).toMatchInlineSnapshot(`"null"`); | ||
}); | ||
|
||
test('checkHostElement allows host element', () => { | ||
const screen = render(<View testID="view" />); | ||
|
||
expect(() => { | ||
// @ts-expect-error | ||
checkHostElement(screen.getByTestId('view'), fakeMatcher, {}); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('checkHostElement allows rejects composite element', () => { | ||
const screen = render(<View testID="view" />); | ||
|
||
expect(() => { | ||
// @ts-expect-error | ||
checkHostElement(screen.UNSAFE_root, fakeMatcher, {}); | ||
}).toThrow(/value must be a host element./); | ||
}); | ||
|
||
test('checkHostElement allows rejects null element', () => { | ||
expect(() => { | ||
// @ts-expect-error | ||
checkHostElement(null, fakeMatcher, {}); | ||
}).toThrowErrorMatchingInlineSnapshot(` | ||
"expect(received).fakeMatcher() | ||
|
||
received value must be a host element. | ||
Received has value: null" | ||
`); | ||
}); |
This file contains hidden or 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,5 @@ | ||
import { toBeOnTheScreen } from './to-be-on-the-screen'; | ||
|
||
expect.extend({ | ||
toBeOnTheScreen, | ||
}); |
This file contains hidden or 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 @@ | ||
export { toBeOnTheScreen } from './to-be-on-the-screen'; |
This file contains hidden or 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,42 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils'; | ||
import { getRootElement } from '../helpers/component-tree'; | ||
import { screen } from '../screen'; | ||
import { checkHostElement, formatElement } from './utils'; | ||
|
||
export function toBeOnTheScreen( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance | ||
) { | ||
if (element !== null || !this.isNot) { | ||
checkHostElement(element, toBeOnTheScreen, this); | ||
} | ||
|
||
const pass = | ||
element === null ? false : screen.UNSAFE_root === getRootElement(element); | ||
|
||
const errorFound = () => { | ||
return `expected element tree not to contain element, but found\n${formatElement( | ||
element | ||
)}`; | ||
}; | ||
|
||
const errorNotFound = () => { | ||
return `element could not be found in the element tree`; | ||
}; | ||
|
||
return { | ||
pass, | ||
message: () => { | ||
return [ | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toBeOnTheScreen`, | ||
'element', | ||
'' | ||
), | ||
'', | ||
RECEIVED_COLOR(this.isNot ? errorFound() : errorNotFound()), | ||
].join('\n'); | ||
}, | ||
}; | ||
} |
This file contains hidden or 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,91 @@ | ||
import { ReactTestInstance } from 'react-test-renderer'; | ||
import { | ||
RECEIVED_COLOR, | ||
matcherHint, | ||
printWithType, | ||
printReceived, | ||
} from 'jest-matcher-utils'; | ||
import prettyFormat, { plugins } from 'pretty-format'; | ||
import redent from 'redent'; | ||
import { isHostElement } from '../helpers/component-tree'; | ||
|
||
class HostElementTypeError extends Error { | ||
constructor( | ||
received: unknown, | ||
matcherFn: jest.CustomMatcher, | ||
context: jest.MatcherContext | ||
) { | ||
super(); | ||
|
||
/* istanbul ignore next */ | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, matcherFn); | ||
} | ||
|
||
let withType = ''; | ||
try { | ||
withType = printWithType('Received', received, printReceived); | ||
/* istanbul ignore next */ | ||
} catch (e) { | ||
// Deliberately empty. | ||
} | ||
|
||
this.message = [ | ||
matcherHint( | ||
`${context.isNot ? '.not' : ''}.${matcherFn.name}`, | ||
'received', | ||
'' | ||
), | ||
'', | ||
`${RECEIVED_COLOR('received')} value must be a host element.`, | ||
withType, | ||
].join('\n'); | ||
} | ||
} | ||
|
||
/** | ||
* Throws HostElementTypeError if passed element is not a host element. | ||
* | ||
* @param element ReactTestInstance to check. | ||
* @param matcherFn Matcher function calling the check used for formatting error. | ||
* @param context Jest matcher context used for formatting error. | ||
*/ | ||
export function checkHostElement( | ||
element: ReactTestInstance | null | undefined, | ||
matcherFn: jest.CustomMatcher, | ||
context: jest.MatcherContext | ||
): asserts element is ReactTestInstance { | ||
if (!isHostElement(element)) { | ||
throw new HostElementTypeError(element, matcherFn, context); | ||
} | ||
} | ||
|
||
/*** | ||
* Format given element as a pretty-printed string. | ||
* | ||
* @param element Element to format. | ||
*/ | ||
export function formatElement(element: ReactTestInstance | null) { | ||
if (element == null) { | ||
return 'null'; | ||
} | ||
|
||
return redent( | ||
prettyFormat( | ||
{ | ||
// This prop is needed persuade the prettyFormat that the element is | ||
// a ReactTestRendererJSON instance, so it is formatted as JSX. | ||
$$typeof: Symbol.for('react.test.json'), | ||
type: element.type, | ||
props: element.props, | ||
}, | ||
{ | ||
plugins: [plugins.ReactTestComponent, plugins.ReactElement], | ||
printFunctionName: false, | ||
printBasicPrototype: false, | ||
highlight: true, | ||
} | ||
), | ||
2 | ||
); | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.