Skip to content

Commit

Permalink
Merge c01feae into 180b3b1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Oct 27, 2023
2 parents 180b3b1 + c01feae commit 3db1ac7
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 9 deletions.
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
5 changes: 0 additions & 5 deletions compat/src/util.js
Expand Up @@ -22,11 +22,6 @@ export function shallowDiffers(a, b) {
return false;
}

export function removeNode(node) {
let parentNode = node.parentNode;
if (parentNode) parentNode.removeChild(node);
}

/**
* Check if two values are the same value
* @param {*} x
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);
});
});
11 changes: 10 additions & 1 deletion jsx-runtime/test/browser/jsx-runtime.test.js
@@ -1,15 +1,18 @@
import { Component, createElement, createRef } from 'preact';
import { Component, createElement, createRef, options } from 'preact';
import { jsx, jsxs, jsxDEV, Fragment } from 'preact/jsx-runtime';
import { setupScratch, teardown } from '../../../test/_util/helpers';

describe('Babel jsx/jsxDEV', () => {
let scratch;
let prevVNodeOption;

beforeEach(() => {
scratch = setupScratch();
prevVNodeOption = options.vnode;
});

afterEach(() => {
options.vnode = prevVNodeOption;
teardown(scratch);
});

Expand Down Expand Up @@ -91,4 +94,10 @@ describe('Babel jsx/jsxDEV', () => {
expect(vnode.props).to.deep.equal({});
expect(vnode.ref).to.equal(ref);
});

it('should call options.vnode with the vnode', () => {
options.vnode = sinon.spy();
const vnode = jsx('div', { class: 'foo' }, 'key');
expect(options.vnode).to.have.been.calledWith(vnode);
});
});
16 changes: 14 additions & 2 deletions test-utils/test/shared/act.test.js
Expand Up @@ -389,7 +389,7 @@ describe('act', () => {
act(() => {
tryRenderBroken();
});
}
};

describe('synchronously', () => {
it('should rethrow the exception', () => {
Expand All @@ -407,7 +407,7 @@ describe('act', () => {
renderWorking();
expect(scratch.textContent).to.equal('1');
});

it('should not affect effects in future renders', () => {
tryRenderBroken();
renderWorking();
Expand All @@ -432,6 +432,18 @@ describe('act', () => {
expect(err.message).to.equal('BrokenWidget is broken');
});

it('should rethrow the exception in nested act calls', async () => {
let err;
try {
await act(async () => {
await renderBrokenAsync();
});
} catch (e) {
err = e;
}
expect(err.message).to.equal('BrokenWidget is broken');
});

it('should not affect state updates in future renders', async () => {
try {
await renderBrokenAsync();
Expand Down

0 comments on commit 3db1ac7

Please sign in to comment.