1
1
import {
2
2
matcherHint ,
3
- printReceived ,
4
3
printExpected ,
5
4
stringify ,
6
5
RECEIVED_COLOR as receivedColor ,
@@ -26,9 +25,19 @@ function checkHtmlElement(htmlElement) {
26
25
}
27
26
}
28
27
29
- const assertMessage = ( assertionName , message , received , expected ) =>
30
- `${ matcherHint ( `${ assertionName } ` , 'received' , '' ) } \n${ message } : ` +
31
- `${ printExpected ( expected ) } \nReceived: ${ printReceived ( received ) } `
28
+ function getMessage (
29
+ matcher ,
30
+ expectedLabel ,
31
+ expectedValue ,
32
+ receivedLabel ,
33
+ receivedValue ,
34
+ ) {
35
+ return [
36
+ `${ matcher } \n` ,
37
+ `${ expectedLabel } :\n ${ expectedColor ( expectedValue ) } ` ,
38
+ `${ receivedLabel } :\n ${ receivedColor ( receivedValue ) } ` ,
39
+ ] . join ( '\n' )
40
+ }
32
41
33
42
function printAttribute ( name , value ) {
34
43
return value === undefined ? name : `${ name } =${ stringify ( value ) } `
@@ -42,58 +51,47 @@ function getAttributeComment(name, value) {
42
51
43
52
const extensions = {
44
53
toBeInTheDOM ( received ) {
45
- getDisplayName ( received )
46
54
if ( received ) {
47
- return {
48
- message : ( ) =>
49
- `${ matcherHint (
50
- '.not.toBeInTheDOM' ,
51
- 'received' ,
52
- '' ,
53
- ) } Expected the element not to be present` +
54
- `\nReceived : ${ printReceived ( received ) } ` ,
55
- pass : true ,
56
- }
57
- } else {
58
- return {
59
- message : ( ) =>
60
- `${ matcherHint (
61
- '.toBeInTheDOM' ,
62
- 'received' ,
55
+ checkHtmlElement ( received )
56
+ }
57
+ return {
58
+ pass : ! ! received ,
59
+ message : ( ) => {
60
+ const to = this . isNot ? 'not to' : 'to'
61
+ return getMessage (
62
+ matcherHint (
63
+ `${ this . isNot ? '.not' : '' } .toBeInTheDOM` ,
64
+ 'element' ,
63
65
'' ,
64
- ) } Expected the element to be present` +
65
- `\nReceived : ${ printReceived ( received ) } ` ,
66
- pass : false ,
67
- }
66
+ ) ,
67
+ 'Expected' ,
68
+ `element ${ to } be present` ,
69
+ 'Received' ,
70
+ received ,
71
+ )
72
+ } ,
68
73
}
69
74
} ,
70
75
71
76
toHaveTextContent ( htmlElement , checkWith ) {
72
77
checkHtmlElement ( htmlElement )
73
78
const textContent = htmlElement . textContent
74
- const pass = matches ( textContent , htmlElement , checkWith )
75
- if ( pass ) {
76
- return {
77
- message : ( ) =>
78
- assertMessage (
79
- '.not.toHaveTextContent' ,
80
- 'Expected value not equals to' ,
81
- htmlElement ,
82
- checkWith ,
83
- ) ,
84
- pass : true ,
85
- }
86
- } else {
87
- return {
88
- message : ( ) =>
89
- assertMessage (
90
- '.toHaveTextContent' ,
91
- 'Expected value equals to' ,
92
- htmlElement ,
93
- checkWith ,
79
+ return {
80
+ pass : matches ( textContent , htmlElement , checkWith ) ,
81
+ message : ( ) => {
82
+ const to = this . isNot ? 'not to' : 'to'
83
+ return getMessage (
84
+ matcherHint (
85
+ `${ this . isNot ? '.not' : '' } .toHaveTextContent` ,
86
+ 'element' ,
87
+ '' ,
94
88
) ,
95
- pass : false ,
96
- }
89
+ `Expected element ${ to } have text content` ,
90
+ checkWith ,
91
+ 'Received' ,
92
+ textContent ,
93
+ )
94
+ } ,
97
95
}
98
96
} ,
99
97
@@ -108,14 +106,9 @@ const extensions = {
108
106
: hasAttribute ,
109
107
message : ( ) => {
110
108
const to = this . isNot ? 'not to' : 'to'
111
- const receivedAttribute = receivedColor (
112
- hasAttribute
113
- ? printAttribute ( name , receivedValue )
114
- : 'attribute was not found' ,
115
- )
116
- const expectedMsg = `Expected the element ${ to } have attribute:\n ${ expectedColor (
117
- printAttribute ( name , expectedValue ) ,
118
- ) } `
109
+ const receivedAttribute = hasAttribute
110
+ ? printAttribute ( name , receivedValue )
111
+ : null
119
112
const matcher = matcherHint (
120
113
`${ this . isNot ? '.not' : '' } .toHaveAttribute` ,
121
114
'element' ,
@@ -127,7 +120,13 @@ const extensions = {
127
120
comment : getAttributeComment ( name , expectedValue ) ,
128
121
} ,
129
122
)
130
- return `${ matcher } \n\n${ expectedMsg } \nReceived:\n ${ receivedAttribute } `
123
+ return getMessage (
124
+ matcher ,
125
+ `Expected the element ${ to } have attribute` ,
126
+ printAttribute ( name , expectedValue ) ,
127
+ 'Received' ,
128
+ receivedAttribute ,
129
+ )
131
130
} ,
132
131
}
133
132
} ,
0 commit comments