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

feat(events): extend usage variants for event dispatch (#529) #536

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,13 @@ const mouseEvent = createMouseEvent('mouseout');
const touchEvent = createTouchEvent('touchmove');
const fakeEvent = createFakeEvent('input');
```

After creating and for example attaching a spy to the event you can dispatch them by using the generic `dispatchEvent` or more specific handlers;

```ts
const touchEvent = createTouchEvent('touchEvent');
spectator.dispatchEvent(SpectatorElement, touchEvent);

const mouseEvent = createMouseEvent('mouseout');
spectator.dispatchMouseEvent(SpectatorElement, mouseEvent);
```
46 changes: 40 additions & 6 deletions projects/spectator/src/lib/base/dom-spectator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SpyObject } from '../mock';
import { getChildren, setProps } from '../internals/query';
import { patchElementFocus } from '../internals/element-focus';
import { createMouseEvent } from '../event-creators';
import { dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent } from '../dispatch-events';
import { dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent, dispatchEvent } from '../dispatch-events';
import { typeInElement } from '../type-in-element';
import { selectOption } from '../select-option';

Expand Down Expand Up @@ -150,20 +150,31 @@ export abstract class DomSpectator<I> extends BaseSpectator {
this.detectChanges();
}

public dispatchMouseEvent(
selector: SpectatorElement,
typeOrEvent: MouseEvent,
)
public dispatchMouseEvent(
selector: SpectatorElement,
typeOrEvent: string,
x?: number,
y?: number,
event?: MouseEvent
)
public dispatchMouseEvent(
selector: SpectatorElement = this.element,
type: string,
typeOrEvent: string | MouseEvent,
x: number = 0,
y: number = 0,
event: MouseEvent = createMouseEvent(type, x, y)
event: MouseEvent = typeof typeOrEvent === 'string' ? createMouseEvent(typeOrEvent, x, y) : typeOrEvent
): MouseEvent {
const element = this.getNativeElement(selector);

if (!(element instanceof Node)) {
throw new Error(`Cannot dispatch mouse event: ${selector} is not a node`);
}

const dispatchedEvent = dispatchMouseEvent(element, type, x, y, event);
const dispatchedEvent = typeof typeOrEvent === 'string' ? dispatchMouseEvent(element, typeOrEvent, x, y, event) : dispatchEvent(element, typeOrEvent);
this.detectChanges();

return dispatchedEvent;
Expand Down Expand Up @@ -191,6 +202,20 @@ export abstract class DomSpectator<I> extends BaseSpectator {
return event;
}

public dispatchEvent<E extends Event = Event>(selector: SpectatorElement = this.element, event: E): Event {
const element = this.getNativeElement(selector);

if (!(element instanceof Node)) {
throw new Error(`Cannot dispatch event: ${selector} is not a node`);
}

const dispatchedEvent = dispatchEvent(element, event);

this.detectChanges();

return dispatchedEvent;
}

public dispatchFakeEvent(selector: SpectatorElement = this.element, type: string, canBubble?: boolean): Event {
const event = dispatchFakeEvent(this.getNativeElement(selector), type, canBubble);
this.detectChanges();
Expand Down Expand Up @@ -247,9 +272,18 @@ export abstract class DomSpectator<I> extends BaseSpectator {
};
}

public dispatchTouchEvent(selector: SpectatorElement = this.element, type: string, x: number = 0, y: number = 0): void {
dispatchTouchEvent(this.getNativeElement(selector), type, x, y);
public dispatchTouchEvent(selector: SpectatorElement = this.element, type: string, x: number = 0, y: number = 0): UIEvent {
const element = this.getNativeElement(selector);

if (!(element instanceof Node)) {
throw new Error(`Cannot dispatch touch event: ${selector} is not a node`);
}

const dispatchedEvent = dispatchTouchEvent(this.getNativeElement(selector), type, x, y);

this.detectChanges();

return dispatchedEvent;
}

public typeInElement(value: string, selector: SpectatorElement = this.element): void {
Expand Down
2 changes: 1 addition & 1 deletion projects/spectator/src/lib/dispatch-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ export function dispatchMouseEvent(
*
* @publicApi
*/
export function dispatchTouchEvent(node: HTMLElement | Window | Document, type: string, x: number = 0, y: number = 0): Event {
export function dispatchTouchEvent(node: HTMLElement | Window | Document, type: string, x: number = 0, y: number = 0): UIEvent {
return dispatchEvent(node, createTouchEvent(type, x, y));
}