Skip to content

Commit

Permalink
Improve coverage for compat
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Oct 27, 2023
1 parent 3534a4f commit 2ff48c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions compat/src/index.d.ts
Expand Up @@ -168,6 +168,9 @@ declare namespace React {
| MutableRefObject<T | null>
| null;

export function flushSync<R>(fn: () => R): R;
export function flushSync<A, R>(fn: (a: A) => R, a: A): R;

export function unstable_batchedUpdates(
callback: (arg?: any) => void,
arg?: any
Expand Down
14 changes: 14 additions & 0 deletions compat/test/browser/events.test.js
Expand Up @@ -295,4 +295,18 @@ describe('preact/compat events', () => {
scratch.firstChild.dispatchEvent(createEvent('compositionstart'));
expect(spy).to.be.calledOnce;
});

it('should normalize onFocus to onfocusin', () => {
let spy = sinon.spy();
render(<input onFocus={spy} />, scratch);
scratch.firstChild.dispatchEvent(createEvent('focusin'));
expect(spy).to.be.calledOnce;
});

it('should normalize onBlur to onfocusout', () => {
let spy = sinon.spy();
render(<input onBlur={spy} />, scratch);
scratch.firstChild.dispatchEvent(createEvent('focusout'));
expect(spy).to.be.calledOnce;
});
});
20 changes: 19 additions & 1 deletion compat/test/browser/unstable_batchedUpdates.test.js
@@ -1,4 +1,4 @@
import { unstable_batchedUpdates } from 'preact/compat';
import { unstable_batchedUpdates, flushSync } from 'preact/compat';

describe('unstable_batchedUpdates', () => {
it('should call the callback', () => {
Expand All @@ -13,3 +13,21 @@ describe('unstable_batchedUpdates', () => {
expect(spy).to.be.calledWithExactly('foo');
});
});

describe('flushSync', () => {
it('should invoke the given callback', () => {
const returnValue = {};
const spy = sinon.spy(() => returnValue);
const result = flushSync(spy);
expect(spy).to.have.been.calledOnce;
expect(result).to.equal(returnValue);
});

it('should invoke the given callback with the given argument', () => {
const returnValue = {};
const spy = sinon.spy(() => returnValue);
const result = flushSync(spy, 'foo');
expect(spy).to.be.calledWithExactly('foo');
expect(result).to.equal(returnValue);
});
});

0 comments on commit 2ff48c9

Please sign in to comment.