-
-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
spectator.blur(..) broken in jest tests since v8.0.3 #477
Comments
@johncrim could you please check it? |
I researched it a bit further. This test, as it currently is, will fail when run as a jest test: it('should handle blur', () => {
spectator.blur('input');
expect(spectator.query('h1')).toHaveText('blur');
}); However, if I focus the element before blurring it, it will work. it('should handle blur', () => {
spectator.focus('input'); // <-- inserting this line makes the test pass
spectator.blur('input');
expect(spectator.query('h1')).toHaveText('blur');
}); I guess this mostly boils down to a difference between how the jasmine spectator patch works vs. how JSDom works. The spectator patch would just mindlessly forward the blur event where as JSDom goes "hey, this element has no focus, blur makes no sense, I'll just ignore it". So although this does break tests between 8.0.2 and 8.0.3 I'm not sure that the JSDom approach is wrong, it's just a different implementation choice. But it is confusing that blur works differently between jasmine and jest and at the very least I think that this should be documented somewhere. |
Also, if it is decided to align jasmine blur() with jest blur() then it's probably far easier to change the spectator patching code than to convince JSDom to do things differently. In this case I would suggest a deprecation period where jasmine blur continues to work as it currently does but it logs a warning if blurring a non-focused element. Anyway, now that we know what's going on it's no longer a problem for us so just offering my five cents. |
I appreciate your investigation. This might not be a good decision to change JSDom's behavior. Adding a note to the documentation will be helpful. Do you want to add it? |
Absolutely, I'll be pleased to do so. |
I chose to describe the limitation using the vague wording of "may be ignored" rather than describing the exact conditions (Spectator 8.0.3+ combined with jest) as that would probably be too much detail. |
Hi @mvindahl - I made the change to Spectator + jest behavior that went into 8.0.3. In general it is more correct now in Jest in that it is consistent with browser behavior (and relies on JSDOM handling focus and blur instead of skipping jsdom), but it can break tests that rely on incorrect behavior. I should have updated docs with this info. I did comment about this in the issue:
I'm not sure how much of this should go in the docs - it seems appropriate in release notes as a potentially breaking change, but it's a one time change, and documenting correct browser focus/blur behavior is probably outside the scope of Spectator docs. I apologize for not including that, this case was not on my mind when I completed the PR. The focus/blur patch is needed in karma tests/browsers b/c browsers don't properly focus/blur when the component is not visible. JSDOM doesn't have this limitation - my thinking is that since JSDOM is a highly functional test fake we should use it. I think making focus/blur work 100% correctly in browsers is outside the scope of Spectator - for example, we can't update |
Perhaps this info should be in the docs:
The reason I was hesitant to include something like this in the docs is that it may add more confusion than it does clarity. |
Hello @johncrim , thanks for your feedback. I had initially reached the conclusion that blur behaviour had been broken -- seeing some of our existing tests that would previously pass but would now fail. On closer reflection, and having investigated the details, my understanding now aligns with yours: JSDom does a better job of mimicking the browser than did the spectator focus/blur patch. Your comment on the 373 issue explains it well; I had somehow managed to overlook that, for which I apologise. As for the docs, I agree that we should not describe the subtle differences in painstaking detail in the docs; this would just leave people confused. How do you like my PR suggestion which adds a somewhat broadly phrased one liner to the docs? |
@mvindahl: I think your comment is good - it at least gives the reader something to try if they're encountering a problem with |
We should also mention the environment. |
@NetanelBasal , which level of detail would you prefer?
|
Option 3, thanks. |
👍 I have updated the PR accordingly. |
* docs: clarified limitation of blur() Closes: #477 * docs: added detail to blur() clarification
I'm submitting a...
Current behavior
spectator.blur(..)
does not work in jest tests when using spectator 8.0.3.We believe this to be due to the recent fix for #373.
Specifically, we believe the regression to be caused by the following change which alters the behavior when running jest: johncrim@9ff8bd4#diff-fdb99b18d9100abb482776b5c9d72409c00c0d64e919c825e993de6f643865a0R16
Expected behavior
It should work like it did in 8.0.2, and like it continues to do for jasmine tests.
Minimal reproduction of the problem with instructions
The regression is easily reproduced by copying the events component spec into the spectator/jest/test folder, "jestifying" the spectator import, and executing jest tests:
Using MacOS bash:
The output will now show the blur method to not work:
On a side note, it would probably be a good idea to keep this jest specific test as part of the fix since focus and blur can no longer be assumed to be handled in the same way across jasmine and jest.
Environment
Checked out the most recent master from ngneat/spectator. The issue is specific to jest tests so browsers are not relevant.
The text was updated successfully, but these errors were encountered: