Skip to content

Commit

Permalink
Migrate input unit tests from Karma to Node
Browse files Browse the repository at this point in the history
  • Loading branch information
willeastcott committed Mar 9, 2024
1 parent ce43116 commit deef8ca
Show file tree
Hide file tree
Showing 9 changed files with 700 additions and 446 deletions.
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'

Check failure on line 5 in test/fixtures.mjs

View workflow job for this annotation

GitHub Actions / Lint

Unable to resolve path to module 'global-jsdom/register'

Check failure on line 5 in test/fixtures.mjs

View workflow job for this annotation

GitHub Actions / Lint

Missing file extension for "global-jsdom/register"

Check failure on line 5 in test/fixtures.mjs

View workflow job for this annotation

GitHub Actions / Lint

Missing semicolon

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

Check failure on line 68 in test/platform/input/keyboard.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Unexpected trailing comma
});
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

Check failure on line 83 in test/platform/input/keyboard.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Unexpected trailing comma
});
window.dispatchEvent(keyUpEvent);
});

});

describe('#wasPressed', function () {

Check failure on line 91 in test/platform/input/keyboard.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
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;
});

});

});

Check failure on line 139 in test/platform/input/keyboard.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Newline required at end of file but not found
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_MOUSEMOVE, EVENT_MOUSEUP,

Check failure on line 3 in test/platform/input/mouse.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

'EVENT_MOUSEMOVE' is defined but never used
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 () {

Check failure on line 90 in test/platform/input/mouse.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Trailing spaces not allowed
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;
}
});

});

});

Check failure on line 140 in test/platform/input/mouse.test.mjs

View workflow job for this annotation

GitHub Actions / Lint

Newline required at end of file but not found
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

0 comments on commit deef8ca

Please sign in to comment.