Skip to content

Commit

Permalink
feat(testing): add toHaveLastReceivedEventDetail event spy matcher (#…
Browse files Browse the repository at this point in the history
…5829)

* add `toHaveLastReceivedEventDetail` matcher

* update e2e event test for new matcher
  • Loading branch information
tanner-reits authored Jun 20, 2024
1 parent d26ea2b commit 63491de
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2090,6 +2090,12 @@ declare global {
*/
toHaveFirstReceivedEventDetail(eventDetail: any): void;

/**
* When given an EventSpy, checks the last event has
* received the correct custom event `detail` data.
*/
toHaveLastReceivedEventDetail(eventDetail: any): void;

/**
* When given an EventSpy, checks the event at an index
* has received the correct custom event `detail` data.
Expand Down
27 changes: 27 additions & 0 deletions src/testing/jest/jest-27-and-under/matchers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ export function toHaveFirstReceivedEventDetail(eventSpy: d.EventSpy, eventDetail

const pass = deepEqual(eventSpy.firstEvent.detail, eventDetail);

expect(eventSpy.firstEvent.detail).toEqual(eventDetail);

return {
message: () => `expected event "${eventSpy.eventName}" detail to ${pass ? 'not ' : ''}equal`,
pass: pass,
};
}

export function toHaveLastReceivedEventDetail(eventSpy: d.EventSpy, eventDetail: any) {
if (!eventSpy) {
throw new Error(`toHaveLastReceivedEventDetail event spy is null`);
}

if (typeof (eventSpy as any).then === 'function') {
throw new Error(`event spy must be a resolved value, not a promise, before it can be tested`);
}

if (!eventSpy.eventName) {
throw new Error(`toHaveLastReceivedEventDetail did not receive an event spy`);
}

if (!eventSpy.firstEvent) {
throw new Error(`event "${eventSpy.eventName}" was not received`);
}

const pass = deepEqual(eventSpy.lastEvent.detail, eventDetail);

expect(eventSpy.lastEvent.detail).toEqual(eventDetail);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/testing/jest/jest-27-and-under/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { toEqualAttribute, toEqualAttributes, toHaveAttribute } from './attribut
import { toHaveClass, toHaveClasses, toMatchClasses } from './class-list';
import {
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toHaveReceivedEvent,
toHaveReceivedEventDetail,
Expand All @@ -25,6 +26,7 @@ export const expectExtend = {
toHaveReceivedEventDetail,
toHaveReceivedEventTimes,
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toMatchScreenshot,
};
27 changes: 27 additions & 0 deletions src/testing/jest/jest-28/matchers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ export function toHaveFirstReceivedEventDetail(eventSpy: d.EventSpy, eventDetail

const pass = deepEqual(eventSpy.firstEvent.detail, eventDetail);

expect(eventSpy.firstEvent.detail).toEqual(eventDetail);

return {
message: () => `expected event "${eventSpy.eventName}" detail to ${pass ? 'not ' : ''}equal`,
pass: pass,
};
}

export function toHaveLastReceivedEventDetail(eventSpy: d.EventSpy, eventDetail: any) {
if (!eventSpy) {
throw new Error(`toHaveLastReceivedEventDetail event spy is null`);
}

if (typeof (eventSpy as any).then === 'function') {
throw new Error(`event spy must be a resolved value, not a promise, before it can be tested`);
}

if (!eventSpy.eventName) {
throw new Error(`toHaveLastReceivedEventDetail did not receive an event spy`);
}

if (!eventSpy.firstEvent) {
throw new Error(`event "${eventSpy.eventName}" was not received`);
}

const pass = deepEqual(eventSpy.lastEvent.detail, eventDetail);

expect(eventSpy.lastEvent.detail).toEqual(eventDetail);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/testing/jest/jest-28/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { toEqualAttribute, toEqualAttributes, toHaveAttribute } from './attribut
import { toHaveClass, toHaveClasses, toMatchClasses } from './class-list';
import {
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toHaveReceivedEvent,
toHaveReceivedEventDetail,
Expand All @@ -25,6 +26,7 @@ export const expectExtend = {
toHaveReceivedEventDetail,
toHaveReceivedEventTimes,
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toMatchScreenshot,
};
27 changes: 27 additions & 0 deletions src/testing/jest/jest-29/matchers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ export function toHaveFirstReceivedEventDetail(eventSpy: d.EventSpy, eventDetail

const pass = deepEqual(eventSpy.firstEvent.detail, eventDetail);

expect(eventSpy.firstEvent.detail).toEqual(eventDetail);

return {
message: () => `expected event "${eventSpy.eventName}" detail to ${pass ? 'not ' : ''}equal`,
pass: pass,
};
}

export function toHaveLastReceivedEventDetail(eventSpy: d.EventSpy, eventDetail: any) {
if (!eventSpy) {
throw new Error(`toHaveLastReceivedEventDetail event spy is null`);
}

if (typeof (eventSpy as any).then === 'function') {
throw new Error(`event spy must be a resolved value, not a promise, before it can be tested`);
}

if (!eventSpy.eventName) {
throw new Error(`toHaveLastReceivedEventDetail did not receive an event spy`);
}

if (!eventSpy.firstEvent) {
throw new Error(`event "${eventSpy.eventName}" was not received`);
}

const pass = deepEqual(eventSpy.lastEvent.detail, eventDetail);

expect(eventSpy.lastEvent.detail).toEqual(eventDetail);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/testing/jest/jest-29/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { toEqualAttribute, toEqualAttributes, toHaveAttribute } from './attribut
import { toHaveClass, toHaveClasses, toMatchClasses } from './class-list';
import {
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toHaveReceivedEvent,
toHaveReceivedEventDetail,
Expand All @@ -25,6 +26,7 @@ export const expectExtend = {
toHaveReceivedEventDetail,
toHaveReceivedEventTimes,
toHaveFirstReceivedEventDetail,
toHaveLastReceivedEventDetail,
toHaveNthReceivedEventDetail,
toMatchScreenshot,
};
3 changes: 2 additions & 1 deletion test/end-to-end/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ export namespace Components {
interface EventCmp {
/**
* this is some method that fires an event with options
* @param mph some value
* @returns
*/
"methodThatFiresEventWithOptions": () => Promise<void>;
"methodThatFiresEventWithOptions": (mph: number) => Promise<void>;
/**
* this is some method that fires a document event
* @returns
Expand Down
12 changes: 7 additions & 5 deletions test/end-to-end/src/event-cmp/event-cmp.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('@Event', () => {
const elm = await page.find('event-cmp');
const elmEventSpy = await elm.spyOnEvent('my-event-with-options');

await elm.callMethod('methodThatFiresEventWithOptions');
await elm.callMethod('methodThatFiresEventWithOptions', 88);

expect(elmEventSpy).toHaveReceivedEventTimes(1);

Expand All @@ -92,11 +92,13 @@ describe('@Event', () => {
const elm = await page.find('event-cmp');
const elmEventSpy = await elm.spyOnEvent('my-event-with-options');

await elm.callMethod('methodThatFiresEventWithOptions');
await elm.callMethod('methodThatFiresEventWithOptions');
await elm.callMethod('methodThatFiresEventWithOptions');
await elm.callMethod('methodThatFiresEventWithOptions', 80);
await elm.callMethod('methodThatFiresEventWithOptions', 90);
await elm.callMethod('methodThatFiresEventWithOptions', 100);

expect(elmEventSpy).toHaveReceivedEventTimes(3);
expect(elmEventSpy).toHaveFirstReceivedEventDetail({ mph: 80 });
expect(elmEventSpy).toHaveLastReceivedEventDetail({ mph: 100 });
});

it('element spyOnEvent', async () => {
Expand All @@ -111,7 +113,7 @@ describe('@Event', () => {

expect(elmEventSpy).not.toHaveReceivedEvent();

await elm.callMethod('methodThatFiresEventWithOptions');
await elm.callMethod('methodThatFiresEventWithOptions', 88);

await page.waitForChanges();

Expand Down
5 changes: 3 additions & 2 deletions test/end-to-end/src/event-cmp/event-cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ export class EventCmp {

/**
* this is some method that fires an event with options
* @param mph some value
* @returns {void}
*/
@Method()
async methodThatFiresEventWithOptions() {
this.myEventWithOptions.emit({ mph: 88 });
async methodThatFiresEventWithOptions(mph: number) {
this.myEventWithOptions.emit({ mph });
}
}
8 changes: 7 additions & 1 deletion test/end-to-end/src/event-cmp/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@

## Methods

### `methodThatFiresEventWithOptions() => Promise<void>`
### `methodThatFiresEventWithOptions(mph: number) => Promise<void>`

this is some method that fires an event with options

#### Parameters

| Name | Type | Description |
| ----- | -------- | ----------- |
| `mph` | `number` | some value |

#### Returns

Type: `Promise<void>`
Expand Down

0 comments on commit 63491de

Please sign in to comment.