Skip to content
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

Query element by selector later if it's not exists during configuration #1679

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

gooddaytoday
Copy link

@gooddaytoday gooddaytoday commented May 26, 2022

I'm sorry I didn't wait for approval #1674 🏎️ . Just need to buy commercial license with this feature 🙂 .

Processing possible absence of an element, configured with string selector: when creating list of steps, we take into account the possible absence of element.

  • If step.element is a selector for which there is no element at the configuration stage, then we replace .element specifying a getter/setter for it. In the getter we return the element via document.querySelector
  • Since that moment we assume that .element should return HTMLElement by selector and if there is still no element - throwing Error. Thus to check raw field below we need use also ._element.

The behavior has changed: earlier if there wasn't element by selector, IntroJS silently created floating tooltip at that step.

Also:

Closes #1674.

When creating list of steps, we take into account the possible absence of element by selector

If .element is a selector for which there is no element at the configuration stage, then we replace .element specifying a getter/setter for it. In the getter  we return the element via document.querySelector

Since that moment we assume that .element should return HTMLElement by selector and if there is still not element - throwing Error. Thus to check raw field below we need use ._element
@gooddaytoday
Copy link
Author

And it's not finished yet. If element is added with delay (for example, after async request to server), an error will occur

@gooddaytoday
Copy link
Author

And it's not finished yet. If element is added with delay (for example, after async request to server), an error will occur

Finished that feature:

  • Added function to wait until element appears in DOM 79dea70
  • waitforElement function applied in nextStep b6e885a

@gooddaytoday
Copy link
Author

  • waitforElement function applied in nextStep b6e885a

Fixed commit b61ffab and added test gooddaytoday@8716a1a for that error

@gooddaytoday gooddaytoday force-pushed the process-absense-element-by-selector branch from 6446dd6 to 43146e2 Compare June 9, 2022 16:10
@gooddaytoday gooddaytoday force-pushed the process-absense-element-by-selector branch from 7930cd6 to 7580560 Compare July 7, 2022 15:03
@gooddaytoday
Copy link
Author

Rebased and edited branch in order to:

  • Reduce amount of changes: added new create_div_btn.html instead of editing index.html, so there is no differences in old screenshots.
  • Clean history to make it more clear

Comment on lines +108 to +113
try {
const element = steps[0].element; // jshint ignore:line
} catch (e) {
if (!e.message.includes("There is no element with given selector:"))
throw e;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we checking here to make sure the steps array is empty?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we calling here .element of the first array item to check that Error "There is not element..." is throwing in case of element absence.

} else {
// If element is not exists yet, we'll get it on step
const elSelector = currentItem.element;
Object.defineProperty(currentItem, "element", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it important to have this object when the DOMElement doesn't exist? Can we just replace it with undefined?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my first (local) implementation was with undefined, but it could be more changes in other code, that calls .element.

I'll try to do with undefined.

}

if (typeof MutationObserver !== "undefined") {
/* jshint ignore:start */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we suppressing jshint errors? Happy to help if there are any issues with our jshint setup

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it could be deleted. Added MutationObserver to globals in jshint config and removed jshint ignore gooddaytoday@33e560b

* @param {number} checkInterval In milliseconds
* @param {number} maxTimeout In milliseconds
*/
function waitForElementByTimeout(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this function please? I'm a little concerned since it's doing a recursive call.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests in gooddaytoday@d5eaf0d

@@ -47,6 +47,14 @@ context("Navigation", () => {
cy.get(".introjs-showElement").should("not.exist");
});

it("should exit the tour after right btn pressed at the end", () => {
cy.get(".introjs-tooltiptext").contains("step one");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beautiful! Thank you

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, there wasn't such error in master before adding this feature. Possible error was taken into account in 87fa604

@gooddaytoday gooddaytoday force-pushed the process-absense-element-by-selector branch from 9db181e to 6c25ffa Compare August 8, 2022 09:45
@gooddaytoday gooddaytoday force-pushed the process-absense-element-by-selector branch from 6c25ffa to d5eaf0d Compare August 8, 2022 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: query element by selector on step if it's not exists during configuration
2 participants