Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

how to test onLongPress? #1510

Unanswered
zilahir asked this question in Q&A
Discussion options

I have read the docs.

However, I am still unable to test this properly.

i tried this:

const event = userEvent.setup();
await event.longPress(getByText("Route 2"));

but in this case I don't know what should be the condition to check.

And I tried this too:

fireEvent(getByText("Route 2"), "onLongPress");
 expect(mockNavigation.navigate).toHaveBeenCalledWith({
 navigation: mockNavigation,
 targetKey: "route2",
 });

but this fails as well:

くろまる TabBar › calls onLongPress when a tab is long-pressed
 expect(jest.fn()).toHaveBeenCalledWith(...expected)
 Expected: {"navigation": {"emit": [Function mockConstructor], "navigate": [Function mockConstructor]}, "targetKey": "route2"}
 Number of calls: 0
 131 | // check
 132 |
 > 133 | expect(mockNavigation.navigate).toHaveBeenCalledWith({
 | ^
 134 | navigation: mockNavigation,
 135 | targetKey: "route2",
 136 | });

Any ideas what I am missing? Thank you!

You must be logged in to vote

Replies: 2 comments

Comment options

update:

I've tried this too, no success:

const event = userEvent.setup();
 await event.longPress(getByText("Route 2"));
 await waitFor(() => {
 // Expect that onLongPress has been called with the correct arguments
 expect(mockOnLongPress).toHaveBeenCalledWith({
 navigation: mockNavigation,
 targetKey: "route2", // Adjust the expected target key as needed
 });
 });
You must be logged in to vote
0 replies
Comment options

Hello @zilahir, the code you posted should work, I can't tell what could be the problem here, could you provide the full example?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /