-
Notifications
You must be signed in to change notification settings - Fork 404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Depreciate toBeInTheDOM with replacement #40
Changes from 14 commits
ea87915
ff4875e
ab1a19f
a9e3eb6
c151c49
9eaaaff
d4d230d
c4961a5
a30bfaa
6047eb3
0018069
f304c07
9af1d22
a7e78e5
45740d5
2e7e70a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
test('.toBeInTheDocument', () => { | ||
document.body.innerHTML = ` | ||
<span data-testid="html-element"><span>Html Element</span></span> | ||
<svg data-testid="svg-element"></svg>` | ||
|
||
const htmlElement = document.querySelector('[data-testid="html-element"]') | ||
const svgElement = document.querySelector('[data-testid="html-element"]') | ||
const detachedElement = document.createElement('div') | ||
const fakeElement = {thisIsNot: 'an html element'} | ||
const undefinedElement = undefined | ||
const nullElement = null | ||
|
||
expect(htmlElement).toBeInTheDocument() | ||
expect(svgElement).toBeInTheDocument() | ||
expect(detachedElement).not.toBeInTheDocument() | ||
|
||
// negative test cases wrapped in throwError assertions for coverage. | ||
expect(() => expect(htmlElement).not.toBeInTheDocument()).toThrowError() | ||
expect(() => expect(svgElement).not.toBeInTheDocument()).toThrowError() | ||
expect(() => expect(detachedElement).toBeInTheDocument()).toThrowError() | ||
expect(() => expect(fakeElement).toBeInTheDocument()).toThrowError() | ||
expect(() => expect(undefinedElement).toBeInTheDocument()).toThrowError() | ||
expect(() => expect(nullElement).toBeInTheDocument()).toThrowError() | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import {checkDocumentKey, deprecate} from '../utils' | ||
|
||
function matcherMock() {} | ||
|
||
test('deprecate', () => { | ||
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) | ||
const name = 'test' | ||
const replacement = 'test' | ||
const message = `Warning: ${name} has been deprecated and will be removed in future updates.` | ||
|
||
deprecate(name, replacement) | ||
expect(spy).toHaveBeenCalledWith(message, replacement) | ||
|
||
deprecate(name) | ||
expect(spy).toHaveBeenCalledWith(message, undefined) | ||
|
||
spy.mockRestore() | ||
}) | ||
|
||
test('checkDocumentKey', () => { | ||
const fakeKey = 'fakeKey' | ||
const realKey = 'documentElement' | ||
const badKeyMessage = `${fakeKey} is undefined on document but is required to use ${ | ||
matcherMock.name | ||
}.` | ||
|
||
const badDocumentMessage = `document is undefined on global but is required to use ${ | ||
matcherMock.name | ||
}.` | ||
|
||
expect(() => | ||
checkDocumentKey(document, realKey, matcherMock), | ||
).not.toThrowError() | ||
|
||
expect(() => { | ||
checkDocumentKey(document, fakeKey, matcherMock) | ||
}).toThrow(badKeyMessage) | ||
|
||
expect(() => { | ||
//eslint-disable-next-line no-undef | ||
checkDocumentKey(undefined, realKey, matcherMock) | ||
}).toThrow(badDocumentMessage) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {matcherHint, printReceived} from 'jest-matcher-utils' | ||
import {checkHtmlElement, checkDocumentKey} from './utils' | ||
|
||
export function toBeInTheDocument(element) { | ||
checkHtmlElement(element, toBeInTheDocument, this) | ||
checkDocumentKey(global.document, 'documentElement', toBeInTheDocument) | ||
|
||
return { | ||
pass: document.documentElement.contains(element), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we check whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm, sounds good, agreed on developer experience. It would be nice to get a clear error. Does the following sound good @jgoz?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 the first message should be good enough for both cases. |
||
message: () => { | ||
return [ | ||
matcherHint( | ||
`${this.isNot ? '.not' : ''}.toBeInTheDocument`, | ||
'element', | ||
'', | ||
), | ||
'', | ||
'Received:', | ||
` ${printReceived( | ||
element.hasChildNodes() ? element.cloneNode(false) : element, | ||
)}`, | ||
].join('\n') | ||
}, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would've thought this line was also going to be replaced to use some of the other elements you define above that are indeed in the document.
I'd even insist in changing it merely on the ground that this is referencing a helper function in dom-testing-library, probably since this README was created based on that other library's README.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am sorry! Great catch!