Skip to content

Commit c958c7b

Browse files
committed
refactor: tweaks
1 parent 786fb07 commit c958c7b

File tree

3 files changed

+34
-40
lines changed

3 files changed

+34
-40
lines changed

src/matchers/__tests__/to-have-prop.test.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,25 @@ test('.toHaveProp', () => {
3131
// title is no longer findable as it is a React child
3232
expect(() => expect(button).toHaveProp('title', 'ok'))
3333
.toThrowErrorMatchingInlineSnapshot(`
34-
"expect(element).toHaveProp("title", "ok") // Element should have prop title with value "ok"
34+
"expect(element).toHaveProp("title", "ok")
3535
3636
Expected the element to have prop:
3737
title="ok"
3838
Received:
39-
null"
39+
undefined"
4040
`);
4141
expect(() => expect(button).toHaveProp('disabled'))
4242
.toThrowErrorMatchingInlineSnapshot(`
43-
"expect(element).toHaveProp("disabled") // Element should have prop disabled
43+
"expect(element).toHaveProp("disabled")
4444
4545
Expected the element to have prop:
4646
disabled
4747
Received:
48-
null"
48+
undefined"
4949
`);
5050
expect(() => expect(text).not.toHaveProp('allowFontScaling', false))
5151
.toThrowErrorMatchingInlineSnapshot(`
52-
"expect(element).not.toHaveProp("allowFontScaling", false) // Element should have prop allowFontScaling with value false
52+
"expect(element).not.toHaveProp("allowFontScaling", false)
5353
5454
Expected the element not to have prop:
5555
allowFontScaling=false
@@ -58,16 +58,16 @@ test('.toHaveProp', () => {
5858
`);
5959
expect(() => expect(text).toHaveProp('style'))
6060
.toThrowErrorMatchingInlineSnapshot(`
61-
"expect(element).toHaveProp("style") // Element should have prop style
61+
"expect(element).toHaveProp("style")
6262
6363
Expected the element to have prop:
6464
style
6565
Received:
66-
null"
66+
undefined"
6767
`);
6868
expect(() => expect(text).toHaveProp('allowFontScaling', 'wrongValue'))
6969
.toThrowErrorMatchingInlineSnapshot(`
70-
"expect(element).toHaveProp("allowFontScaling", "wrongValue") // Element should have prop allowFontScaling with value "wrongValue"
70+
"expect(element).toHaveProp("allowFontScaling", "wrongValue")
7171
7272
Expected the element to have prop:
7373
allowFontScaling="wrongValue"

src/matchers/to-have-prop.ts

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@ import type { ReactTestInstance } from 'react-test-renderer';
22
import { matcherHint, stringify, printExpected } from 'jest-matcher-utils';
33
import { checkHostElement, formatMessage } from './utils';
44

5-
function printAttribute(name: string, value: unknown) {
6-
return value === undefined ? name : `${name}=${stringify(value)}`;
7-
}
8-
9-
function getPropComment(name: string, value: unknown) {
10-
return value === undefined
11-
? `Element should have prop ${name}`
12-
: `Element should have prop ${name} with value ${stringify(value)}`;
13-
}
14-
155
export function toHaveProp(
166
this: jest.MatcherContext,
177
element: ReactTestInstance,
@@ -20,35 +10,39 @@ export function toHaveProp(
2010
) {
2111
checkHostElement(element, toHaveProp, this);
2212

23-
const propValue = element.props[name];
24-
25-
const isDefined = expectedValue !== undefined;
2613
const hasProp = name in element.props;
14+
const value = element.props[name];
15+
16+
const pass =
17+
expectedValue !== undefined
18+
? hasProp && this.equals(expectedValue, value)
19+
: hasProp;
2720

2821
return {
29-
pass: isDefined
30-
? hasProp && this.equals(propValue, expectedValue)
31-
: hasProp,
22+
pass,
3223
message: () => {
3324
const to = this.isNot ? 'not to' : 'to';
34-
35-
const receivedProp = hasProp ? printAttribute(name, propValue) : null;
36-
const matcher = matcherHint(
37-
`${this.isNot ? '.not' : ''}.toHaveProp`,
38-
'element',
39-
printExpected(name),
40-
{
41-
secondArgument: isDefined ? printExpected(expectedValue) : undefined,
42-
comment: getPropComment(name, expectedValue),
43-
}
44-
);
4525
return formatMessage(
46-
matcher,
47-
`Expected the element ${to} have prop`,
48-
printAttribute(name, expectedValue),
26+
matcherHint(
27+
`${this.isNot ? '.not' : ''}.toHaveProp`,
28+
'element',
29+
printExpected(name),
30+
{
31+
secondArgument:
32+
expectedValue !== undefined
33+
? printExpected(expectedValue)
34+
: undefined,
35+
}
36+
),
37+
`Expected element ${to} have prop`,
38+
formatProp(name, expectedValue),
4939
'Received',
50-
receivedProp
40+
hasProp ? formatProp(name, value) : undefined
5141
);
5242
},
5343
};
5444
}
45+
46+
function formatProp(name: string, value: unknown) {
47+
return value === undefined ? name : `${name}=${stringify(value)}`;
48+
}

src/matchers/utils.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export function formatMessage(
111111
expectedLabel: string,
112112
expectedValue: string | RegExp,
113113
receivedLabel: string,
114-
receivedValue: string | null
114+
receivedValue: string | null | undefined
115115
) {
116116
return [
117117
`${matcher}\n`,

0 commit comments

Comments
 (0)