-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from buttercup/feat/matching_improvements
Improved input handling/matching
- Loading branch information
Showing
8 changed files
with
126 additions
and
75 deletions.
There are no files selected for viewing
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
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,44 @@ | ||
import { LocustInputEvent } from "./LocustInputEvent.js"; | ||
|
||
const MAX_TYPE_WAIT = 20 | ||
const MIN_TYPE_WAIT = 2; | ||
|
||
const nativeInputValueSetter = Object.getOwnPropertyDescriptor( | ||
window.HTMLInputElement.prototype, | ||
"value" | ||
).set; | ||
|
||
async function sleep(time: number): Promise<void> { | ||
await new Promise<void>(resolve => { | ||
setTimeout(() => { | ||
resolve(); | ||
}, time); | ||
}); | ||
} | ||
|
||
export async function typeIntoInput(input: HTMLInputElement, value: string): Promise<void> { | ||
// Focus input | ||
const focusEvent = new FocusEvent("focus", { bubbles: true }); | ||
input.dispatchEvent(focusEvent); | ||
// Start typing | ||
const characters = value.split(""); | ||
let newValue = ""; | ||
while (characters.length > 0) { | ||
const char = characters.shift(); | ||
newValue = `${newValue}${char}`; | ||
input.setAttribute("value", newValue); | ||
nativeInputValueSetter.call(input, newValue); | ||
// Input event data takes the single new character | ||
const inputEvent = new LocustInputEvent("fill", "input", char, { bubbles: true }); | ||
input.dispatchEvent(inputEvent); | ||
// Wait | ||
const waitTime = Math.floor(Math.random() * (MAX_TYPE_WAIT - MIN_TYPE_WAIT)) + MIN_TYPE_WAIT; | ||
await sleep(waitTime); | ||
} | ||
// Blur input | ||
const blurEvent = new FocusEvent("blur", { bubbles: true }); | ||
input.dispatchEvent(blurEvent); | ||
// The change event gets all of the new data | ||
const changeEvent = new LocustInputEvent("fill", "change", value, { bubbles: true }); | ||
input.dispatchEvent(changeEvent); | ||
} |
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,56 @@ | ||
const { expect } = require("chai"); | ||
const sinon = require("sinon"); | ||
const { typeIntoInput } = require("../dist/typing.js"); | ||
|
||
describe("typing", function () { | ||
describe("typeIntoInput", function () { | ||
beforeEach(function () { | ||
this.input = document.createElement("input"); | ||
document.body.appendChild(this.input); | ||
}); | ||
|
||
afterEach(function () { | ||
document.body.removeChild(this.input); | ||
}); | ||
|
||
it("sets the input's value", async function () { | ||
expect(this.input.value).to.equal(""); | ||
await typeIntoInput(this.input, "new value"); | ||
expect(this.input.value).to.equal("new value"); | ||
}); | ||
|
||
it("fires the input's 'change' event", async function () { | ||
let entered = ""; | ||
const work = new Promise((resolve) => { | ||
this.input.addEventListener( | ||
"change", | ||
(event) => { | ||
entered = event.target.value; | ||
resolve(); | ||
}, | ||
false | ||
); | ||
}); | ||
await typeIntoInput(this.input, "123"); | ||
await work; | ||
expect(entered).to.equal("123"); | ||
}); | ||
|
||
it("fires the input's 'input' event", async function () { | ||
let typed = ""; | ||
const work = new Promise((resolve) => { | ||
this.input.addEventListener( | ||
"input", | ||
(event) => { | ||
typed = event.data; | ||
resolve(); | ||
}, | ||
false | ||
); | ||
}); | ||
await typeIntoInput(this.input, "4"); | ||
await work; | ||
expect(typed).to.equal("4"); | ||
}); | ||
}); | ||
}); |