Skip to content

Commit

Permalink
InputWritr: a first revamp (#319)
Browse files Browse the repository at this point in the history
Cleans up InputWritr a bit by improving some of the docs and function names.

Adds partial unit test coverage
Moves alias conversion to UserWrappr, where it belongs
Renames makePipe to createPipe (I like "create" more for this)_
Removes a few unused areas of code

Filed #317 to tackle a more complete overhaul of the package long term.
  • Loading branch information
JoshuaKGoldberg committed Aug 21, 2022
1 parent b0addb5 commit e6dc76e
Show file tree
Hide file tree
Showing 11 changed files with 280 additions and 314 deletions.
2 changes: 1 addition & 1 deletion docs/walkthrough/5. Inputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Inputs<Game extends FullScreenSaver> extends InputsBase<Game> {

gameWindow.addEventListener(
"keydown",
this.game.inputWriter.makePipe("onkeydown", "keyCode")
this.game.inputWriter.createPipe("onkeydown", "keyCode")
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/FullScreenSaver/src/sections/Inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Inputs<Game extends FullScreenSaver> extends InputsBase<Game> {

gameWindow.addEventListener(
"keydown",
this.game.inputWriter.makePipe("onkeydown", "keyCode")
this.game.inputWriter.createPipe("onkeydown", "keyCode")
);
}
}
4 changes: 2 additions & 2 deletions packages/eightbittr/src/sections/Inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export class Inputs<Game extends EightBittr> extends Section<Game> {
public readonly aliases?: Aliases;

/**
* Whether input events are allowed to trigger (by default, true).
* Whether input events are allowed to trigger.
*/
public readonly canInputsTrigger: boolean | CanTrigger = true;
public readonly canInputsTrigger?: CanTrigger;

/**
* Mapping of events to their key codes, to their callbacks.
Expand Down
2 changes: 1 addition & 1 deletion packages/inputwritr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![NPM version](https://badge.fury.io/js/inputwritr.svg)](http://badge.fury.io/js/inputwritr)
[![Join the chat at https://gitter.im/FullScreenShenanigans/community](https://badges.gitter.im/FullScreenShenanigans/community.svg)](https://gitter.im/FullScreenShenanigans/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

Pipes input events to action callbacks.
Pipes input DOM events to callbacks based on their device codes.

<!-- /Top -->

Expand Down
124 changes: 0 additions & 124 deletions packages/inputwritr/src/AliasConverter.ts

This file was deleted.

142 changes: 140 additions & 2 deletions packages/inputwritr/src/InputWritr.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,143 @@
import { expect } from "chai";
import * as sinon from "sinon";

import { InputWritr } from "./InputWritr";
import { InputWritrSettings } from "./types";

const createInputWritr = (overrides?: Partial<InputWritrSettings>) => {
const keyDownLeft = sinon.spy();
const inputWriter = new InputWritr({
aliases: {
keyDownLeft: [65],
},
triggers: {
onkeydown: { keyDownLeft },
},
...overrides,
});

return { inputWriter, keyDownLeft };
};

describe("InputWritr", () => {
it("_", () => {
/* ... */
describe("callEvent", () => {
it("does not throw an error when the event type does not exist", () => {
// Arrange
const { inputWriter } = createInputWritr();

// Act
const act = () => inputWriter.callEvent("onkeyup", 65);

// Assert
expect(act).not.to.throw();
});

it("does not throw an error when the key code does not exist ", () => {
// Arrange
const { inputWriter } = createInputWritr();

// Act
const act = () => inputWriter.callEvent("onkeydown", -1);

// Assert
expect(act).not.to.throw();
});

it("does not trigger the event when canTrigger returns false", () => {
// Arrange
const { inputWriter, keyDownLeft } = createInputWritr({
canTrigger: () => false,
});

// Act
inputWriter.callEvent("keyDownLeft", 65);

// Assert
expect(keyDownLeft).to.have.callCount(0);
});

it("triggers the event when canTrigger returns true", () => {
// Arrange
const { inputWriter, keyDownLeft } = createInputWritr({
canTrigger: () => true,
});

// Act
inputWriter.callEvent("onkeydown", 65);

// Assert
expect(keyDownLeft).to.have.callCount(1);
});
});

describe("createPipe", () => {
it("throws an error when the event type does not exist", () => {
// Arrange
const { inputWriter } = createInputWritr();

// Act
const act = () => inputWriter.createPipe("onkeyup", "keyCode");

// Assert
expect(act).to.throw(`No trigger of type 'onkeyup' defined.`);
});

it("does not call to the piped function when the event code label does not match", () => {
// Arrange
const { inputWriter, keyDownLeft } = createInputWritr();

const pipe = inputWriter.createPipe("onkeydown", "keyCode");
const event = new KeyboardEvent("onkeydown", { keyCode: -1 });

// Act
pipe(event);

// Assert
expect(keyDownLeft).to.have.callCount(0);
});

it("calls to the piped function when the event type and its code label match", () => {
// Arrange
const { keyDownLeft, inputWriter } = createInputWritr();

const pipe = inputWriter.createPipe("onkeydown", "keyCode");
const event = new KeyboardEvent("onkeydown", { keyCode: 65 });

// Act
pipe(event);

// Assert
expect(keyDownLeft).to.have.been.calledWith(event);
});

it("it does not call preventDefault when preventDefault is false", () => {
// Arrange
const { inputWriter } = createInputWritr();

const pipe = inputWriter.createPipe("onkeydown", "keyCode", false);
const event = new KeyboardEvent("onkeydown", { keyCode: 65 });
event.preventDefault = sinon.spy();

// Act
pipe(event);

// Assert
expect(event.preventDefault).to.have.callCount(0);
});

it("it calls preventDefault when preventDefault is true", () => {
// Arrange
const { inputWriter } = createInputWritr();

const pipe = inputWriter.createPipe("onkeydown", "keyCode", true);
const event = new KeyboardEvent("onkeydown", { keyCode: 65 });
event.preventDefault = sinon.spy();

// Act
pipe(event);

// Assert
expect(event.preventDefault).to.have.callCount(1);
});
});
});

0 comments on commit e6dc76e

Please sign in to comment.