-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow async functions in ReactNativeTester and add tests
Summary: Changelog: [internal] This allows async functions to be passed to `runTask` (just a type change really) and adds tests for ReactNativeTester. Error handling isn't currently set up correctly, so those tests are disabled for now. Differential Revision: D66698547
- Loading branch information
1 parent
67bff87
commit b037250
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
packages/react-native/src/private/__tests__/ReactNativeTester-itest.js
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,72 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
import '../../../Libraries/Core/InitializeCore'; | ||
import * as ReactNativeTester from './ReactNativeTester'; | ||
|
||
describe('ReactNativeTester', () => { | ||
describe('runTask', () => { | ||
it('should run a task synchronously', () => { | ||
const task = jest.fn(); | ||
|
||
ReactNativeTester.runTask(task); | ||
|
||
expect(task).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
// TODO: fix error handling and make this pass | ||
it.skip('should re-throw errors from the task synchronously', () => { | ||
Check warning on line 26 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js GitHub Actions / test_js (20)
|
||
expect(() => { | ||
ReactNativeTester.runTask(() => { | ||
throw new Error('test error'); | ||
}); | ||
}).toThrow('test error'); | ||
}); | ||
|
||
it('should exhaust the microtask queue synchronously', () => { | ||
const lastMicrotask = jest.fn(); | ||
|
||
ReactNativeTester.runTask(() => { | ||
queueMicrotask(() => { | ||
queueMicrotask(() => { | ||
queueMicrotask(() => { | ||
queueMicrotask(lastMicrotask); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
expect(lastMicrotask).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
// TODO: fix error handling and make this pass | ||
it.skip('should re-throw errors from microtasks synchronously', () => { | ||
Check warning on line 51 in packages/react-native/src/private/__tests__/ReactNativeTester-itest.js GitHub Actions / test_js (20)
|
||
expect(() => { | ||
ReactNativeTester.runTask(() => { | ||
queueMicrotask(() => { | ||
throw new Error('test error'); | ||
}); | ||
}); | ||
}).toThrow('test error'); | ||
}); | ||
|
||
it('should run async tasks synchronously', () => { | ||
let completed = false; | ||
|
||
ReactNativeTester.runTask(async () => { | ||
await Promise.resolve(6); | ||
completed = true; | ||
}); | ||
|
||
expect(completed).toBe(true); | ||
}); | ||
}); | ||
}); |
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