-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TS migration] Migrate react-native-onyx mock #35674
Changes from all commits
b0c8780
20ad77b
732c20e
ffebb9c
50d1404
b5b642b
52408bb
3fd24ea
83a5b3b
7fdbd4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we get rid of this, or add a comment explaining why we need it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't get rid of this but I've added a comment explaining the reason we are disabling the rule. |
||
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}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.