-
Notifications
You must be signed in to change notification settings - Fork 82
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
Documentation on "testing that a component dispatch an event" #179
Comments
This issue here is that the platformShowToast stub does not export the event name so your handler in your test is never configured correctly. |
@damianpoole how am I even able to import it then?
|
@muenzpraeger what needs to be done to stub specifically? |
You would have to hardcode |
Just to be sure, there are two ways I can act:
When the jest config mention these folders, should I place my custom mocks there? or are these available as a consequence of importing some additional npm modules? i.e. who places something in '/force-app/test/jest-mocks/lightning/navigation' ?
Thanks a lot for your help |
Yes you would place your custom mock there or change the reference to a folder that better suits your project. |
but the question is, why doesn't the import statement fail in npm if that constant is not exported? |
AFAIK because you are creating a variable called |
Now the handler is getting triggered, except that the event.detail is null in the handler so if I had the following assertion, the test fails
|
Does your mock constructor pass the detail along like the example @muenzpraeger linked? |
I am not mocking the event, it is dispatched from within the component. I hardcoded the event name and subscribed to it in my test |
@edmondo1984 Do you use the stub that I linked? |
No @muenzpraeger I understood it was enough to perform this change in my unit test:
|
As mentioned earlier: If you want to test any of the details, like variant, message etc, you'd have also to use the custom mock. |
Thanks, I didn't understood that. Can you please explain why? |
Because the |
Excellent, thank you. It was basically me getting confused, maybe I can submit a PR to the documentation saying that you need to be careful about default stubs not stubbing all the properties? |
Description
The example shows how to test events dispatching, but events are dispatched explicitly. https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/default/lwc/miscNotification/__tests__/miscNotification.test.js
One additional pattern is instead verifying that for example, upon invoking a wire service that returns an error, the component dispatch the event, i.e. we do not want to dispatch the event explicitly.
Steps to Reproduce
# Command to repro sfdx-lwc-jest -- --no-cache
Expected Results
the Jest handler is invoked once
Actual Results
Expected number of calls: >= 1
Received number of calls: 0
Version
The text was updated successfully, but these errors were encountered: