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(testing): add toHaveLastReceivedEventDetail event spy matcher #5829

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
6 changes: 6 additions & 0 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,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);
Copy link
Contributor Author

@tanner-reits tanner-reits Jun 13, 2024

Choose a reason for hiding this comment

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

Don't like how Git is showing the diff, but this line used to be: expect(eventSpy.lastEvent.detail).toEqual(eventDetail)

Changed it to use firstEvent, since, well, this is supposed to be checking what event data the spy first received. Idk why this was lastEvent before, but, in testing, I setup a component to emit multiple events and checked that the assertions using toHaveFirstReceivedEventDetail and toHaveLastReceivedEventDetail were working as expected with the emitted values.

I'm not sure if this is a breaking change, but seems like the original implementation was wrong in the first place 😅

(made the change for each adapter version)


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