-
Notifications
You must be signed in to change notification settings - Fork 405
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
Improve WebChannel mocking #3647
Improve WebChannel mocking #3647
Conversation
Codecov Report
@@ Coverage Diff @@
## main #3647 +/- ##
==========================================
- Coverage 88.90% 88.87% -0.03%
==========================================
Files 258 258
Lines 21498 21542 +44
Branches 5494 5504 +10
==========================================
+ Hits 19112 19146 +34
- Misses 2212 2222 +10
Partials 174 174
Continue to review full report at Codecov.
|
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.
r=me for calling the original addEventListener
/removeEventListener
/dispatchEvent
functions, but please double check if all these restoreOriginals
calls are useful, as I suspect they're not. If they're not useful can you please remove them?
Thanks!
@@ -124,5 +127,7 @@ describe('app/DragAndDrop', () => { | |||
|
|||
fireEvent.dragLeave(dragAndDrop); | |||
expect(overlay.classList).not.toContain('dragging'); | |||
|
|||
restoreOriginals(); |
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.
We don't need to add all these restoreOriginals calls, because jest.spyOn
spys are automatically removed after each test. See
Lines 25 to 28 in bead77f
jest.resetAllMocks(); | |
jest.restoreAllMocks(); | |
jest.clearAllTimers(); | |
jest.useRealTimers(); |
(historical note: in the past we used to do this with some jest configuration, but this wasn't working properly, see jestjs/jest#9896 especially. That's why we're doing it manually in the setup file. I don't know if that improved.)
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.
Oh, perfect! That should make the patch a lot simpler.
This means that these functions will be usable from UrlManager.test.js. I'll make use of that in a future PR.
The WebChannel mocking now calls the original functions for addEventListener, removeEventListener, and dispatchEvent. This means that it won't interfere with other uses of these APIs. One example of such a use is the popstate event: UrlManager.test.js has a test which makes sure that the URL state is restored when going backwards in history. Using a mocked WebChannel in that test broke it because the popstate event was no longer being dispatched.
d0af54c
to
8d06672
Compare
This PR has two commits which improve the mock WebChannel which we use in tests: The code is moved to a different file, it no longer breaks other uses of window.dispatchEvent, and it adds a way to clean up after the test.