-
Notifications
You must be signed in to change notification settings - Fork 277
feat: toHaveProp
matcher
#1477
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
feat: toHaveProp
matcher
#1477
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
015b96e
feat: implement toHaveProp
AntoineThibi 786fb07
refactor: use screen in the test
AntoineThibi c958c7b
refactor: tweaks
mdjastrzebski f539a62
refactor: tweaks
mdjastrzebski 5ecf177
refactor: final polishing
mdjastrzebski e7e4589
refactor: cleanup
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React from 'react'; | ||
import { View, Text, TextInput } from 'react-native'; | ||
import { render, screen } from '../..'; | ||
import '../extend-expect'; | ||
|
||
test('.toHaveProp basic case', () => { | ||
render( | ||
<View testID="view" style={null}> | ||
<Text ellipsizeMode="head">Hello</Text> | ||
<TextInput testID="input" textAlign="right" /> | ||
</View> | ||
); | ||
|
||
const view = screen.getByTestId('view'); | ||
expect(view).toHaveProp('style'); | ||
expect(view).toHaveProp('style', null); | ||
expect(view).not.toHaveProp('ellipsizeMode'); | ||
|
||
const text = screen.getByText('Hello'); | ||
expect(text).toHaveProp('ellipsizeMode'); | ||
expect(text).toHaveProp('ellipsizeMode', 'head'); | ||
expect(text).not.toHaveProp('style'); | ||
expect(text).not.toHaveProp('ellipsizeMode', 'tail'); | ||
|
||
const input = screen.getByTestId('input'); | ||
expect(input).toHaveProp('textAlign'); | ||
expect(input).toHaveProp('textAlign', 'right'); | ||
expect(input).not.toHaveProp('textAlign', 'left'); | ||
expect(input).not.toHaveProp('editable'); | ||
expect(input).not.toHaveProp('editable', false); | ||
}); | ||
|
||
test('.toHaveProp error messages', () => { | ||
render(<View testID="view" collapsable={false} />); | ||
|
||
const view = screen.getByTestId('view'); | ||
|
||
expect(() => expect(view).toHaveProp('accessible')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveProp("accessible") | ||
|
||
Expected element to have prop: | ||
accessible | ||
Received: | ||
undefined" | ||
`); | ||
|
||
expect(() => expect(view).toHaveProp('accessible', true)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveProp("accessible", true) | ||
|
||
Expected element to have prop: | ||
accessible={true} | ||
Received: | ||
undefined" | ||
`); | ||
|
||
expect(() => expect(view).not.toHaveProp('collapsable')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toHaveProp("collapsable") | ||
|
||
Expected element not to have prop: | ||
collapsable | ||
Received: | ||
collapsable={false}" | ||
`); | ||
|
||
expect(() => expect(view).toHaveProp('collapsable', true)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).toHaveProp("collapsable", true) | ||
|
||
Expected element to have prop: | ||
collapsable={true} | ||
Received: | ||
collapsable={false}" | ||
`); | ||
|
||
expect(() => expect(view).not.toHaveProp('collapsable', false)) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"expect(element).not.toHaveProp("collapsable", false) | ||
|
||
Expected element not to have prop: | ||
collapsable={false} | ||
Received: | ||
collapsable={false}" | ||
`); | ||
}); |
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 |
---|---|---|
@@ -1,18 +1,20 @@ | ||
/// <reference path="./extend-expect.d.ts" /> | ||
|
||
import { toBeOnTheScreen } from './to-be-on-the-screen'; | ||
import { toBeDisabled, toBeEnabled } from './to-be-disabled'; | ||
import { toBeEmptyElement } from './to-be-empty-element'; | ||
import { toBeVisible } from './to-be-visible'; | ||
import { toHaveDisplayValue } from './to-have-display-value'; | ||
import { toHaveProp } from './to-have-prop'; | ||
import { toHaveTextContent } from './to-have-text-content'; | ||
import { toBeDisabled, toBeEnabled } from './to-be-disabled'; | ||
|
||
expect.extend({ | ||
toBeOnTheScreen, | ||
toBeDisabled, | ||
toBeEmptyElement, | ||
toBeEnabled, | ||
toBeVisible, | ||
toHaveDisplayValue, | ||
toHaveProp, | ||
toHaveTextContent, | ||
toBeDisabled, | ||
toBeEnabled, | ||
}); |
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,56 @@ | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint, stringify, printExpected } from 'jest-matcher-utils'; | ||
import { checkHostElement, formatMessage } from './utils'; | ||
|
||
export function toHaveProp( | ||
this: jest.MatcherContext, | ||
element: ReactTestInstance, | ||
name: string, | ||
expectedValue: unknown | ||
) { | ||
checkHostElement(element, toHaveProp, this); | ||
|
||
const isExpectedValueDefined = expectedValue !== undefined; | ||
const hasProp = name in element.props; | ||
const receivedValue = element.props[name]; | ||
|
||
const pass = isExpectedValueDefined | ||
? hasProp && this.equals(expectedValue, receivedValue) | ||
: hasProp; | ||
|
||
return { | ||
pass, | ||
message: () => { | ||
const to = this.isNot ? 'not to' : 'to'; | ||
const matcher = matcherHint( | ||
`${this.isNot ? '.not' : ''}.toHaveProp`, | ||
'element', | ||
printExpected(name), | ||
{ | ||
secondArgument: isExpectedValueDefined | ||
? printExpected(expectedValue) | ||
: undefined, | ||
} | ||
); | ||
return formatMessage( | ||
matcher, | ||
`Expected element ${to} have prop`, | ||
formatProp(name, expectedValue), | ||
'Received', | ||
hasProp ? formatProp(name, receivedValue) : undefined | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
function formatProp(name: string, value: unknown) { | ||
if (value === undefined) { | ||
return name; | ||
} | ||
|
||
if (typeof value === 'string') { | ||
return `${name}="${value}"`; | ||
} | ||
|
||
return `${name}={${stringify(value)}}`; | ||
} |
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.