-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
afterEach, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
expect, | ||
test, | ||
} from "vitest"; | ||
import { initAutosubmit } from "./autosubmit"; | ||
|
||
describe("autosubmit", () => { | ||
beforeAll(() => { | ||
initAutosubmit(); | ||
}); | ||
|
||
beforeEach(() => { | ||
// prevent HTMLFormElement.prototype.requestSubmit is not implemented log. | ||
// | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
window._virtualConsole.emit = () => {}; | ||
}); | ||
|
||
afterEach(() => { | ||
while (document.body.firstChild) { | ||
document.body.firstChild.remove(); | ||
} | ||
}); | ||
|
||
test("auto-submits text input", async () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<input type="text" data-auto-submit="submitter" /> | ||
<button type="submit" id="submitter">Submit</button> | ||
</form> | ||
`; | ||
|
||
let submitter; | ||
|
||
document.querySelector("form").addEventListener("submit", function (event) { | ||
submitter = event.submitter; | ||
}); | ||
|
||
const inputElement = document.querySelector("input"); | ||
inputElement.value = "awesome text"; | ||
inputElement.dispatchEvent(new InputEvent("input", { bubbles: true })); | ||
|
||
await wait(); | ||
|
||
expect(submitter).toBe(document.querySelector("button")); | ||
}); | ||
|
||
test("auto-submits text input with custom delay", async () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<input type="text" data-auto-submit="submitter" data-auto-submit-delay="100" /> | ||
<button type="submit" id="submitter">Submit</button> | ||
</form> | ||
`; | ||
|
||
let submitter; | ||
|
||
document.querySelector("form").addEventListener("submit", function (event) { | ||
submitter = event.submitter; | ||
}); | ||
|
||
const inputElement = document.querySelector("input"); | ||
inputElement.value = "awesome text"; | ||
inputElement.dispatchEvent(new InputEvent("input", { bubbles: true })); | ||
|
||
await wait(); | ||
expect(submitter).toBeUndefined(); | ||
|
||
await wait(100); | ||
expect(submitter).toBe(document.querySelector("button")); | ||
}); | ||
}); | ||
|
||
function wait(delay = 0) { | ||
return new Promise((resolve) => setTimeout(resolve, delay)); | ||
} |