-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35674 from ruben-rebelo/ts-migration/react-native…
…-onyx-mock
- Loading branch information
Showing
2 changed files
with
43 additions
and
27 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,43 @@ | ||
/** | ||
* We are disabling the lint rule that doesn't allow the usage of Onyx.connect outside libs | ||
* because the intent of this file is to mock the usage of react-native-onyx so we will have to mock the connect function | ||
*/ | ||
|
||
/* eslint-disable rulesdir/prefer-onyx-connect-in-libs */ | ||
import type {ConnectOptions, OnyxKey} from 'react-native-onyx'; | ||
import Onyx, {withOnyx} from 'react-native-onyx'; | ||
|
||
let connectCallbackDelay = 0; | ||
function addDelayToConnectCallback(delay: number) { | ||
connectCallbackDelay = delay; | ||
} | ||
|
||
type ReactNativeOnyxMock = { | ||
addDelayToConnectCallback: (delay: number) => void; | ||
} & typeof Onyx; | ||
|
||
type ConnectionCallback<TKey extends OnyxKey> = NonNullable<ConnectOptions<TKey>['callback']>; | ||
type ConnectionCallbackParams<TKey extends OnyxKey> = Parameters<ConnectionCallback<TKey>>; | ||
|
||
const reactNativeOnyxMock: ReactNativeOnyxMock = { | ||
...Onyx, | ||
connect: <TKey extends OnyxKey>(mapping: ConnectOptions<TKey>) => { | ||
const callback = (...params: ConnectionCallbackParams<TKey>) => { | ||
if (connectCallbackDelay > 0) { | ||
setTimeout(() => { | ||
(mapping.callback as (...args: ConnectionCallbackParams<TKey>) => void)?.(...params); | ||
}, connectCallbackDelay); | ||
} else { | ||
(mapping.callback as (...args: ConnectionCallbackParams<TKey>) => void)?.(...params); | ||
} | ||
}; | ||
return Onyx.connect({ | ||
...mapping, | ||
callback, | ||
}); | ||
}, | ||
addDelayToConnectCallback, | ||
}; | ||
|
||
export default reactNativeOnyxMock; | ||
export {withOnyx}; |