Skip to content

Commit

Permalink
Merge pull request #1098 from Afsoon/task/1097-implements-symbol-toSt…
Browse files Browse the repository at this point in the history
…ringTag

#1097@patch: Implements Symbol.toStringTag on EventTarget and AbortSignal.
  • Loading branch information
capricorn86 committed Sep 27, 2023
2 parents c06f50d + c71a121 commit c68e51a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/happy-dom/src/event/EventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ export default abstract class EventTarget implements IEventTarget {
[k: string]: (IEventListenerOptions | null)[];
} = {};

/**
* Return a default description for the EventTarget class.
*/
public get [Symbol.toStringTag](): string {
return 'EventTarget';
}

/**
* Adds an event listener.
*
Expand Down
5 changes: 5 additions & 0 deletions packages/happy-dom/src/event/IEventTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import IEventListenerOptions from './IEventListenerOptions.js';
* Handles events.
*/
export default interface IEventTarget {
/**
* Return a default description for the EventTarget class.
*/
[Symbol.toStringTag]: string;

/**
* Adds an event listener.
*
Expand Down
7 changes: 7 additions & 0 deletions packages/happy-dom/src/fetch/AbortSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export default class AbortSignal extends EventTarget {
public readonly reason: string | null = null;
public onabort: ((this: AbortSignal, event: Event) => void) | null = null;

/**
* Return a default description for the AbortSignal class.
*/
public get [Symbol.toStringTag](): string {
return 'AbortSignal';
}

/**
* Aborts the signal.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/happy-dom/test/event/EventTarget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,12 @@ describe('EventTarget', () => {
expect(recievedEvent).toBe(null);
});
});

describe('[Symbol.toStringTag]', () => {
it('Returns EventTarget string.', () => {
const description = 'EventTarget';

expect(eventTarget[Symbol.toStringTag]).toBe(description);
});
});
});
9 changes: 9 additions & 0 deletions packages/happy-dom/test/fetch/AbortSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,13 @@ describe('AbortSignal', () => {
expect(signal.reason).toBe(reason);
});
});

describe('AbortSignal[Symbol.toStringTag]', () => {
it('Returns AbortSignal string.', () => {
const description = 'AbortSignal';
const signal = new AbortSignal();

expect(signal[Symbol.toStringTag]).toBe(description);
});
});
});

0 comments on commit c68e51a

Please sign in to comment.