|
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';
|
@@ -200,4 +200,144 @@ describe('act', () => {
|
200 | 200 | });
|
201 | 201 | expect(scratch.firstChild.textContent).to.equal('1');
|
202 | 202 | });
|
| 203 | + |
| 204 | + it('returns a Promise if invoked with a sync callback', () => { |
| 205 | + const result = act(() => {}); |
| 206 | + expect(result.then).to.be.a('function'); |
| 207 | + return result; |
| 208 | + }); |
| 209 | + |
| 210 | + it('returns a Promise if invoked with an async callback', () => { |
| 211 | + const result = act(async () => {}); |
| 212 | + expect(result.then).to.be.a('function'); |
| 213 | + return result; |
| 214 | + }); |
| 215 | + |
| 216 | + it('should await "thenable" result of callback before flushing', async () => { |
| 217 | + const events = []; |
| 218 | + |
| 219 | + function TestComponent() { |
| 220 | + useEffect(() => { |
| 221 | + events.push('flushed effect'); |
| 222 | + }, []); |
| 223 | + events.push('scheduled effect'); |
| 224 | + return <div>Test</div>; |
| 225 | + } |
| 226 | + |
| 227 | + const delay = ms => new Promise(resolve => setTimeout(resolve, ms)); |
| 228 | + |
| 229 | + events.push('began test'); |
| 230 | + const acted = act(async () => { |
| 231 | + events.push('began act callback'); |
| 232 | + await delay(1); |
| 233 | + render(<TestComponent />, scratch); |
| 234 | + events.push('end act callback'); |
| 235 | + }); |
| 236 | + events.push('act returned'); |
| 237 | + await acted; |
| 238 | + events.push('act result resolved'); |
| 239 | + |
| 240 | + expect(events).to.deep.equal([ |
| 241 | + 'began test', |
| 242 | + 'began act callback', |
| 243 | + 'act returned', |
| 244 | + 'scheduled effect', |
| 245 | + 'end act callback', |
| 246 | + 'flushed effect', |
| 247 | + 'act result resolved' |
| 248 | + ]); |
| 249 | + }); |
| 250 | + |
| 251 | + context('when `act` calls are nested', () => { |
| 252 | + it('should invoke nested sync callback and return a Promise', () => { |
| 253 | + let innerResult; |
| 254 | + const spy = sinon.stub(); |
| 255 | + |
| 256 | + act(() => { |
| 257 | + innerResult = act(spy); |
| 258 | + }); |
| 259 | + |
| 260 | + expect(spy).to.be.calledOnce; |
| 261 | + expect(innerResult.then).to.be.a('function'); |
| 262 | + }); |
| 263 | + |
| 264 | + it('should invoke nested async callback and return a Promise', async () => { |
| 265 | + const events = []; |
| 266 | + |
| 267 | + await act(async () => { |
| 268 | + events.push('began outer act callback'); |
| 269 | + await act(async () => { |
| 270 | + events.push('began inner act callback'); |
| 271 | + await Promise.resolve(); |
| 272 | + events.push('end inner act callback'); |
| 273 | + }); |
| 274 | + events.push('end outer act callback'); |
| 275 | + }); |
| 276 | + events.push('act finished'); |
| 277 | + |
| 278 | + expect(events).to.deep.equal([ |
| 279 | + 'began outer act callback', |
| 280 | + 'began inner act callback', |
| 281 | + 'end inner act callback', |
| 282 | + 'end outer act callback', |
| 283 | + 'act finished' |
| 284 | + ]); |
| 285 | + }); |
| 286 | + |
| 287 | + it('should only flush effects when outer `act` call returns', () => { |
| 288 | + let counter = 0; |
| 289 | + |
| 290 | + function Widget() { |
| 291 | + useEffect(() => { |
| 292 | + ++counter; |
| 293 | + }); |
| 294 | + const [, forceUpdate] = useReducer(x => x + 1, 0); |
| 295 | + return <button onClick={forceUpdate}>test</button>; |
| 296 | + } |
| 297 | + |
| 298 | + act(() => { |
| 299 | + render(<Widget />, scratch); |
| 300 | + const button = scratch.querySelector('button'); |
| 301 | + expect(counter).to.equal(0); |
| 302 | + |
| 303 | + act(() => { |
| 304 | + button.dispatchEvent(createEvent('click')); |
| 305 | + }); |
| 306 | + |
| 307 | + // Effect triggered by inner `act` call should not have been |
| 308 | + // flushed yet. |
| 309 | + expect(counter).to.equal(0); |
| 310 | + }); |
| 311 | + |
| 312 | + // Effects triggered by inner `act` call should now have been |
| 313 | + // flushed. |
| 314 | + expect(counter).to.equal(2); |
| 315 | + }); |
| 316 | + |
| 317 | + it('should only flush updates when outer `act` call returns', () => { |
| 318 | + function Button() { |
| 319 | + const [count, setCount] = useState(0); |
| 320 | + const increment = () => setCount(count => count + 1); |
| 321 | + return <button onClick={increment}>{count}</button>; |
| 322 | + } |
| 323 | + |
| 324 | + render(<Button />, scratch); |
| 325 | + const button = scratch.querySelector('button'); |
| 326 | + expect(button.textContent).to.equal('0'); |
| 327 | + |
| 328 | + act(() => { |
| 329 | + act(() => { |
| 330 | + button.dispatchEvent(createEvent('click')); |
| 331 | + }); |
| 332 | + |
| 333 | + // Update triggered by inner `act` call should not have been |
| 334 | + // flushed yet. |
| 335 | + expect(button.textContent).to.equal('0'); |
| 336 | + }); |
| 337 | + |
| 338 | + // Updates from outer and inner `act` calls should now have been |
| 339 | + // flushed. |
| 340 | + expect(button.textContent).to.equal('1'); |
| 341 | + }); |
| 342 | + }); |
203 | 343 | });
|
0 commit comments