diff --git a/index.test.ts b/index.test.ts index d1a1d86..af9453b 100644 --- a/index.test.ts +++ b/index.test.ts @@ -76,7 +76,11 @@ function getState(field: HTMLElement) { } for (const type of ['textarea', 'input', 'contenteditable'] as const) { - test(`${type}: harness test`, t => { + test(`${type}: harness test`, async t => { + // Reset the document focus + (document.activeElement as HTMLElement)?.blur?.(); + document.body.focus(); + t.equal(getState(getField(type)), '|'); t.equal(getState(getField(type, '|')), '|'); t.equal(getState(getField(type, 'A|')), 'A|'); diff --git a/index.ts b/index.ts index 277f6da..3d8dac1 100644 --- a/index.ts +++ b/index.ts @@ -15,8 +15,8 @@ function withFocus(field: HTMLElement, callback: () => T): T { field.focus(); return callback(); } finally { + field.blur(); // Supports `intialFocus === body` if (initialFocus instanceof HTMLElement) { - field.blur(); // Supports `intialFocus === body` initialFocus.focus(); } }