-
Notifications
You must be signed in to change notification settings - Fork 411
feat: Add .toAppearBefore/.toAppearAfter matcher #702
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
Open
nossbigg
wants to merge
2
commits into
testing-library:main
Choose a base branch
from
nossbigg:feat/add-toAppearBefore
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import {render} from './helpers/test-utils' | ||
|
||
describe('.toAppearBefore', () => { | ||
const {queryByTestId} = render(` | ||
<div> | ||
<div data-testid='div-a'> | ||
<span data-testid='text-a'>Text A</span> | ||
<span data-testid='text-b'>Text B</span> | ||
</div> | ||
</div> | ||
`) | ||
|
||
const textA = queryByTestId('text-a') | ||
const textB = queryByTestId('text-b') | ||
const divA = queryByTestId('div-a') | ||
|
||
it('returns correct result when first element is before second element', () => { | ||
expect(textA).toAppearBefore(textB) | ||
}) | ||
|
||
it('returns correct for .not() invocation', () => { | ||
expect(textB).not.toAppearBefore(textA) | ||
}) | ||
|
||
it('errors out when first element is not before second element', () => { | ||
expect(() => expect(textB).toAppearBefore(textA)).toThrowError( | ||
/Received: Node.DOCUMENT_POSITION_PRECEDING \(2\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when first element is parent of second element', () => { | ||
expect(() => expect(divA).toAppearBefore(textA)).toThrowError( | ||
/Received: Unknown document position \(20\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when first element is child of second element', () => { | ||
expect(() => expect(textA).toAppearBefore(divA)).toThrowError( | ||
/Received: Unknown document position \(10\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when either first or second element is not HTMLElement', () => { | ||
expect(() => expect(1).toAppearBefore(textB)).toThrowError() | ||
expect(() => expect(textA).toAppearBefore(1)).toThrowError() | ||
}) | ||
}) | ||
|
||
describe('.toAppearAfter', () => { | ||
const {queryByTestId} = render(` | ||
<div> | ||
<div data-testid='div-a'> | ||
<span data-testid='text-a'>Text A</span> | ||
<span data-testid='text-b'>Text B</span> | ||
</div> | ||
</div> | ||
`) | ||
|
||
const textA = queryByTestId('text-a') | ||
const textB = queryByTestId('text-b') | ||
const divA = queryByTestId('div-a') | ||
|
||
it('returns correct result when first element is after second element', () => { | ||
expect(textB).toAppearAfter(textA) | ||
}) | ||
|
||
it('returns correct for .not() invocation', () => { | ||
expect(textA).not.toAppearAfter(textB) | ||
}) | ||
|
||
it('errors out when first element is not after second element', () => { | ||
expect(() => expect(textA).toAppearAfter(textB)).toThrowError( | ||
/Received: Node.DOCUMENT_POSITION_FOLLOWING \(4\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when first element is parent of second element', () => { | ||
expect(() => expect(divA).toAppearAfter(textA)).toThrowError( | ||
/Received: Unknown document position \(20\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when first element is child of second element', () => { | ||
expect(() => expect(textA).toAppearAfter(divA)).toThrowError( | ||
/Received: Unknown document position \(10\)/i, | ||
) | ||
}) | ||
|
||
it('errors out when either first or second element is not HTMLElement', () => { | ||
expect(() => expect(1).toAppearAfter(textB)).toThrowError() | ||
expect(() => expect(textA).toAppearAfter(1)).toThrowError() | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {checkHtmlElement} from './utils' | ||
|
||
// ref: https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition | ||
const DOCUMENT_POSITIONS_STRINGS = { | ||
[Node.DOCUMENT_POSITION_DISCONNECTED]: 'Node.DOCUMENT_POSITION_DISCONNECTED', | ||
[Node.DOCUMENT_POSITION_PRECEDING]: 'Node.DOCUMENT_POSITION_PRECEDING', | ||
[Node.DOCUMENT_POSITION_FOLLOWING]: 'Node.DOCUMENT_POSITION_FOLLOWING', | ||
[Node.DOCUMENT_POSITION_CONTAINS]: 'Node.DOCUMENT_POSITION_CONTAINS', | ||
[Node.DOCUMENT_POSITION_CONTAINED_BY]: 'Node.DOCUMENT_POSITION_CONTAINED_BY', | ||
[Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC]: | ||
'Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC', | ||
} | ||
|
||
const makeDocumentPositionErrorString = documentPosition => { | ||
gnapse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if (documentPosition in DOCUMENT_POSITIONS_STRINGS) { | ||
return `${DOCUMENT_POSITIONS_STRINGS[documentPosition]} (${documentPosition})` | ||
} | ||
|
||
return `Unknown document position (${documentPosition})` | ||
} | ||
|
||
const checkToAppear = (methodName, targetDocumentPosition) => { | ||
gnapse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
// eslint-disable-next-line func-names | ||
return function (element, secondElement) { | ||
checkHtmlElement(element, toAppearBefore, this) | ||
checkHtmlElement(secondElement, toAppearBefore, this) | ||
|
||
const documentPosition = element.compareDocumentPosition(secondElement) | ||
const pass = documentPosition === targetDocumentPosition | ||
|
||
return { | ||
pass, | ||
message: () => { | ||
return [ | ||
this.utils.matcherHint( | ||
`${this.isNot ? '.not' : ''}.${methodName}`, | ||
'element', | ||
'secondElement', | ||
), | ||
'', | ||
`Received: ${makeDocumentPositionErrorString(documentPosition)}`, | ||
].join('\n') | ||
}, | ||
} | ||
} | ||
} | ||
|
||
export function toAppearBefore(element, secondElement) { | ||
return checkToAppear( | ||
'toAppearBefore', | ||
Node.DOCUMENT_POSITION_FOLLOWING, | ||
).apply(this, [element, secondElement]) | ||
} | ||
|
||
export function toAppearAfter(element, secondElement) { | ||
return checkToAppear('toAppearAfter', Node.DOCUMENT_POSITION_PRECEDING).apply( | ||
this, | ||
[element, secondElement], | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
What is the expected behavior if the two elements are parent/ancestor? That is:
Whatever is it (I hope it is a logical behavior), can we document that behavior? And also add it to the tests?
Uh oh!
There was an error while loading. Please reload this page.
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.
@gnapse given:
A
is parent spanB
is nested spanThen
expect(a).toAppearBefore(b)
, it will returnReceived: Unknown document position (20)
expect(b).toAppearBefore(a)
, it will returnReceived: Unknown document position (10)
Note:
Unknown document position
is a default fill when we can't find a corresponding string representation (ref) for the returnedcompareDocumentPosition()
value.I've covered the parent/child scenarios here ✅
jest-dom/src/__tests__/to-appear-before.js
Lines 31 to 41 in efba541
I think there's three ways that we can go with this:
.toAppearBefore()
matcher to use cases where devs are trying to test against parent/child relationships, but otherwise makes the matcher more lax..toAppearBefore()
matcher to strictly only sibling use cases, which is more exacting, but leaves devs out in the cold when it comes to testing parent/child relationships (ie. they'll have to write their own test helper for it)Thoughts?
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'm leaning towards thinking that the behavior I'd expect is that neither element is before or after the other. So both assertions would fail.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ah ok - the current implementation does error out on parent/child relationships. Shall I outline this behavior in the docs?
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.
If it errors out in both cases, that's ok.