-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: implement toHaveProp * refactor: use screen in the test * refactor: tweaks * refactor: tweaks * refactor: final polishing * refactor: cleanup --------- Co-authored-by: Maciej Jastrzebski <mdjastrzebski@gmail.com>
- Loading branch information
1 parent
4659bba
commit 2d96b77
Showing
9 changed files
with
162 additions
and
16 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
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
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,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 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,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 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 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