Skip to content

Commit 1d46020

Browse files
authored
feat: 🎸 expose event creators (#355)
1 parent eb291b2 commit 1d46020

File tree

7 files changed

+42
-8
lines changed

7 files changed

+42
-8
lines changed

.all-contributorsrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,16 @@
443443
"contributions": [
444444
"code"
445445
]
446+
},
447+
{
448+
"login": "twittwer",
449+
"name": "Tobias Wittwer",
450+
"avatar_url": "https://avatars1.githubusercontent.com/u/8677948?v=4",
451+
"profile": "https://github.com/twittwer",
452+
"contributions": [
453+
"code",
454+
"doc"
455+
]
446456
}
447457
]
448458
}

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img width="100%" height="100%" src="https://raw.githubusercontent.com/ngneat/spectator/master/image.svg?sanitize=true">
33
</p>
44

5-
[![All Contributors](https://img.shields.io/badge/all_contributors-47-orange.svg?style=flat-square)](#contributors)
5+
[![All Contributors](https://img.shields.io/badge/all_contributors-48-orange.svg?style=flat-square)](#contributors)
66
[![spectator](https://img.shields.io/badge/tested%20with-spectator-2196F3.svg?style=flat-square)]()
77
[![MIT](https://img.shields.io/packagist/l/doctrine/orm.svg?style=flat-square)]()
88
[![commitizen](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg?style=flat-square)]()
@@ -36,6 +36,7 @@ Spectator helps you get rid of all the boilerplate grunt work, leaving you with
3636
- [Testing Components](#testing-components)
3737
- [Events API](#events-api)
3838
- [Custom Events](#custom-events)
39+
- [Event Creators](#event-creators)
3940
- [Keyboard helpers](#keyboard-helpers)
4041
- [Mouse helpers](#mouse-helpers)
4142
- [Queries](#queries)
@@ -225,7 +226,7 @@ spectator.typeInElement(value, byText('Element'));
225226
spectator.dispatchMouseEvent(SpectatorElement, 'mouseout');
226227
spectator.dispatchMouseEvent(SpectatorElement, 'mouseout'), x, y, event);
227228
spectator.dispatchMouseEvent(byText('Element'), 'mouseout');
228-
spectator.dispatchMouseEvent(byText('Element'), 'mouseout'), x, y, event);
229+
spectator.dispatchMouseEvent(byText('Element'), 'mouseout', x, y, event);
229230
```
230231
- `dispatchKeyboardEvent()` - Triggers a keyboard event:
231232
```ts
@@ -248,6 +249,17 @@ spectator.triggerEventHandler(MyChildComponent, 'myCustomEvent', 'eventValue');
248249
spectator.triggerEventHandler('app-child-component', 'myCustomEvent', 'eventValue');
249250
```
250251

252+
#### Event Creators
253+
254+
In case you want to test events independently of any template (e.g. in presenter services) you can fallback on the underlying event creators.
255+
They are basically providing the same signature without the preceding element.
256+
```ts
257+
const keyboardEvent = createKeyboardEvent('keyup', 'ArrowDown'/*, targetElement */);
258+
const mouseEvent = createMouseEvent('mouseout');
259+
const touchEvent = createTouchEvent('touchmove');
260+
const fakeEvent = createFakeEvent('input');
261+
```
262+
251263
### Keyboard helpers
252264
```ts
253265
spectator.keyboard.pressEnter();
@@ -1153,7 +1165,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
11531165
| [<img src="https://avatars1.githubusercontent.com/u/20629635?v=4" width="100px;"/><br /><sub><b>Chris Cooper</b></sub>](https://github.com/cjcoops)<br />[💻](https://github.com/ngneat/spectator/commits?author=cjcoops "Code") | [<img src="https://avatars1.githubusercontent.com/u/5886709?v=4" width="100px;"/><br /><sub><b>Marc Scheib</b></sub>](https://github.com/MarcScheib)<br />[📖](https://github.com/ngneat/spectator/commits?author=MarcScheib "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/2036391?v=4" width="100px;"/><br /><sub><b>dgsmith2</b></sub>](https://github.com/dgsmith2)<br />[💻](https://github.com/ngneat/spectator/commits?author=dgsmith2 "Code") | [<img src="https://avatars1.githubusercontent.com/u/50801476?v=4" width="100px;"/><br /><sub><b>dedwardstech</b></sub>](https://github.com/dedwardstech)<br />[💻](https://github.com/ngneat/spectator/commits?author=dedwardstech "Code") [🤔](#ideas-dedwardstech "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/11571461?v=4" width="100px;"/><br /><sub><b>tamasfoldi</b></sub>](https://github.com/tamasfoldi)<br />[💻](https://github.com/ngneat/spectator/commits?author=tamasfoldi "Code") [🤔](#ideas-tamasfoldi "Ideas, Planning, & Feedback") | [<img src="https://avatars0.githubusercontent.com/u/10036108?v=4" width="100px;"/><br /><sub><b>Paolo Caleffi</b></sub>](https://github.com/IlCallo)<br />[💻](https://github.com/ngneat/spectator/commits?author=IlCallo "Code") | [<img src="https://avatars2.githubusercontent.com/u/7110786?v=4" width="100px;"/><br /><sub><b>Toni Villena</b></sub>](https://github.com/tonivj5)<br />[💻](https://github.com/ngneat/spectator/commits?author=tonivj5 "Code") |
11541166
| [<img src="https://avatars2.githubusercontent.com/u/6719615?v=4" width="100px;"/><br /><sub><b>Itay Oded</b></sub>](https://github.com/itayod)<br />[💻](https://github.com/ngneat/spectator/commits?author=itayod "Code") | [<img src="https://avatars0.githubusercontent.com/u/1236069?v=4" width="100px;"/><br /><sub><b>Guillaume de Jabrun</b></sub>](https://twitter.com/Wykks)<br />[💻](https://github.com/ngneat/spectator/commits?author=Wykks "Code") | [<img src="https://avatars2.githubusercontent.com/u/5392144?v=4" width="100px;"/><br /><sub><b>Anand Tiwary</b></sub>](https://github.com/codeNoobie)<br />[💻](https://github.com/ngneat/spectator/commits?author=codeNoobie "Code") | [<img src="https://avatars3.githubusercontent.com/u/13190278?v=4" width="100px;"/><br /><sub><b>Ales Doganoc</b></sub>](https://github.com/AlesDo)<br />[💻](https://github.com/ngneat/spectator/commits?author=AlesDo "Code") | [<img src="https://avatars3.githubusercontent.com/u/1009783?v=4" width="100px;"/><br /><sub><b>Zoltan</b></sub>](https://zoltan.nz)<br />[💻](https://github.com/ngneat/spectator/commits?author=zoltan-nz "Code") | [<img src="https://avatars1.githubusercontent.com/u/13370430?v=4" width="100px;"/><br /><sub><b>Vitalii Baziuk</b></sub>](https://github.com/Coffee-Tea)<br />[💻](https://github.com/ngneat/spectator/commits?author=Coffee-Tea "Code") | [<img src="https://avatars0.githubusercontent.com/u/50454150?v=4" width="100px;"/><br /><sub><b>clementlemarc-certua</b></sub>](https://github.com/clementlemarc-certua)<br />[💻](https://github.com/ngneat/spectator/commits?author=clementlemarc-certua "Code") |
11551167
| [<img src="https://avatars1.githubusercontent.com/u/302213?v=4" width="100px;"/><br /><sub><b>Yuriy Grunin</b></sub>](https://github.com/th0r)<br />[💻](https://github.com/ngneat/spectator/commits?author=th0r "Code") | [<img src="https://avatars2.githubusercontent.com/u/11780854?v=4" width="100px;"/><br /><sub><b>Andrey Chalkin</b></sub>](https://t.me/L2jLiga)<br />[💻](https://github.com/ngneat/spectator/commits?author=L2jLiga "Code") | [<img src="https://avatars3.githubusercontent.com/u/7720242?s=400&v=4" width="100px;"/><br /><sub><b>Steven Harris</b></sub>](https://stevenharrisdev.com)<br />[💻](https://github.com/ngneat/spectator/commits?author=Steven-Harris "Code") [📖](https://github.com/ngneat/spectator/commits?author=Steven-Harris "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4697912?v=4" width="100px;"/><br /><sub><b>Richard Sahrakorpi</b></sub>](http://rjgunning.com)<br />[💻](https://github.com/ngneat/spectator/commits?author=RGunning "Code") | [<img src="https://avatars2.githubusercontent.com/u/13023533?v=4" width="100px;"/><br /><sub><b>Dominik Kremer</b></sub>](https://github.com/kremerd)<br />[💻](https://github.com/ngneat/spectator/commits?author=kremerd "Code") | [<img src="https://avatars1.githubusercontent.com/u/5039606?v=4" width="100px;"/><br /><sub><b>Mehmet Ozan Turhan</b></sub>](https://medium.com/@ozanturhan)<br />[💻](https://github.com/ngneat/spectator/commits?author=ozanturhan "Code") | [<img src="https://avatars2.githubusercontent.com/u/13765663?v=4" width="100px;"/><br /><sub><b>Vlad Lashko</b></sub>](https://github.com/thunderer199)<br />[💻](https://github.com/ngneat/spectator/commits?author=thunderer199 "Code") |
1156-
| [<img src="https://avatars0.githubusercontent.com/u/20136906?v=4" width="100px;"/><br /><sub><b>William Tjondrosuharto</b></sub>](https://github.com/williamjuan027)<br />[💻](https://github.com/ngneat/spectator/commits?author=williamjuan027 "Code") | [<img src="https://avatars0.githubusercontent.com/u/1752170?v=4" width="100px;"/><br /><sub><b>Chaz Gatian</b></sub>](https://twitter.com/Cgatian)<br />[💻](https://github.com/ngneat/spectator/commits?author=cgatian "Code") | [<img src="https://avatars2.githubusercontent.com/u/27703381?v=4" width="100px;"/><br /><sub><b>Pavel Korobov</b></sub>](https://github.com/shirados90)<br />[💻](https://github.com/ngneat/spectator/commits?author=shirados90 "Code") | [<img src="https://avatars3.githubusercontent.com/u/25953659?v=4" width="100px;"/><br /><sub><b>Enno Lohmann</b></sub>](https://github.com/EnnoLohmann)<br />[💻](https://github.com/ngneat/spectator/commits?author=EnnoLohmann "Code") | [<img src="https://avatars2.githubusercontent.com/u/5169399?v=4" width="100px;"/><br /><sub><b>Pawel Boguslawski</b></sub>](https://github.com/bogusweb)<br />[💻](https://github.com/ngneat/spectator/commits?author=bogusweb "Code") |
1168+
| [<img src="https://avatars0.githubusercontent.com/u/20136906?v=4" width="100px;"/><br /><sub><b>William Tjondrosuharto</b></sub>](https://github.com/williamjuan027)<br />[💻](https://github.com/ngneat/spectator/commits?author=williamjuan027 "Code") | [<img src="https://avatars0.githubusercontent.com/u/1752170?v=4" width="100px;"/><br /><sub><b>Chaz Gatian</b></sub>](https://twitter.com/Cgatian)<br />[💻](https://github.com/ngneat/spectator/commits?author=cgatian "Code") | [<img src="https://avatars2.githubusercontent.com/u/27703381?v=4" width="100px;"/><br /><sub><b>Pavel Korobov</b></sub>](https://github.com/shirados90)<br />[💻](https://github.com/ngneat/spectator/commits?author=shirados90 "Code") | [<img src="https://avatars3.githubusercontent.com/u/25953659?v=4" width="100px;"/><br /><sub><b>Enno Lohmann</b></sub>](https://github.com/EnnoLohmann)<br />[💻](https://github.com/ngneat/spectator/commits?author=EnnoLohmann "Code") | [<img src="https://avatars2.githubusercontent.com/u/5169399?v=4" width="100px;"/><br /><sub><b>Pawel Boguslawski</b></sub>](https://github.com/bogusweb)<br />[💻](https://github.com/ngneat/spectator/commits?author=bogusweb "Code") | [<img src="https://avatars1.githubusercontent.com/u/8677948?v=4" width="100px;"/><br /><sub><b>Tobias Wittwer</b></sub>](https://github.com/twittwer)<br />[💻](https://github.com/ngneat/spectator/commits?author=twittwer "Code") [📖](https://github.com/ngneat/spectator/commits?author=twittwer "Documentation") |
11571169
<!-- ALL-CONTRIBUTORS-LIST:END -->
11581170

11591171
This project follows the [all-contributors](https://allcontributors.org/docs/en/emoji-key) specification. Contributions of any kind welcome!

docs/docs/events.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ spectator.typeInElement(value, SpectatorElement);
3232
- `dispatchMouseEvent()` - Triggers a mouse event:
3333
```ts
3434
spectator.dispatchMouseEvent(SpectatorElement, 'mouseout');
35-
spectator.dispatchMouseEvent(SpectatorElement, 'mouseout'), x, y, event);
35+
spectator.dispatchMouseEvent(SpectatorElement, 'mouseout', x, y, event);
3636
```
3737
- `dispatchKeyboardEvent()` - Triggers a keyboard event:
3838
```ts
@@ -51,3 +51,14 @@ You can trigger custom events (`@Output()` of child components) [using](https://
5151
spectator.triggerEventHandler(MyChildComponent, 'myCustomEvent', 'eventValue');
5252
spectator.triggerEventHandler('app-child-component', 'myCustomEvent', 'eventValue');
5353
```
54+
55+
## Event Creators
56+
57+
In case you want to test events independently of any template (e.g. in presenter services) you can fallback on the underlying event creators.
58+
They are basically providing the same signature without the preceding element.
59+
```ts
60+
const keyboardEvent = createKeyboardEvent('keyup', 'ArrowDown'/*, targetElement */);
61+
const mouseEvent = createMouseEvent('mouseout');
62+
const touchEvent = createTouchEvent('touchmove');
63+
const fakeEvent = createFakeEvent('input');
64+
```

projects/spectator/src/lib/base/dom-spectator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { isString, QueryOptions, QueryType, SpectatorElement, EventEmitterType,
99
import { SpyObject } from '../mock';
1010
import { getChildren, setProps } from '../internals/query';
1111
import { patchElementFocus } from '../internals/element-focus';
12-
import { createMouseEvent } from '../internals/event-objects';
12+
import { createMouseEvent } from '../event-creators';
1313
import { dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent } from '../dispatch-events';
1414
import { typeInElement } from '../type-in-element';
1515
import { selectOption } from '../select-option';

projects/spectator/src/lib/dispatch-events.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Credit - Angular Material
33
*/
44

5-
import { createFakeEvent, createKeyboardEvent, createMouseEvent, createTouchEvent } from './internals/event-objects';
5+
import { createFakeEvent, createKeyboardEvent, createMouseEvent, createTouchEvent } from './event-creators';
66
import { KeyboardEventOptions } from './types';
77

88
/**

projects/spectator/src/lib/internals/event-objects.ts renamed to projects/spectator/src/lib/event-creators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* Credit - Angular Material
33
*/
44

5-
import { parseKeyOptions } from './key-parser';
6-
import { KeyboardEventOptions } from '../types';
5+
import { parseKeyOptions } from './internals/key-parser';
6+
import { KeyboardEventOptions } from './types';
77

88
/** Creates a browser MouseEvent with the specified options. */
99
export function createMouseEvent(type: string, x: number = 0, y: number = 0, button: number = 0): MouseEvent {

projects/spectator/src/public_api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ export * from './lib/types';
4141
export { typeInElement } from './lib/type-in-element';
4242
export { defineGlobalsInjections } from './lib/globals-injections';
4343
export { dispatchEvent, dispatchFakeEvent, dispatchKeyboardEvent, dispatchMouseEvent, dispatchTouchEvent } from './lib/dispatch-events';
44+
export { createFakeEvent, createKeyboardEvent, createMouseEvent, createTouchEvent } from './lib/event-creators';
4445
export { MockComponentDeprecated, MockDirectiveDeprecated } from './lib/deprecated';

0 commit comments

Comments
 (0)