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

Migrate input unit tests from Karma to Node #6133

Merged
merged 2 commits into from
Mar 10, 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
419 changes: 417 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@
"chai": "^4.3.10",
"eslint": "^8.52.0",
"fflate": "^0.8.1",
"global-jsdom": "^24.0.0",
"jsdoc": "^4.0.2",
"jsdoc-tsimport-plugin": "^1.0.5",
"jsdoc-typeof-plugin": "^1.0.0",
"jsdom": "^24.0.0",
"karma": "^6.4.2",
"karma-chrome-launcher": "^3.2.0",
"karma-mocha": "2.0.1",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import handler from 'serve-handler';
import http from 'http';
import XMLHttpRequest from 'xhr2';

import 'global-jsdom/register'; // eslint-disable-line import/no-unresolved,import/extensions

let server;

export const mochaGlobalSetup = () => {
Expand Down
139 changes: 139 additions & 0 deletions test/platform/input/keyboard.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { Keyboard } from '../../../src/platform/input/keyboard.js';
import { EVENT_KEYDOWN, EVENT_KEYUP, KEY_UP } from '../../../src/platform/input/constants.js';

import { expect } from 'chai';

describe('Keyboard', function () {

/** @type { Keyboard } */
let keyboard;

beforeEach(function () {
keyboard = new Keyboard();
keyboard.attach(window);
});

afterEach(function () {
keyboard.detach();
});

describe('#constructor', function () {

it('should create a new instance', function () {
expect(keyboard).to.be.an.instanceOf(Keyboard);
});

});

describe('#isPressed', function () {

it('should return false for a key that is not pressed', function () {
expect(keyboard.isPressed(KEY_UP)).to.be.false;
});

it('should return true for a key that is pressed', function () {
const keyDownEvent = new KeyboardEvent('keydown', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyDownEvent);

expect(keyboard.isPressed(KEY_UP)).to.be.true;

keyboard.update();

expect(keyboard.isPressed(KEY_UP)).to.be.true;

const keyUpEvent = new KeyboardEvent('keyup', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyUpEvent);

expect(keyboard.isPressed(KEY_UP)).to.be.false;
});

});

describe('#on', function () {

it('should handle keydown events', function (done) {
keyboard.on(EVENT_KEYDOWN, function (event) {
expect(event.key).to.equal(KEY_UP);
expect(event.element).to.equal(window);
expect(event.event).to.be.an.instanceOf(KeyboardEvent);

done();
});

const keyDownEvent = new KeyboardEvent('keydown', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyDownEvent);
});

it('should handle keyup events', function (done) {
keyboard.on(EVENT_KEYUP, function (event) {
expect(event.key).to.equal(KEY_UP);
expect(event.element).to.equal(window);
expect(event.event).to.be.an.instanceOf(KeyboardEvent);

done();
});

const keyUpEvent = new KeyboardEvent('keyup', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyUpEvent);
});

});

describe('#wasPressed', function () {

it('should return false for a key that was not pressed', function () {
expect(keyboard.wasPressed(KEY_UP)).to.be.false;
});

it('should return true for a key that was pressed since the last update', function () {
const keyDownEvent = new KeyboardEvent('keydown', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyDownEvent);

expect(keyboard.wasPressed(KEY_UP)).to.be.true;

keyboard.update();

expect(keyboard.wasPressed(KEY_UP)).to.be.false;
});

});

describe('#wasReleased', function () {

it('should return false for a key that was not released', function () {
expect(keyboard.wasReleased(KEY_UP)).to.be.false;
});

it('should return true for a key that was released since the last update', function () {
const keyDownEvent = new KeyboardEvent('keydown', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyDownEvent);

keyboard.update();

const keyUpEvent = new KeyboardEvent('keyup', {
keyCode: 38 // Up arrow
});
window.dispatchEvent(keyUpEvent);

expect(keyboard.wasReleased(KEY_UP)).to.be.true;

keyboard.update();

expect(keyboard.wasReleased(KEY_UP)).to.be.false;
});

});

});
140 changes: 140 additions & 0 deletions test/platform/input/mouse.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Mouse } from '../../../src/platform/input/mouse.js';
import {
EVENT_MOUSEDOWN, EVENT_MOUSEUP,
MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT
} from '../../../src/platform/input/constants.js';

import { expect } from 'chai';

const buttons = [MOUSEBUTTON_LEFT, MOUSEBUTTON_MIDDLE, MOUSEBUTTON_RIGHT];

// Mock the _getTargetCoords method, otherwise it returns null
Mouse.prototype._getTargetCoords = function (event) {
return { x: 0, y: 0 };
};

describe('Mouse', function () {

/** @type { Mouse } */
let mouse;

beforeEach(function () {
mouse = new Mouse(document.body);
});

afterEach(function () {
mouse.detach();
});

describe('#constructor', function () {

it('should create a new instance', function () {
expect(mouse).to.be.an.instanceOf(Mouse);
});

});

describe('#isPressed', function () {

it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.isPressed(button)).to.be.false;
}
});

it('should return true for a mouse button that is pressed', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);

expect(mouse.isPressed(button)).to.be.true;

const mouseUpEvent = new MouseEvent('mouseup', { button });
window.dispatchEvent(mouseUpEvent);

expect(mouse.isPressed(button)).to.be.false;
}
});

});

describe('#on', function () {

it('should handle mousedown events', function (done) {
mouse.on(EVENT_MOUSEDOWN, function (event) {
expect(event.button).to.equal(MOUSEBUTTON_LEFT);
expect(event.event).to.be.an.instanceOf(MouseEvent);

done();
});

const mouseDownEvent = new MouseEvent('mousedown', { button: 0 });
window.dispatchEvent(mouseDownEvent);
});

it('should handle mouseup events', function (done) {
mouse.on(EVENT_MOUSEUP, function (event) {
expect(event.button).to.equal(MOUSEBUTTON_LEFT);
expect(event.event).to.be.an.instanceOf(MouseEvent);

done();
});

const mouseUpEvent = new MouseEvent('mouseup', { button: 0 });
window.dispatchEvent(mouseUpEvent);
});

});

describe('#wasPressed', function () {

it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.wasPressed(button)).to.be.false;
}
});

it('should return true for a mouse button that was pressed', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);

expect(mouse.wasPressed(button)).to.be.true;

mouse.update();

expect(mouse.wasPressed(button)).to.be.false;
}
});

});

describe('#wasReleased', function () {

it('should return false for all buttons by default', function () {
for (const button of buttons) {
expect(mouse.wasReleased(button)).to.be.false;
}
});

it('should return true for a mouse button that was released', function () {
for (const button of buttons) {
const mouseDownEvent = new MouseEvent('mousedown', { button });
window.dispatchEvent(mouseDownEvent);

mouse.update();

const mouseUpEvent = new MouseEvent('mouseup', { button });
window.dispatchEvent(mouseUpEvent);

expect(mouse.wasReleased(button)).to.be.true;

mouse.update();

expect(mouse.wasReleased(button)).to.be.false;
}
});

});

});
84 changes: 0 additions & 84 deletions tests/platform/input/simulate_event.js

This file was deleted.

6 changes: 0 additions & 6 deletions tests/platform/input/test_input.js

This file was deleted.

Loading