-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate input unit tests from Karma to Node (#6133)
* Migrate input unit tests from Karma to Node * Lint fixes
- Loading branch information
1 parent
ce43116
commit b9c56b1
Showing
9 changed files
with
700 additions
and
446 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.