Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix-force-flag
- Loading branch information
Showing
13 changed files
with
202 additions
and
10 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 |
---|---|---|
|
@@ -31,4 +31,4 @@ | |
"$_processingException": "__p" | ||
} | ||
} | ||
} | ||
} |
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
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,19 @@ | ||
{ | ||
"name": "test-utils", | ||
"amdName": "preactTestUtils", | ||
"version": "0.1.0", | ||
"private": true, | ||
"description": "Test-utils for Preact", | ||
"main": "dist/testUtils.js", | ||
"module": "dist/testUtils.mjs", | ||
"umd:main": "dist/testUtils.umd.js", | ||
"source": "src/index.js", | ||
"license": "MIT", | ||
"types": "src/index.d.ts", | ||
"peerDependencies": { | ||
"preact": "^10.0.0-alpha.0" | ||
}, | ||
"mangle": { | ||
"regex": "^_" | ||
} | ||
} |
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,2 @@ | ||
export function setupRerender(): void; | ||
export function act(callback: () => void): void; |
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,26 @@ | ||
import { Component, options } from 'preact'; | ||
|
||
/** | ||
* Setup a rerender function that will drain the queue of pending renders | ||
* @returns {() => void} | ||
*/ | ||
export function setupRerender() { | ||
Component.__test__previousDebounce = options.debounceRendering; | ||
options.debounceRendering = cb => Component.__test__drainQueue = cb; | ||
return () => Component.__test__drainQueue && Component.__test__drainQueue(); | ||
} | ||
|
||
export function act(cb) { | ||
const previousDebounce = options.debounceRendering; | ||
const previousRequestAnimationFrame = options.requestAnimationFrame; | ||
const rerender = setupRerender(); | ||
let flush; | ||
options.requestAnimationFrame = (fc) => flush = fc; | ||
cb(); | ||
if (flush) { | ||
flush(); | ||
} | ||
rerender(); | ||
options.debounceRendering = previousDebounce; | ||
options.requestAnimationFrame = previousRequestAnimationFrame; | ||
} |
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,77 @@ | ||
import { options, createElement as h, render } from 'preact'; | ||
import sinon from 'sinon'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
|
||
import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
import { act } from '../../src'; | ||
|
||
/** @jsx h */ | ||
describe('act', () => { | ||
|
||
/** @type {HTMLDivElement} */ | ||
let scratch; | ||
|
||
beforeEach(() => { | ||
scratch = setupScratch(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardown(scratch); | ||
}); | ||
|
||
it('should reset options after act finishes', () => { | ||
expect(options.requestAnimationFrame).to.equal(undefined); | ||
act(() => null); | ||
expect(options.requestAnimationFrame).to.equal(undefined); | ||
}); | ||
|
||
it('should drain the queue of hooks', () => { | ||
function StateContainer() { | ||
const [count, setCount] = useState(0); | ||
return (<div> | ||
<p>Count: {count}</p> | ||
<button onClick={() => setCount(c => c + 11)} /> | ||
</div>); | ||
} | ||
|
||
render(<StateContainer />, scratch); | ||
expect(scratch.textContent).to.include('Count: 0'); | ||
act(() => { | ||
const button = scratch.querySelector('button'); | ||
button.click(); | ||
expect(scratch.textContent).to.include('Count: 0'); | ||
}); | ||
expect(scratch.textContent).to.include('Count: 1'); | ||
}); | ||
|
||
it('should flush pending effects', () => { | ||
let spy = sinon.spy(); | ||
function StateContainer() { | ||
useEffect(spy); | ||
return <div />; | ||
} | ||
|
||
act(() => render(<StateContainer />, scratch)); | ||
expect(spy).to.be.calledOnce; | ||
}); | ||
|
||
it('should restore options.requestAnimationFrame', () => { | ||
const spy = sinon.spy(); | ||
|
||
options.requestAnimationFrame = spy; | ||
act(() => null); | ||
|
||
expect(options.requestAnimationFrame).to.equal(spy); | ||
expect(spy).to.not.be.called; | ||
}); | ||
|
||
it('should restore options.debounceRendering after act', () => { | ||
const spy = sinon.spy(); | ||
|
||
options.debounceRendering = spy; | ||
act(() => null); | ||
|
||
expect(options.debounceRendering).to.equal(spy); | ||
expect(spy).to.not.be.called; | ||
}); | ||
}); |
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