-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for async act
callbacks and nested calls to act
#1854
Changes from 3 commits
066c789
5658e22
a66efcb
921ba36
104b02d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { options, createElement as h, render } from 'preact'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
import { useEffect, useReducer, useState } from 'preact/hooks'; | ||
|
||
import { setupScratch, teardown } from '../../../test/_util/helpers'; | ||
import { act } from '../../src'; | ||
|
@@ -191,4 +191,144 @@ describe('act', () => { | |
}); | ||
expect(scratch.firstChild.textContent).to.equal('1'); | ||
}); | ||
|
||
it('returns a Promise if invoked with a sync callback', () => { | ||
const result = act(() => {}); | ||
expect(result.then).to.be.a('function'); | ||
return result; | ||
}); | ||
|
||
it('returns a Promise if invoked with an async callback', () => { | ||
const result = act(async () => {}); | ||
expect(result.then).to.be.a('function'); | ||
return result; | ||
}); | ||
|
||
it('should await "thenable" result of callback before flushing', async () => { | ||
const events = []; | ||
|
||
function TestComponent() { | ||
useEffect(() => { | ||
events.push('flushed effect'); | ||
}, []); | ||
events.push('scheduled effect'); | ||
return <div>Test</div>; | ||
} | ||
|
||
const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
events.push('began test'); | ||
const acted = act(async () => { | ||
events.push('began act callback'); | ||
await delay(1); | ||
render(<TestComponent />, scratch); | ||
events.push('end act callback'); | ||
}); | ||
events.push('act returned'); | ||
await acted; | ||
events.push('act result resolved'); | ||
|
||
expect(events).to.deep.equal([ | ||
'began test', | ||
'began act callback', | ||
'act returned', | ||
'scheduled effect', | ||
'end act callback', | ||
'flushed effect', | ||
'act result resolved' | ||
]); | ||
}); | ||
|
||
context('when `act` calls are nested', () => { | ||
it('should invoke nested sync callback and return a Promise', () => { | ||
let innerResult; | ||
const spy = sinon.stub(); | ||
|
||
act(() => { | ||
innerResult = act(spy); | ||
}); | ||
|
||
expect(spy).to.be.calledOnce; | ||
expect(innerResult.then).to.be.a('function'); | ||
}); | ||
|
||
it('should invoke nested async callback and return a Promise', async () => { | ||
const events = []; | ||
|
||
await act(async () => { | ||
events.push('began outer act callback'); | ||
await act(async () => { | ||
events.push('began inner act callback'); | ||
await Promise.resolve(); | ||
events.push('end inner act callback'); | ||
}); | ||
events.push('end outer act callback'); | ||
}); | ||
events.push('act finished'); | ||
|
||
expect(events).to.deep.equal([ | ||
'began outer act callback', | ||
'began inner act callback', | ||
'end inner act callback', | ||
'end outer act callback', | ||
'act finished' | ||
]); | ||
}); | ||
|
||
it('should only flush effects when outer `act` call returns', () => { | ||
let counter = 0; | ||
|
||
function Widget() { | ||
useEffect(() => { | ||
++counter; | ||
}); | ||
const [, forceUpdate] = useReducer(x => x + 1, 0); | ||
return <button onClick={forceUpdate}>test</button>; | ||
} | ||
|
||
act(() => { | ||
render(<Widget />, scratch); | ||
const button = scratch.querySelector('button'); | ||
expect(counter).to.equal(0); | ||
|
||
act(() => { | ||
button.dispatchEvent(new Event('click')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same IE11 issue as below |
||
}); | ||
|
||
// Effect triggered by inner `act` call should not have been | ||
// flushed yet. | ||
expect(counter).to.equal(0); | ||
}); | ||
|
||
// Effects triggered by inner `act` call should now have been | ||
// flushed. | ||
expect(counter).to.equal(2); | ||
}); | ||
|
||
it('should only flush updates when outer `act` call returns', () => { | ||
function Button() { | ||
const [count, setCount] = useState(0); | ||
const increment = () => setCount(count => count + 1); | ||
return <button onClick={increment}>{count}</button>; | ||
} | ||
|
||
render(<Button />, scratch); | ||
const button = scratch.querySelector('button'); | ||
expect(button.textContent).to.equal('0'); | ||
|
||
act(() => { | ||
act(() => { | ||
button.dispatchEvent(new Event('click')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will fail in IE11 because it doesn't support There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this. I've updated the code to use the helper. |
||
}); | ||
|
||
// Update triggered by inner `act` call should not have been | ||
// flushed yet. | ||
expect(button.textContent).to.equal('0'); | ||
}); | ||
|
||
// Updates from outer and inner `act` calls should now have been | ||
// flushed. | ||
expect(button.textContent).to.equal('1'); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super super tiny nit: We can combine this line and the above one: