Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ test('using jest helpers to assert element states', () => {
expect(() =>
expect(queryByTestId('count-value')).not.toHaveTextContent('2'),
).toThrowError()

expect(() =>
expect({thisIsNot: 'an html element'}).toBeInTheDOM(),
).toThrowError()
})

test('using jest helpers to check element attributes', () => {
Expand Down
111 changes: 55 additions & 56 deletions src/jest-extensions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
matcherHint,
printReceived,
printExpected,
stringify,
RECEIVED_COLOR as receivedColor,
Expand All @@ -26,9 +25,19 @@ function checkHtmlElement(htmlElement) {
}
}

const assertMessage = (assertionName, message, received, expected) =>
`${matcherHint(`${assertionName}`, 'received', '')} \n${message}: ` +
`${printExpected(expected)} \nReceived: ${printReceived(received)}`
function getMessage(
matcher,
expectedLabel,
expectedValue,
receivedLabel,
receivedValue,
) {
return [
`${matcher}\n`,
`${expectedLabel}:\n ${expectedColor(expectedValue)}`,
`${receivedLabel}:\n ${receivedColor(receivedValue)}`,
].join('\n')
}

function printAttribute(name, value) {
return value === undefined ? name : `${name}=${stringify(value)}`
Expand All @@ -42,58 +51,47 @@ function getAttributeComment(name, value) {

const extensions = {
toBeInTheDOM(received) {
getDisplayName(received)
if (received) {
return {
message: () =>
`${matcherHint(
'.not.toBeInTheDOM',
'received',
'',
)} Expected the element not to be present` +
`\nReceived : ${printReceived(received)}`,
pass: true,
}
} else {
return {
message: () =>
`${matcherHint(
'.toBeInTheDOM',
'received',
checkHtmlElement(received)
}
return {
pass: !!received,
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
matcherHint(
`${this.isNot ? '.not' : ''}.toBeInTheDOM`,
'element',
'',
)} Expected the element to be present` +
`\nReceived : ${printReceived(received)}`,
pass: false,
}
),
'Expected',
`element ${to} be present`,
'Received',
received,
)
},
}
},

toHaveTextContent(htmlElement, checkWith) {
checkHtmlElement(htmlElement)
const textContent = htmlElement.textContent
const pass = matches(textContent, htmlElement, checkWith)
if (pass) {
return {
message: () =>
assertMessage(
'.not.toHaveTextContent',
'Expected value not equals to',
htmlElement,
checkWith,
),
pass: true,
}
} else {
return {
message: () =>
assertMessage(
'.toHaveTextContent',
'Expected value equals to',
htmlElement,
checkWith,
return {
pass: matches(textContent, htmlElement, checkWith),
message: () => {
const to = this.isNot ? 'not to' : 'to'
return getMessage(
matcherHint(
`${this.isNot ? '.not' : ''}.toHaveTextContent`,
'element',
'',
),
pass: false,
}
`Expected element ${to} have text content`,
checkWith,
'Received',
textContent,
)
},
}
},

Expand All @@ -108,14 +106,9 @@ const extensions = {
: hasAttribute,
message: () => {
const to = this.isNot ? 'not to' : 'to'
const receivedAttribute = receivedColor(
hasAttribute
? printAttribute(name, receivedValue)
: 'attribute was not found',
)
const expectedMsg = `Expected the element ${to} have attribute:\n ${expectedColor(
printAttribute(name, expectedValue),
)}`
const receivedAttribute = hasAttribute
? printAttribute(name, receivedValue)
: null
const matcher = matcherHint(
`${this.isNot ? '.not' : ''}.toHaveAttribute`,
'element',
Expand All @@ -127,7 +120,13 @@ const extensions = {
comment: getAttributeComment(name, expectedValue),
},
)
return `${matcher}\n\n${expectedMsg}\nReceived:\n ${receivedAttribute}`
return getMessage(
matcher,
`Expected the element ${to} have attribute`,
printAttribute(name, expectedValue),
'Received',
receivedAttribute,
)
},
}
},
Expand Down