Skip to content

Commit

Permalink
feat: to have display value
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjastrzebski committed Aug 19, 2023
1 parent 4a8072d commit d779037
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/matchers/to-have-display-value.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { ReactTestInstance } from 'react-test-renderer';
import { matcherHint } from 'jest-matcher-utils';
import { isHostTextInput } from '../helpers/host-component-names';
import { ErrorWithStack } from '../helpers/errors';
import { TextMatch, TextMatchOptions, matches } from '../matches';
import { checkHostElement, formatMessage } from './utils';

export function toHaveDisplayValue(
this: jest.MatcherContext,
element: ReactTestInstance,
expectedValue: TextMatch,
options?: TextMatchOptions
) {
checkHostElement(element, toHaveDisplayValue, this);

if (!isHostTextInput(element)) {
throw new ErrorWithStack(
`toHaveDisplayValue() works only with host "TextInput" elements. Passed element has type "${element.type}".`,
toHaveDisplayValue
);
}

const value = element.props.value ?? element.props.defaultValue;

return {
pass: matches(expectedValue, value, options?.normalizer, options?.exact),
message: () => {
return [
formatMessage(
matcherHint(
`${this.isNot ? '.not' : ''}.toHaveDisplayValue`,
'element',
''
),
`Expected element ${this.isNot ? 'not to' : 'to'} have display value`,
expectedValue,
'Received',
value
),
].join('\n');
},
};
}

0 comments on commit d779037

Please sign in to comment.