|
1 | 1 | import { options, createElement as h, render } from 'preact';
|
2 |
| -import { useEffect, useState } from 'preact/hooks'; |
| 2 | +import { useEffect, useReducer, useState } from 'preact/hooks'; |
3 | 3 |
|
4 | 4 | import { setupScratch, teardown } from '../../../test/_util/helpers';
|
5 | 5 | import { act } from '../../src';
|
@@ -238,4 +238,97 @@ describe('act', () => {
|
238 | 238 | 'act result resolved'
|
239 | 239 | ]);
|
240 | 240 | });
|
| 241 | + |
| 242 | + context('when `act` calls are nested', () => { |
| 243 | + it('should invoke nested sync callback and return a Promise', () => { |
| 244 | + let innerResult; |
| 245 | + const spy = sinon.stub(); |
| 246 | + |
| 247 | + act(() => { |
| 248 | + innerResult = act(spy); |
| 249 | + }); |
| 250 | + |
| 251 | + expect(spy).to.be.calledOnce; |
| 252 | + expect(innerResult.then).to.be.a('function'); |
| 253 | + }); |
| 254 | + |
| 255 | + it('should invoke nested async callback and return a Promise', async () => { |
| 256 | + const events = []; |
| 257 | + |
| 258 | + await act(async () => { |
| 259 | + events.push('began outer act callback'); |
| 260 | + await act(async () => { |
| 261 | + events.push('began inner act callback'); |
| 262 | + await Promise.resolve(); |
| 263 | + events.push('end inner act callback'); |
| 264 | + }); |
| 265 | + events.push('end outer act callback'); |
| 266 | + }); |
| 267 | + events.push('act finished'); |
| 268 | + |
| 269 | + expect(events).to.deep.equal([ |
| 270 | + 'began outer act callback', |
| 271 | + 'began inner act callback', |
| 272 | + 'end inner act callback', |
| 273 | + 'end outer act callback', |
| 274 | + 'act finished' |
| 275 | + ]); |
| 276 | + }); |
| 277 | + |
| 278 | + it('should only flush effects when outer `act` call returns', () => { |
| 279 | + let counter = 0; |
| 280 | + |
| 281 | + function Widget() { |
| 282 | + useEffect(() => { |
| 283 | + ++counter; |
| 284 | + }); |
| 285 | + const [, forceUpdate] = useReducer(x => x + 1, 0); |
| 286 | + return <button onClick={forceUpdate}>test</button>; |
| 287 | + } |
| 288 | + |
| 289 | + act(() => { |
| 290 | + render(<Widget />, scratch); |
| 291 | + const button = scratch.querySelector('button'); |
| 292 | + expect(counter).to.equal(0); |
| 293 | + |
| 294 | + act(() => { |
| 295 | + button.dispatchEvent(new Event('click')); |
| 296 | + }); |
| 297 | + |
| 298 | + // Effect triggered by inner `act` call should not have been |
| 299 | + // flushed yet. |
| 300 | + expect(counter).to.equal(0); |
| 301 | + }); |
| 302 | + |
| 303 | + // Effects triggered by inner `act` call should not have been |
| 304 | + // flushed yet. |
| 305 | + expect(counter).to.equal(2); |
| 306 | + }); |
| 307 | + |
| 308 | + it('should only flush updates when outer `act` call returns', () => { |
| 309 | + function Button() { |
| 310 | + const [count, setCount] = useState(0); |
| 311 | + const increment = () => setCount(count => count + 1); |
| 312 | + return <button onClick={increment}>{count}</button>; |
| 313 | + } |
| 314 | + |
| 315 | + render(<Button />, scratch); |
| 316 | + const button = scratch.querySelector('button'); |
| 317 | + expect(button.textContent).to.equal('0'); |
| 318 | + |
| 319 | + act(() => { |
| 320 | + act(() => { |
| 321 | + button.dispatchEvent(new Event('click')); |
| 322 | + }); |
| 323 | + |
| 324 | + // Update triggered by inner `act` call should not have been |
| 325 | + // flushed yet. |
| 326 | + expect(button.textContent).to.equal('0'); |
| 327 | + }); |
| 328 | + |
| 329 | + // Updates from outer and inner `act` calls should now have been |
| 330 | + // flushed. |
| 331 | + expect(button.textContent).to.equal('1'); |
| 332 | + }); |
| 333 | + }); |
241 | 334 | });
|
0 commit comments