11import { fireEvent } from "./events" ;
22
33function jestFakeTimersAreEnabled ( ) {
4- /* istanbul ignore else */
5- if ( typeof jest !== 'undefined' && jest !== null ) {
6- return (
7- // legacy timers
8- setTimeout . _isMockFunction === true ||
9- // modern timers
10- Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
11- )
12- }
13- // istanbul ignore next
14- return false
4+ /* istanbul ignore else */
5+ if ( typeof jest !== 'undefined' && jest !== null ) {
6+ return (
7+ // legacy timers
8+ setTimeout . _isMockFunction === true ||
9+ // modern timers
10+ Object . prototype . hasOwnProperty . call ( setTimeout , 'clock' )
11+ )
12+ }
13+ // istanbul ignore next
14+ return false
1515}
1616
1717const instance = { current : undefined } ;
@@ -21,35 +21,46 @@ const instance = {current: undefined};
2121 * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup
2222 */
2323afterEach ( ( ) => {
24- /**
25- * Behavior should be customizability like it is with `react-testing-library`
26- * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup
27- */
28- if ( instance . current ) fireEvent . sigkill ( instance . current ) ;
29- instance . current = undefined ;
24+ /**
25+ * Behavior should be customizability like it is with `react-testing-library`
26+ * @see https://testing-library.com/docs/react-testing-library/setup/#skipping-auto-cleanup
27+ */
28+ if ( instance . current ) fireEvent . sigkill ( instance . current ) ;
29+ instance . current = undefined ;
3030} )
3131
3232function getCurrentInstance ( ) {
33- /**
34- * Worth mentioning that this deviates from the upstream implementation
35- * of `dom-testing-library`'s `getDocument`, which throws an error whenever
36- * `window` is not defined.
37- *
38- * Admittedly, this is another way that `cli-testing-library` will need to figure out
39- * the right solution to this problem, since there is no omni-present parent `instance`
40- * in a CLI like there is in a browser. (although FWIW, "process" might work)
41- *
42- * Have ideas how to solve? Please let us know:
43- * https://github.com/crutchcorn/cli-testing-library/issues/2
44- */
45- return instance . current
33+ /**
34+ * Worth mentioning that this deviates from the upstream implementation
35+ * of `dom-testing-library`'s `getDocument`, which throws an error whenever
36+ * `window` is not defined.
37+ *
38+ * Admittedly, this is another way that `cli-testing-library` will need to figure out
39+ * the right solution to this problem, since there is no omni-present parent `instance`
40+ * in a CLI like there is in a browser. (although FWIW, "process" might work)
41+ *
42+ * Have ideas how to solve? Please let us know:
43+ * https://github.com/crutchcorn/cli-testing-library/issues/2
44+ */
45+ return instance . current
4646}
4747
4848// TODO: Does this need to be namespaced for each test that runs?
4949// That way, we don't end up with a "singleton" that ends up wiped between
5050// parallel tests.
5151function setCurrentInstance ( newInstance ) {
52- instance . current = newInstance ;
52+ instance . current = newInstance ;
5353}
5454
55- export { jestFakeTimersAreEnabled , setCurrentInstance , getCurrentInstance }
55+ function debounce ( func , timeout ) {
56+ let timer ;
57+ return ( ...args ) => {
58+ clearTimeout ( timer ) ;
59+ timer = setTimeout ( ( ) => {
60+ // eslint-disable-next-line @babel/no-invalid-this
61+ func . apply ( this , args ) ;
62+ } , timeout ) ;
63+ } ;
64+ }
65+
66+ export { jestFakeTimersAreEnabled , setCurrentInstance , getCurrentInstance , debounce }
0 commit comments