Skip to content

Commit

Permalink
Merge pull request #723 from aripalo/task/722-timeStamp-property-miss…
Browse files Browse the repository at this point in the history
…ing-from-event

#722@minor: Add timeStamp property to Event.
  • Loading branch information
capricorn86 committed Feb 13, 2023
2 parents c8f89f7 + 35782ad commit 5f304c7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
2 changes: 2 additions & 0 deletions packages/happy-dom/src/event/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import IWindow from '../window/IWindow';
import IShadowRoot from '../nodes/shadow-root/IShadowRoot';
import IEventTarget from './IEventTarget';
import NodeTypeEnum from '../nodes/node/NodeTypeEnum';
import { performance } from 'perf_hooks';

/**
* Event.
Expand All @@ -17,6 +18,7 @@ export default class Event {
public _propagationStopped = false;
public _target: IEventTarget = null;
public _currentTarget: IEventTarget = null;
public timeStamp: number = performance.now();
public type: string = null;

/**
Expand Down
20 changes: 20 additions & 0 deletions packages/happy-dom/test/event/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Window from '../../src/window/Window';
import IDocument from '../../src/nodes/document/IDocument';
import Event from '../../src/event/Event';
import CustomElement from '../CustomElement';
import { performance } from 'perf_hooks';

describe('Event', () => {
let window: IWindow;
Expand All @@ -15,6 +16,25 @@ describe('Event', () => {
window.customElements.define('custom-element', CustomElement);
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('get timeStamp()', () => {
it('Returns the value returned by performance.now() at the time it was created.', () => {
Object.defineProperty(performance, 'now', {
value: jest.fn(),
configurable: true,
writable: true
});

const performanceNow = 12345;
jest.spyOn(performance, 'now').mockImplementation(() => performanceNow);
const event = new Event('click');
expect(event.timeStamp).toBe(performanceNow);
});
});

describe('composedPath()', () => {
it('Returns a composed path.', () => {
const div = document.createElement('div');
Expand Down
24 changes: 10 additions & 14 deletions packages/happy-dom/test/nodes/html-element/HTMLElement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,22 +354,18 @@ describe('HTMLElement', () => {

describe('click()', () => {
it('Dispatches "click" event.', () => {
let triggeredEvent = null;
let triggeredEvent: PointerEvent = null;

element.addEventListener('click', (event) => {
triggeredEvent = event;
});
element.addEventListener('click', (event: PointerEvent) => (triggeredEvent = event));

element.click();

const event = new PointerEvent('click', {
bubbles: true,
composed: true
});
event._target = element;
event._currentTarget = element;

expect(triggeredEvent).toEqual(event);
expect(triggeredEvent instanceof PointerEvent).toBe(true);
expect(triggeredEvent.type).toBe('click');
expect(triggeredEvent.bubbles).toBe(true);
expect(triggeredEvent.composed).toBe(true);
expect(triggeredEvent.target === element).toBe(true);
expect(triggeredEvent.currentTarget === element).toBe(true);
});
});

Expand Down Expand Up @@ -401,7 +397,7 @@ describe('HTMLElement', () => {
expect(triggeredFocusOutEvent.composed).toBe(true);
expect(triggeredFocusOutEvent.target === element).toBe(true);

expect(document.activeElement).toEqual(document.body);
expect(document.activeElement === document.body).toBe(true);
});

it('Does not dispatch "blur" event if not connected to the DOM.', () => {
Expand Down Expand Up @@ -459,7 +455,7 @@ describe('HTMLElement', () => {
expect(triggeredFocusInEvent.composed).toBe(true);
expect(triggeredFocusInEvent.target === element).toBe(true);

expect(document.activeElement).toEqual(element);
expect(document.activeElement === element).toBe(true);
});

it('Does not dispatch "focus" event if not connected to the DOM.', () => {
Expand Down
12 changes: 12 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@
"lint": {
"outputs": []
},
"lint:fix": {
"outputs": []
},
"test": {
"outputs": []
},
"test:watch": {
"outputs": []
},
"version": {
"outputs": []
},
"publish": {
"outputs": []
}
}
}

0 comments on commit 5f304c7

Please sign in to comment.