Skip to content

Commit d779037

Browse files
committed
feat: to have display value
1 parent 4a8072d commit d779037

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import type { ReactTestInstance } from 'react-test-renderer';
2+
import { matcherHint } from 'jest-matcher-utils';
3+
import { isHostTextInput } from '../helpers/host-component-names';
4+
import { ErrorWithStack } from '../helpers/errors';
5+
import { TextMatch, TextMatchOptions, matches } from '../matches';
6+
import { checkHostElement, formatMessage } from './utils';
7+
8+
export function toHaveDisplayValue(
9+
this: jest.MatcherContext,
10+
element: ReactTestInstance,
11+
expectedValue: TextMatch,
12+
options?: TextMatchOptions
13+
) {
14+
checkHostElement(element, toHaveDisplayValue, this);
15+
16+
if (!isHostTextInput(element)) {
17+
throw new ErrorWithStack(
18+
`toHaveDisplayValue() works only with host "TextInput" elements. Passed element has type "${element.type}".`,
19+
toHaveDisplayValue
20+
);
21+
}
22+
23+
const value = element.props.value ?? element.props.defaultValue;
24+
25+
return {
26+
pass: matches(expectedValue, value, options?.normalizer, options?.exact),
27+
message: () => {
28+
return [
29+
formatMessage(
30+
matcherHint(
31+
`${this.isNot ? '.not' : ''}.toHaveDisplayValue`,
32+
'element',
33+
''
34+
),
35+
`Expected element ${this.isNot ? 'not to' : 'to'} have display value`,
36+
expectedValue,
37+
'Received',
38+
value
39+
),
40+
].join('\n');
41+
},
42+
};
43+
}

0 commit comments

Comments
 (0)