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

Add tests to HyperLink plugin #1329

Merged
merged 5 commits into from
Nov 14, 2022
Merged

Add tests to HyperLink plugin #1329

merged 5 commits into from
Nov 14, 2022

Conversation

ianeli1
Copy link
Member

@ianeli1 ianeli1 commented Oct 11, 2022

This test suite covers:

  • Hovering over nothing displaying no title
  • Hovering over a link displaying a message
  • Ctrl+Clicking the link opening it in a new tab
  • Clicking the link doing nothing

});

it('Removes title attribute if nothing is being hovered', () => {
div.dispatchEvent(new MouseEvent('mouseover'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of really dispatch an event, we prefer to mock a PluginEvent and call plugin.onPluginEvent() directly since we don't really need to test browser's code, but just test our plugin code only.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same for the others

Copy link
Member Author

@ianeli1 ianeli1 Oct 25, 2022

Choose a reason for hiding this comment

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

this is handled by mouseover and mouseout events, not onPluginEvent

Should I (<any>plugin).onMouse and just test them directly?

this.disposer = editor.addDomEventHandler({
mouseover: <DOMEventHandler>this.onMouse,
mouseout: <DOMEventHandler>this.onMouse,
blur: <DOMEventHandler>this.onBlur,
});
}
protected onMouse = (e: MouseEvent) => {
const a = this.editor?.getElementAtCursor(
'a[href]',
<Node>e.target
) as HTMLAnchorElement | null;
const href = a && this.tryGetHref(a);
if (href) {
this.editor?.setEditorDomAttribute(
'title',
e.type == 'mouseover' ? this.getTooltipCallback(href, a) : null
);
}
};

Copy link
Collaborator

Choose a reason for hiding this comment

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

@ianeli1 I think you can mock/spy editor.addDomEventHandler, so that you can easily get the onMouse callback in an elegant way.

});

it('Removes title attribute if nothing is being hovered', () => {
div.dispatchEvent(new MouseEvent('mouseover'));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same for the others

it('Opens the link whenever Ctrl+Click', () => {
const openSpy = spyOn(window, 'open');
div.appendChild(anchor);
plugin.onPluginEvent({
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is nice

@ianeli1 ianeli1 merged commit 76a8b5d into master Nov 14, 2022
@JiuqingSong JiuqingSong deleted the u/ianeli/hyperlink-test branch September 8, 2023 22:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants