Repository navigation
remove jquery from ember-testing - #16075
Conversation
I believe we can't do this. At least I don't know any RFC with this discussed. @rwjblue isn't |
|
Another potential hairy spot - Listening for Could create our own data structure to hold these events instead.... 🤔 |
snewcomer
left a comment
There was a problem hiding this comment.
Two items left include:
find- replace jquery with JS- Bring back those tests with some data structure that holds / resets
ajaxSend&&ajaxComplete
| } else { | ||
| $el.trigger('focusin'); | ||
| } | ||
| let browserIsNotFocused = document.hasFocus && !document.hasFocus(); |
There was a problem hiding this comment.
Looks like there is a better, but similar sol'n here
| selector argument to find only within the context's children | ||
| @return {Object} jQuery object representing the results of the query | ||
| @throws {Error} throws error if jQuery object returned has a length of 0 | ||
| @return {Object} DOM element representing the results of the query |
There was a problem hiding this comment.
This has yet to be implemented. find is still using jquery. If we merge w/o the find change, I can revert these files.
7912b24 to
a1ae8bc
Compare
| // does not have focus. If the document doesn't have focus just | ||
| // use trigger('focusin') instead. | ||
| if (browserIsNotFocused) { | ||
| fireEvent(el, 'focusin'); |
There was a problem hiding this comment.
FWIW, I totally realize that this isn't a mistake in your migrate to native from jQuery, and that it was preexisting. During my testing of this functionality while migrating it to ember-test-helpers I don't think this is quite right, it should be something akin to this:
// makes `document.activeElement` be `element`. If the browser is focused, it also fires a focus event
element.focus();
// Firefox does not trigger the `focusin` event if the window
// does not have focus. If the document does not have focus then
// fire `focusin` event as well.
if (browserIsNotFocused) {
// if the browser is not focused the previous `el.focus()` didn't fire an event, so we simulate it
fireEvent(element, 'focus', {
bubbles: false,
});
fireEvent(element, 'focusin');
}Otherwise the event ordering is incorrect...
See related implementation from @ember/test-helpers.
There was a problem hiding this comment.
Getting these ie11 errors after refactoring to this.
Looks like it is triggering the events multiple times and causing pain click triggering events in order.
| event = document.createEvent('KeyEvents'); | ||
| let eventOpts = jQuery.extend({}, DEFAULT_EVENT_OPTIONS, options); | ||
| let eventOpts = assign({}, DEFAULT_EVENT_OPTIONS); | ||
| assign(eventOpts, options); |
There was a problem hiding this comment.
Ember.assign is a rough polyfill for Object.assign and does properly accept multiple arguments. Can you update to:
let eventOpts = assign({}, DEFAULT_EVENT_OPTIONS, options);| focus(el); | ||
|
|
||
| $el.eq(0).val(text); | ||
| el.value = text; |
There was a problem hiding this comment.
Can you confirm if $el.eq(0).val(text) supported content editable elements? I don't recall if it did, but if it did then this would be a regression (since when content editable set you need to set innerHTML).
See related code from @ember/test-helper.
There was a problem hiding this comment.
Seems that both jquery and JS. And yeah def need to set that innerHTML! 👍
el = document.createElement('div')
$(el).attr('contenteditable', 'true')
$(el).val('wat')
$(el).text() // or $(el).html() // ""
| .on('click', handler) | ||
| .trigger('click') | ||
| .remove(); | ||
| input.setAttribute('type', 'checkbox'); |
There was a problem hiding this comment.
I think this should stay back in jQuery mode since its only ran behind a !jQueryDisabled guard
| 'onInjectHelpers are called after injectTestHelpers' | ||
| ); | ||
| } | ||
| // [`@test #setupForTesting attaches ajax listeners`](assert) { |
There was a problem hiding this comment.
Can you bring these tests back, but put a short circuit in when jQuery is disabled?
something like:
['@test asdfasdf'](assert) {
if (jQueryDisabled) {
assert.expect(0);
return;
}
}There was a problem hiding this comment.
So this one might be a bit difficult to get at the event listeners w/ documentEvents = jQuery._data(document, 'events'); if jquery is enabled since it is now using document.addEventListener in setup-for-testing. The only thing I was thinking was to track the events with a data structure of our own. Thoughts?
b57c2fe to
da22320
Compare
| context = context || get(app, 'rootElement'); | ||
| $el = app.$(selector, context); | ||
| return $el; | ||
| if (jQueryDisabled) { |
There was a problem hiding this comment.
So to allow this to work in all cases, find needs to accept an element or selector...not just a selector.
| return $el; | ||
| if (jQueryDisabled) { | ||
| let rootElement = document.querySelector(context); | ||
| $el = rootElement.querySelector(selector); |
There was a problem hiding this comment.
This should be returned, (we don't need to wrap in an array)
There was a problem hiding this comment.
Also, I think we should do:
let result;
if (context instanceof Element) {
result = context.querySelector(selector);
} else if (typeof context === 'string') {
result = document.querySelector(`${context} ${selector}`);
} else {
result = document.querySelector(selector);
}
return result;There was a problem hiding this comment.
Fixed! 🌮
So to support both jquery and JS, I think we need to return an array or array-like object from find. Other helpers are expecting this (like findWithAssert). Unless we wanted to go with only returning one element; however, perhaps users were using find to select multiple as show here.
| } | ||
| return []; | ||
| } else { | ||
| $el = app.$(selector, context); |
There was a problem hiding this comment.
Can probably return this directly
| @method click | ||
| @param {String} selector jQuery selector for finding element on the DOM | ||
| @param {Object} context A DOM Element, Document, or jQuery to use as context | ||
| @param {String} selector css selector for finding element on the DOM |
There was a problem hiding this comment.
Although the functionality has not changed and still uses jquery when it's available, this docs change could be misleading, suggesting there is a breaking change. Shouldn't this state something like jQuery selector or css selector (if jQuery is not present) for finding element on the DOM?
There was a problem hiding this comment.
Oh, seems you changed it already while I wrote this!? :)
There was a problem hiding this comment.
That seems like a good option. I have it now as @param {String|Element} target the element or selector to click on. What do you think of this? Should it be:
the element or jQuery selector or css selector (if jQuery is not preset) to click on
403df0f to
814ed21
Compare
snewcomer
left a comment
There was a problem hiding this comment.
I think almost done! Lmk what you think about the conditionals I put around the tests.
| } | ||
|
|
||
| setupForTesting(); | ||
| // [`@test #setupForTesting attaches ajax listeners`](assert) { |
There was a problem hiding this comment.
If we want to bring back these tests, we would have to add back the jquery.on listeners in setup_for_testing. I don't think there is a way to track all of the events listening in a cross browser way (getEventListeners; however, I think no IE). We could add an explicit trackEventLIsteners method but I don't think we would want to do that either.
However, it seems to me that the below tests cover the functionality in setup_for_testing and these commented out tests could be deleted.
There was a problem hiding this comment.
@rwjblue lmk what you think about this remaining issue.
| @method click | ||
| @param {String} selector jQuery selector for finding element on the DOM | ||
| @param {Object} context A DOM Element, Document, or jQuery to use as context | ||
| @param {Object} context A DOM Element, Document, or css to use as context |
There was a problem hiding this comment.
need to revert this
| selector argument to find only within the context's children | ||
| @return {Object} jQuery object representing the results of the query | ||
| @throws {Error} throws error if jQuery object returned has a length of 0 | ||
| @return {Object} DOM element representing the results of the query |
There was a problem hiding this comment.
need to revert this.
| window.visit('/').then(() => { | ||
| expectAssertion(() => window.find('.name'), | ||
| /If not using jQuery, please import and use methods from @ember\/test-helpers/); | ||
| }); |
There was a problem hiding this comment.
This asserts that assert is thrown
9de52e9 to
cf6ce62
Compare
|
I left one inline comment, and other than that we need to squash down the commits into one and this should be good to go... |
| */ | ||
| export default function find(app, selector, context) { | ||
| if (jQueryDisabled) { | ||
| assert('If not using jQuery, please import and use methods from @ember/test-helpers'); |
There was a problem hiding this comment.
which helper exactly can be used? I think @ember/test-helpers doesn't provide any alternatives to find.
There was a problem hiding this comment.
@ro0gr - Usages of find should be replaced by this.element.querySelector or this.element.querySelectorAll...
There was a problem hiding this comment.
but the old good acceptance tests don't have this.element. Should this assertion have a reference to setupTest api?
There was a problem hiding this comment.
FWIW - By the time this hits a release version, the guides will all have been updated to reference the new style (work progressing in emberjs/guides#2145).
For now, lets update the assertion to tell the user that find is not available when jQuery is disabled without giving a more detailed recourse (we can tweak the message when we have better docs to cross link to)...
What do y'all think?
371ee49 to
59d6680
Compare
59d6680 to
3e005da
Compare
|
@rwjblue It looks like the tests are passing but for a focus-in test in |
Edited: The path forward now is to warn the end user that if jQuery is disabled, they should use instead
@ember/test-helpers.References #16058